Go: // Warshall–Floyd Algorithm

package main

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

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
					c_prev[i][j] = -1
				} else {
					c_dist[i][j] = c_matrix[i][j]
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0 {
					t_dist[i][j] = INF
					t_prev[i][j] = -1
				} else {
					t_dist[i][j] = t_matrix[i][j]
					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]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[k][j]
					}
				}
			}
		}
		for k := 0; k < m; k++ {
			for i := 0; i < m; i++ {
				for j := 0; j < m; j++ {
					t_newlen := t_dist[i][k] + t_dist[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>
#include <algorithm>
const int INF = 1000000;
using namespace std;
int map[101][101][2];//a, b, cost, time;
int d[101];//始点からの最短距離
bool used[101];//使われたかのフラグ
int V;//頂点数

int dijkstra(int start, int end, bool flag){
	fill(d, d + V + 1, INF);
	fill(used, used + V + 1, false);
	d[start] = 0;

	while(1){
		int v = -1;
		for(int u=1;u<=V;u++){
			if(!used[u] && (v == -1 || d[u] < d[v]))v = u;
		}

		if(v == -1)break;

		used[v] = true;

		for(int u=1;u<=V;u++){
			d[u] = min(d[u], d[v] + map[v][u][flag]);
		}
	}

	return d[end];
}

int main(){
	int n, m;
	while(cin >> n >> m, n){
		V = m;

		for(int i=0;i<101;i++){
			for(int j=0;j<101;j++){
				map[i][j][0] = (map[i][j][1] = INF);
			}
		}

		for(int i=n;i;i--){
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			map[a][b][0] = cost;
			map[a][b][1] = time;
			map[b][a][0] = cost;
			map[b][a][1] = time;
		}

		int k;
		cin >> k;
		while(k--){
			int p, q, r;
			cin >> p >> q >> r;
			cout << dijkstra(p, q, r) << '\n';
		}
	}
}

C++: #include <iostream>
using namespace std;
#define REP(i,n) for (int i=0; i<(n); ++i)

int d[2][100][100];

int main() {
    ios::sync_with_stdio(false);
    int n, m;
    while (cin >> n >> m, n || m) {
        REP(i,2) REP(j,m) REP(k,m) {
            d[i][j][k] = (j == k ? 0 : 1e8);
        }
        int a, b, cost, time;
        REP(i,n) {
            cin >> a >> b >> cost >> time;
            -- a; -- b;
            d[0][a][b] = min(d[0][a][b], cost);
            d[0][b][a] = min(d[0][b][a], cost);
            d[1][a][b] = min(d[1][a][b], time);
            d[1][b][a] = min(d[1][b][a], time);
        }
        REP(k,m) REP(i,m) REP(j,m) {
            d[0][i][j] = min(d[0][i][j], d[0][i][k] + d[0][k][j]);
            d[0][j][i] = min(d[0][j][i], d[0][j][k] + d[0][k][i]);
            d[1][i][j] = min(d[1][i][j], d[1][i][k] + d[1][k][j]);
            d[1][j][i] = min(d[1][j][i], d[1][j][k] + d[1][k][i]);
        }
        int k, p, q, r;
        cin >> k;
        REP(i,k) {
            cin >> p >> q >> r;
            -- p; -- q;
            cout << d[r][p][q] << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <utility>
#include <queue>
#include <functional>
#define N 101
#define INF 200000000
using namespace std;

int dist[N];
typedef pair<int,int> pii;

std::vector<pii> e1[N];
std::vector<pii> e2[N];

int main(void){
	int a,b,c,d;
	int n,m;
	while(cin>>n>>m,n||m){
		int dp[m+1][m+1];
		int dp1[m+1][m+1];
		for(int i=1;i<=m;i++){
			for(int j=1;j<=m;j++){
				if(i==j)dp[i][j]=0;
				else dp[i][j]=INF;
			}
		}
		for(int i=1;i<=m;i++){
			for(int j=1;j<=m;j++){
				if(i==j)dp1[i][j]=0;
				else dp1[i][j]=INF;
			}
		}
		for(int i=0;i<n;i++){
			cin>>a>>b>>c>>d;
			// e1[a].push_back(make_pair(c,b));
			// e2[a].push_back(make_pair(d,b));
			dp[b][a]=c;
			dp[a][b]=c;
			dp1[b][a]=d;
			dp1[a][b]=d;
		}

		for(int k=1;k<=m;k++){
			for(int u=1;u<=m;u++){
				for(int v=1;v<=m;v++){
					dp[u][v]=min(dp[u][v],dp[u][k]+dp[k][v]);
				}
			}
		}

		for(int k=1;k<=m;k++){
			for(int u=1;u<=m;u++){
				for(int v=1;v<=m;v++){
					dp1[u][v]=min(dp1[u][v],dp1[u][k]+dp1[k][v]);
				}
			}
		}

		int k;
		cin>>k;
		while(k--){
			cin>>a>>b>>c;
			if(c) cout<<dp1[a][b]<<endl;
			else cout<<dp[a][b]<<endl;
		}
	}
	return 0;
}

C++: //AOJ 0200
//菴ソ縺?婿繧ゅｏ縺九ｉ縺ェ縺?¥縺帙↓include縺励∪縺上ｋ莠コ髢薙?繧ッ繧コ
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<list>
#include<deque>
using namespace std;
#define INF 1 << 30
#define MAX_B 101

int a, b, i, j, k, money[MAX_B][MAX_B], time[MAX_B][MAX_B], n, m, c, d;
bool judge;
stack<int> left, right;


int main(){
  while(1){
    scanf("%d %d", &n, &m);
    if(n == 0 && m == 0) return 0;
    for(i = 1; i <= m; i++){
      for(j = 1; j <= m; j++){
	money[i][j] = INF;
	time[i][j] = INF;
      }
    }
    for(i = 1; i <= m; i++){
      money[i][i] = 0;
      time[i][i] = 0;
    }
    for(i = 0; i < n; i++){
      scanf("%d %d %d %d", &a, &b, &c, &d);
      money[a][b] = c;
      money[b][a] = c;
      time[a][b] = d;
      time[b][a] = d;
    }
    scanf("%d", &a);
    for(i = 1; i <= m; i++){
      for(j = 1; j <= m; j++){
	for(k = 1; k <= m; k++){
	  if(money[j][k] > money[j][i] + money[i][k]){
	    money[j][k] = money[j][i] + money[i][k];
	    money[k][j] = money[j][i] + money[i][k];
	  }
	  if(time[j][k] > time[j][i] + time[i][k]){
	    time[j][k] = time[j][i] + time[i][k];
	    time[k][j] = time[j][i] + time[i][k];
	  }
	}
      }
    }
    for(i = 0; i < a; i++){
      scanf("%d %d %d", &b, &c, &d);
      if(d == 1)
	printf("%d\n", time[b][c]);
      if(d == 0)
	printf("%d\n", money[b][c]);
    }
  }
  return 0;
}