Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		c_matrix := make([][]int, m)
		for i := range c_matrix {
			c_matrix[i] = make([]int, m)
		}
		t_matrix := make([][]int, m)
		for i := range t_matrix {
			t_matrix[i] = make([]int, m)
		}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			c, _ := strconv.Atoi(buf_[2])
			t, _ := strconv.Atoi(buf_[3])
			c_matrix[a-1][b-1] = c
			t_matrix[a-1][b-1] = t
		}
		c_dist := make([][]int, m)
		c_prev := make([][]int, m)
		t_dist := make([][]int, m)
		t_prev := make([][]int, m)
		for i := 0; i < m; i++ {
			c_dist[i] = make([]int, m)
			c_prev[i] = make([]int, m)
			t_dist[i] = make([]int, m)
			t_prev[i] = make([]int, m)
		}
		for i := 0; i < m; i++ {
			for j := 0; j < m; j++ {
				if c_matrix[i][j] == 0{
					c_dist[i][j] = INF
				} else {
					c_dist[i][j] = c_matrix[i][j]
				}
				if c_matrix[i][j] == 0{
					c_prev[i][j] = -1
				} else {
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0{
					t_dist[i][j] = INF
				} else {
					t_dist[i][j] = t_matrix[i][j]
				}
				if t_matrix[i][j] == 0{
					t_prev[i][j] = -1
				} else {
					t_prev[i][j] = i
				}
			}
			c_dist[i][i] = 0
			t_dist[i][i] = 0
		}
		for k := 0; k < m; k++ {
			for i := 0; i < m; i++ {
				for j := 0; j < m; j++ {
					c_newlen := c_dist[i][k] + c_dist[k][j]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[k][j]
					}
					if t_newlen < t_dist[i][j] {
						t_dist[i][j] = t_newlen
						t_prev[i][j] = t_prev[k][j]
					}
				}
			}
		}
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < k; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			p, _ := strconv.Atoi(buf_[0])
			q, _ := strconv.Atoi(buf_[1])
			r, _ := strconv.Atoi(buf_[2])
			switch r {
			case 0: fmt.Println(c_dist[p-1][q-1])
			case 1: fmt.Println(t_dist[p-1][q-1])
			}
		}
	}
}

C++: #include <iostream>

using namespace std;

const int MAX_N = 110;
const int INF = 10000;
int d_cost[MAX_N][MAX_N];
int d_time[MAX_N][MAX_N];
int n, m;

void warshall_floyd(int d[MAX_N][MAX_N])
{
    for (int k = 0; k < m; k++)
        for (int i = 0; i < m; i++)
            for (int j = 0; j < m; j++)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main()
{
    while (cin >> n >> m, n || m) {
        int a, b, cost, time;
        int k, p, q, r;

        // 初期化
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {
                if (i == j) 
                    d_cost[i][j] = d_time[i][j] = 0;
                else
                    d_cost[i][j] = d_time[i][j] = INF;
            }

        for (int i = 0; i < n; i++) {
            cin >> a >> b >> cost >> time;
            a--, b--;
            d_cost[a][b] = d_cost[b][a] = cost;
            d_time[a][b] = d_time[b][a] = time;
        }
//        warshall_floyd(d_cost);
//        warshall_floyd(d_time);

        cin >> k;
        for (int i = 0; i < k; i++) {
            cin >> p >> q >> r;

            p--, q--;
            if (r == 0)
                cout << d_cost[p][q] << endl;
            else
                cout << d_time[p][q] << endl;
        }
    }

    return 0;
}

C++: #include<math.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;
const int INF = (1<<30);
int cost[200][200], time[200][200],dist[200];
int x[200][200];
bool vis[200];
int M,N;

void dij(int p, int q, int r){
	int i,j,k,l,m,n,mn;
	rep(i,M)rep(j,M)x[i][j]= (r==0)?cost[i][j]:time[i][j];
	rep(i,M)vis[i]=false,dist[i]=INF;
	dist[p-1]=0;
	for(;;){
		k=-1,mn=INF;
		rep(i,M)if(!vis[i]&&dist[i]<mn){mn=dist[i],k=i;}
		if(k==-1 || mn>=INF) break;
		if(k==q-1)break;
		vis[k]=true;
		rep(i,M)if(!vis[i])dist[i]=min(dist[i],dist[k]+x[k][i]);
	}
	cout<<dist[q-1]<<endl;
}

int main(){
	int i,j,k,l,m,n;
	
	while(cin>>N>>M){
		if((N|M)==0) break;

		rep(i,M){
			dist[i]=INF; 
		    rep(j,M){
			  if(i==j) {cost[i][j]=time[i][j]=0; continue;}
			  cost[i][j]=time[i][j]=INF;
			}
		}
		rep(i,N){//graph info
			int a,b,c,t;
			cin>>a>>b>>c>>t;
			a--; b--;
			cost[a][b]=cost[b][a]=c; time[a][b]=time[b][a]=t;
		}
		cin>>k;
		rep(i,k){//question
			int p,q,r;
			cin>>p>>q>>r;
			dij(p,q,r);
		}
	}
}

C++: #define NODE 0
#define WEIGHT 1
#define COST_IDX 0
#define TIME_IDX 1
#define P_IDX 0
#define Q_IDX 1
#define R_IDX 2
#include <iostream>
#include <unordered_map>
#include <queue>
#include <vector>
using namespace std;

int n, m, k;
unordered_map<int, unordered_map<int, int*> > weights;
int **requests;

bool read_input() {
  cin >> n >> m;
  if (n*m == 0) 
    return false;
  for (int i = 0; i < n; i++) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    weights[a][b] = new int[2];
    weights[b][a] = new int[2];
    weights[a][b][COST_IDX] = weights[b][a][COST_IDX] = c;
    weights[a][b][TIME_IDX] = weights[b][a][TIME_IDX] = t;
  }
  cin >> k;
  requests = new int*[k];
  for (int i = 0; i < k; i++) {
    requests[i] = new int[3];
    cin >> requests[i][P_IDX] >> requests[i][Q_IDX] >> requests[i][R_IDX];
  }
  return true;
}

