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<stdio.h>

int d[100][100];
int s[100][100];
int dd[100][100];
int ss[100][100];
int main(){
	while(1){
	int a,b;
	scanf("%d%d",&a,&b);
	if(b==0)return 0;
	for(int i=0;i<b;i++)
		for(int j=0;j<b;j++){
			d[i][j]=99999999;
			dd[i][j]=99999999;
		}
	
	for(int i=0;i<a;i++){
		int m,n,o,p;
		scanf("%d%d%d%d",&m,&n,&o,&p);
		d[m-1][n-1]=o;
		d[n-1][m-1]=o;
		dd[m-1][n-1]=p;
		dd[n-1][m-1]=p;
	}
	int h=0;
	for(int i=0;i<b;i++){
		for(int j=0;j<b;j++){
			s[i][j]=j;
		}
	}
	while(1<<h<b-1){
		for(int i=0;i<b;i++){
			for(int j=0;j<b;j++){
				for(int k=0;k<b;k++){
					if(d[i][j]>d[k][j]+d[i][k]){
						d[i][j]=d[i][k]+d[k][j];
						s[i][j]=s[i][k];
					}
				}
			}
		}
		h++;
	}
	h=0;
	for(int i=0;i<b;i++){
		for(int j=0;j<b;j++){
			ss[i][j]=j;
		}
	}
	while(1<<h<b-1){
		for(int i=0;i<b;i++){
			for(int j=0;j<b;j++){
				for(int k=0;k<b;k++){
					if(dd[i][j]>dd[k][j]+dd[i][k]){
						dd[i][j]=dd[i][k]+dd[k][j];
						ss[i][j]=ss[i][k];
					}
				}
			}
		}
		h++;
	}
	
	int x;
	scanf("%d",&x);
	for(int i=0;i<x;i++){
		int m,n,o;
		scanf("%d%d%d",&m,&n,&o);
		if(o)printf("%d\n",dd[m-1][n-1]);
		else printf("%d\n",d[m-1][n-1]);
	}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
#define rep(i,n) for(int i = 0 ; i < n ; i++)
 
int main(){
    int mon[100][100];
    int tim[100][100];
    int n,m;
    while(cin >> m >> n && n ){
        rep(i,n)rep(j,n) mon[i][j] = tim[i][j] = 1e9;
        for(int i = 0 ; i < m ; i++){
            int a,b,c,d;
            cin >> a >> b >> c >> d;
            a--,b--;
            mon[a][b] = mon[b][a] = c;
            tim[a][b] = tim[b][a] = d;
        }
        rep(i,n)rep(j,n)rep(k,n){
            mon[j][k] = min(mon[j][k],mon[j][i]+mon[i][k]);
            tim[j][k] = min(tim[j][k],tim[j][i]+tim[i][k]);
        }
        int R;
        cin >> R;
        rep(i,R){
            int a,b,c;
            cin >> a >> b >> c;
            a--,b--;
            if(c) cout << tim[a][b];
            else cout << mon[a][b];
            cout << endl;
        }
    }
}

C++: #include <iostream>
using namespace std;

#define REP(i,p,n) for(int i=p;i<n;++i)
#define rep(i,n) REP(i,0,n)

typedef pair<int,int> pii;
const int INF=1e6, MAXM=128;
int C[2][MAXM][MAXM], dist[MAXM], visited[MAXM], N, M;

int main()
{
        int a,b,c,d;
        while(cin>>N>>M&&(N|M))
        {
                rep(i,M) rep(j,N) C[0][i][j]=C[1][i][j]=INF;
                rep(i,N){ cin>>a>>b>>c>>d; --a; --b; C[0][a][b]=C[0][b][a]=c; C[1][a][b]=C[1][b][a]=d; }

                cin>>d;
                rep(i,d)
                {
                        fill(dist,dist+MAXM,INF);
                        fill(visited,visited+MAXM,false);

                        cin>>a>>b>>c; --a; --b;
                        dist[a]=0;

                        while(true)
                        {
                                int from=-1;
                                rep(i,M) if(!visited[i]&&(from==-1||dist[i]<dist[from])){ from=i; }
                                if(from==b){ cout<<dist[from]<<endl; break; } else { visited[from]=true; }

                                rep(to,M) dist[to]=min(dist[to],dist[from]+C[c][from][to]);
                        }
                }
        }

        return 0;
}

C++: /*****include*****/
#include <iostream>
#include <fstream>

/*****デバッグ定義*****/
//#define DEBUG
#define INF 10000
/*****マクロ定義*****/

/*****名前空間*****/
using namespace std;

/*****グローバル変数置き場*****/
typedef struct{
	int time;
	int cost;
}Field;
Field field[100][100];
/*****その他関数置き場*****/
int djikstra(int start,int goal,int num,int order){
	int *dis;
	bool *used;
	dis = new int [num];
	used = new bool [num];
	int station;
	int res;
	for(int i=0;i<num;i++){
		dis[i] = INF;
		used[i] = false;
	}
	dis[start] = 0;
	while(true){
		station = -1;
		for(int i=0;i<num;i++){
			if(!used[i] && (station == -1 || dis[i] < dis[station])){
				station = i;
			}
		}
		if(station == -1)	break;
		used[station] = true;
		for(int i=0;i<num;i++){
			int adder;
			adder = ((order) ? field[station][i].time : field[station][i].cost);
			if(dis[i] > dis[station]+adder){
				dis[i] = dis[station]+adder;
			}
		}
	}
	res = dis[goal];
	delete dis;
	delete used;
	return res;
}
/*****main関数*****/
int main(){
	/*****ファイルオープン*****/
#ifdef DEBUG
	ofstream fout("output.txt");
	ifstream fin("input.txt");
	if(!fout || !fin){
		cout << "Can't open the file.\n";
		return;
	}
#endif
	/*****変数置き場*****/
	int rail_num,sta_num;
	while(1){
		for(int i=0;i<100;i++){
			for(int j=0;j<100;j++){
				field[i][j].time = INF;
				field[i][j].cost = INF;
			}
		}
		cin >> rail_num >> sta_num;
		if(rail_num == 0 && sta_num == 0)	break;
		for(int i=0;i<rail_num;i++){
			int st1,st2,cost,time;
			cin >> st1 >> st2 >> cost >> time;
			field[st1-1][st2-1].cost = cost;
			field[st2-1][st1-1].cost = cost;
			field[st1-1][st2-1].time = time;
			field[st2-1][st1-1].time = time;
			field[st1-1][st1-1].cost = 0;
			field[st2-1][st2-1].time = 0;
		}
		int loop;
		int min;
		cin >> loop;
		for(int i=0;i<loop;i++){
			int order,start,goal;
			cin >> start >> goal >> order;
			if(order==1){
				min = djikstra(start-1,goal-1,sta_num,order);
			}else{
				min = djikstra(start-1,goal-1,sta_num,order);
			}
			cout << min << endl;
		}
	}
	/*****処理部*****/
	
	/*****処理終了後*****/
#ifdef DEBUG
	fout.close();
	fin.close();
#endif
	return 0;
}