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]
					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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define INF (1<<28)

int main(){
  int time[101][101], cost[101][101];
  int n, m;
  int from, to, Cost, Time;
  
  while(cin >> n >> m){
    if(n == 0 && m == 0) break;
    for(int i = 1 ; i <= m ; i++){
      for(int j = 1 ; j <= m ; j++){
	if(i == j) time[i][j] = cost[i][j] = 0;
	else time[i][j] = cost[i][j] = INF;
      }
    }
    
    while(n--){
      cin >> from >> to >> Cost >> Time;
      cost[from][to] = Cost;
      time[from][to] = Time;
    }
    
    for(int k = 1 ; k <= m ; k++){
      for(int i = 1 ; i <= m ; i++){
	for(int j = 1 ; j <= m ; j++){
	  cost[i][j] = min(cost[i][j], cost[i][k]+cost[k][j]);
	  time[i][j] = min(time[i][j], time[i][k]+time[k][j]);
	}
      }
    }
    int num;
    cin >> n;
    while(n--){
      cin >> from >> to >> num;
      if(num == 0) cout << cost[from][to] << endl;
      else cout << time[from][to] << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#define INF 2 << 20
using namespace std;
int main(){
  int n,m;
  while(cin >> n >> m){
    if(n==0&&m==0) break;
    int cost[m+1][m+1];
    int time[m+1][m+1];

    for(int i=0;i<=m;i++)
      for(int j=0;j<=m;j++)
        cost[i][j] = time[i][j] = INF;

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

    for (int i = 1; i <= m; i++)      // ?????±????????????
      for (int j = 1; j <= m; j++)    // ????§???????
        for (int k = 1; k <= m; k++){  // ??????
          cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k]);
          time[j][k] = min(time[j][k], time[j][i] + time[i][k]);
        }


    int k;
    cin >> k;
    int p,q,r;
    for(int i=0;i<k;i++){
      cin >> p >> q >> r;
      switch(r){
      case 0: cout << cost[p][q] << endl; break;
      case 1: cout << time[p][q] << endl; break;
      default: break;
      }
    }
  }
}

C++: #include <iostream>
#include <vector>
#include <cstring>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)


struct edge {
    int from,to,money,time;
};
int d[101];
vector<edge> E;

int main(){
    int n,m;
    while(cin>>n>>m, n|m){
        E.resize(n);
        for(edge& e:E){
            cin>>e.from>>e.to>>e.money>>e.time;
        }
        int k; cin>>k;
        rep(i,k){
            int a,b,c; cin>>a>>b>>c;
            for(int& a:d) a=1<<30;
            d[a]=0;
            while(1){
                bool end=true;
                for(edge& e:E){
                    int cost=(c==0 ? e.money : e.time);
                    if(d[e.to]>d[e.from]+cost){
                        d[e.to]=d[e.from]+cost;
                        end=false;
                    }
                }
                if(end) break;
            }
            cout<<d[b]<<endl;
        }
    }
}

C++: // Name: Traveling Alone: One-way Ticket of Youth
// Level: 2
// Category: グラフ,Graph,最短経路,Warshall-Floyd
// Note:

/**
 * 最小金額と最短時間のそれぞれについて、Warshall-Floydで前処理しておけばよい。
 * オーダーは O(M^3 + K)。
 */
#include <iostream>
#include <vector>

using namespace std;

bool solve() {
    int N, M;
    if(!(cin >> N >> M)) return false;
    if(!N && !M) return false;

    vector<vector<int>> cost(M, vector<int>(M, -1));
    vector<vector<int>> dist(M, vector<int>(M, -1));
    for(int i = 0; i < N; ++i) {
        int a, b, c, t;
        cin >> a >> b >> c >> t;
        --a; --b;
        cost[a][b] = cost[b][a] = c;
        dist[a][b] = dist[b][a] = t;
    }
    for(int k = 0; k < M; ++k) {
        for(int i = 0; i < M; ++i) {
            for(int j = 0; j < M; ++j) {
                if(cost[i][k] != -1 && cost[k][j] != -1 && (cost[i][j] == -1 || cost[i][k]+cost[k][j] < cost[i][j])) {
                    cost[i][j] = cost[i][k] + cost[k][j];
                }
                if(dist[i][k] != -1 && dist[k][j] != -1 && (dist[i][j] == -1 || dist[i][k]+dist[k][j] < dist[i][j])) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }
    int K;
    cin >> K;
    while(K--) {
        int a, b, c;
        cin >> a >> b >> c;
        --a; --b;
        if(c == 0) {
            cout << cost[a][b] << endl;
        } else {
            cout << dist[a][b] << endl;
        }
    }
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    while(solve()) ;
    return 0;
}