Go: // Warshall–Floyd Algorithm

package main

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

var scanner = bufio.NewScanner(os.Stdin)

const INF = 2000000000

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 <bits/stdc++.h> // only for GCC

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define loop(i,a,b) for(ull i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define iter(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define riter(i,c) for(auto i=(c).rbegin(); i!=(c).rend(); ++i)
#define MAX_V 100
const double eps = 1e-10;
const double pi  = acos(-1.0);
const double INF = (int)1e8;

int cost[MAX_V][MAX_V]; int d[MAX_V];
int t[MAX_V][MAX_V];
bool used[MAX_V];
int V;
// cost[u][v]は辺e=(u,v)のコスト(存在しない場合はINF) // 頂点sからの最短距離
// すて&#12441;に使われたかのフラク&#12441;
// 頂点数
// 始点sから各頂点への最短距離を求める
void dijkstra(int s) {
    fill(d, d + V, INF);
    fill(used, used + V, false);
    d[s] = 0;
    while(true) {
        int v = -1;
        // また&#12441;使われていない頂点のうち距離か&#12441;最小のものを探す
        for (int u = 0; u < V; u++) {
            if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
        }
        if (v == -1) break;
        used[v] = true;
        for (int u = 0; u < V; u++) {
            d[u] = min(d[u], d[v] + cost[v][u]);
            //cout << d[u] << endl;
        }
    }
}
void dijkstra2(int s) {
    fill(d, d + V, INF);
    fill(used, used + V, false);
    d[s] = 0;
    while(true) {
        int v = -1;
        // また&#12441;使われていない頂点のうち距離か&#12441;最小のものを探す
        for (int u = 0; u < V; u++) {
            if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
        }
        if (v == -1) break;
        used[v] = true;
        for (int u = 0; u < V; u++) {
            d[u] = min(d[u], d[v] + t[v][u]);
        }
    }
}

int main(){
    int n,m;
    int a,b;
    int x;
    bool w;
    while(cin >> n >> m && n|m){
    	rep(i,m)rep(j,m){
    		cost[i][j]=INF;
    		t[i][j]=INF;
    	}
    	V=m;
        rep(i,n){
            cin >> a >> b;
            a--;b--;
            cin >> cost[a][b] >> t[a][b];
            cost[b][a]=cost[a][b];
            t[b][a]=t[a][b];
        }
        cin >> x;
        rep(i,x){
            cin >> a >> b >> w;
            a--;b--;
            if(!w){
                dijkstra(a);
                cout << d[b] << endl;
            }else{
                dijkstra2(a);
                cout << d[b] << endl;
            }
        }
    }
}

C++: #include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<complex>
#include<set>
#include<map>
#include<algorithm>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

int main(){
	
	A:;
	int n,m;
	cin>>n>>m;
	if(n==0)return 0;
	
	const int N = 111;
	const int INF = 1000000000;
	
	int dpcost[N][N];
	int dptime[N][N];
	rep(i,N)rep(j,N)dpcost[i][j]=dptime[i][j]=INF;
	rep(i,N)dpcost[i][i]=dptime[i][i]=0;
	
	rep(i,n){
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		dpcost[a][b]=c;
		dpcost[b][a]=c;
		dptime[a][b]=d;
		dptime[b][a]=d;
	}
	
	
	reps(k,1,n+1)reps(i,1,n+1)reps(j,1,n+1){
		dpcost[i][j] = min(dpcost[i][j], dpcost[i][k]+dpcost[k][j]);
		dptime[i][j] = min(dptime[i][j], dptime[i][k]+dptime[k][j]);
	}
	
	
	int r;
	cin>>r;
	
	rep(i,r){
		int a,b,c;
		cin>>a>>b>>c;
		if(c==0)printf("%d\n",dpcost[a][b]);
		if(c==1)printf("%d\n",dptime[a][b]);
	}
	
	goto A;
}

C++: //2013/11/15(Fri)

#include <iostream>
using namespace std;
int main()
{
	int n,m;
	int a,b,cost[100][100],time[100][100];
	int k;
	int p,q,r;
	int INF = 1001;

	int dcost[100][100],dtime[100][100];
	while(true){
		cin >> n >> m;
		if(n==0 && m==0)
			break;

		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++){
				if(i==j){
					cost[i][j] = 0;
					time[i][j] = 0;
				}		
				else{
					cost[i][j] = INF;
					time[i][j] = INF;
				}
			}

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

			
		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++){
				dcost[i][j] = cost[i][j];
				dtime[i][j] = time[i][j];
			}
		for(int h=0; h<n; h++)
			for(int i=0; i<n; i++)
				for(int j=0; j<n; j++){
					if(dcost[i][j] > dcost[i][h] + dcost[h][j])
						dcost[i][j] = dcost[i][h] + dcost[h][j];
					if(dtime[i][j] > dtime[i][h] + dtime[h][j])
						dtime[i][j] = dtime[i][h] + dtime[h][j];
				}

		cin >> k;
		for(int i=0; i<k; i++){
			cin >> p >> q >> r;
			if(r == 0)
				cout << dcost[p][q] << endl;
			else if(r == 1)
				cout << dtime[p][q] << endl;
		}
	}
	return 0;
}