int resolve_request(int i_th) {
  int p, q, r;
  p = requests[i_th][P_IDX];
  q = requests[i_th][Q_IDX];
  r = requests[i_th][R_IDX];

  /* keep track of latest weight_sum of each node since values 
   * in priority queue might not be up to date (since STL prioroty 
   * queue does not support value change, ignore popped itmes
   * that does not match values in this map). */  
  unordered_map<int, int> weight_sum_map;
  
  /* starting node has weight_sum of 0 */
  weight_sum_map[p] = 0;

  /* create priority queue and push p into it as the initial element */
  auto cmp = [](int *a, int *b) { return a[WEIGHT] > b[WEIGHT]; };
  priority_queue<int*, vector<int*>, decltype(cmp)> pq(cmp); 
  int *itm = new int[2];
  itm[NODE] = p;
  itm[WEIGHT] = 0;
  pq.push(itm);

  while (!pq.empty()) {
    itm = pq.top(); 
    int weight_sum = itm[WEIGHT];
    int node = itm[NODE];

    /* release allocated mem and remove from priority queue*/
    delete[] itm;
    pq.pop();

    // if q was popped, release all allocated mem and break 
    if (node == q) {/*
      while (!pq.empty()) {
	itm = pq.top();
	delete[] itm;
	pq.pop();
	}*/
      break;
    }

    /* ignore if wight_sum value not up to date */
    if (weight_sum != weight_sum_map[node]) {
      continue;
    }
    auto connections = weights[node];
    for (auto itr = connections.begin(); itr != connections.end(); itr++) {
      int adj_node = itr->first;
      int w = itr->second[r]; // get the requested weight type r == 0(cost) or 1(time)
      int new_weight_sum = weight_sum + w;
      if (weight_sum_map.count(adj_node) == 0 || new_weight_sum < weight_sum_map[adj_node]) {
	weight_sum_map[adj_node] = new_weight_sum;
	itm = new int[2];
	itm[NODE] = adj_node;
	itm[WEIGHT] = new_weight_sum;
	pq.push(itm);
      }
    }
  }
  return weight_sum_map[q];
}

void solve() {
  for (int i = 0; i < k; i++)
    cout << resolve_request(i) << endl;
}

void cleanup() {/*
  for (int i = 0; i < k; i++) {
    delete[] requests[i];
  }
  delete[] requests;
		*/
  /*
  for (auto itr_a = weights.begin(); itr_a != weights.end(); itr_a++) {
    auto m = itr_a->second;
    for (auto itr_b = m.begin(); itr_b != m.end(); itr_b++) {
      delete[] itr_b->second;
    }
    m.clear();
    }*/
  weights.clear();
}

int main() {
  while(read_input()) {
    solve();
    cleanup();
  }
  return 0;
}