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<cstdio>
#include<algorithm>

using namespace std;

int main(void){
	int floy[101][101][2];
	while(1){
		int n,m;
		scanf("%d%d",&n,&m);
		if(n==0&&m==0){
			break;
		}
			
		for(int b=0;b<2;b++){
			for(int i=0;i<=m;i++){
				for(int j=0;j<=m;j++){
					floy[i][j][b]=1<<29;
				}
			}
		}
		
		for(int i=0;i<n;i++){
			int a,b,cost,time;
			scanf("%d%d%d%d",&a,&b,&cost,&time);
			floy[a][b][0]=cost;
			floy[b][a][0]=cost;
			floy[a][b][1]=time;
			floy[b][a][1]=time;
		}
		
		for(int b=0;b<2;b++){
					for(int k=1;k<=m;k++){
			for(int i=1;i<=m;i++){
				for(int j=1;j<=m;j++){
						floy[i][j][b]=min(floy[i][j][b],floy[i][k][b]+floy[k][j][b]);
					}
				}
			}
		}
		
		for(int b=0;b<2;b++){
			for(int i=1;i<=m;i++){
				for(int j=1;j<=m;j++){
					printf("%d ",floy[i][j][b]);
				}
				puts("");
			}
			puts("\n");
		}
		
		int kazu;
		scanf("%d",&kazu);
		
		for(int i=0;i<kazu;i++){
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			printf("%d\n",floy[a][b][c]);
		}
	}
}

C++: #include<cstdio>
#include<algorithm>
#include<queue>

#define INF 1<<29

using namespace std;

int main(){
	while(1){
		int n,m;
		scanf("%d%d",&m,&n);
		if(m==0 && n==0){
			break;
		}
		int cost[101][101];
		int time[101][101];
		
		for(int i=0;i<101;i++)
		for(int j=0;j<101;j++)
		cost[i][j]=time[i][j]=INF;
		
		for(int i=0;i<m;i++){
			int a,b,c,d;
			scanf("%d%d%d%d",&a,&b,&c,&d);
			cost[a][b]=cost[b][a]=c;
			time[a][b]=time[b][a]=d;
		}
		
		for(int k=1;k<=n;k++){
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;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]);
				}
			}
		}
		
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			if(c==0){
				printf("%d\n",cost[a][b]);
			}else{
				printf("%d\n",time[a][b]);
			}
		}
	}
}

C++: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define INF 1e9
using namespace std;
typedef long long ll;

ll min(ll a,ll b){
	if(a>b)return b;
	else return a;
}


int main(){
	int n,m,a,b,cost,time,l,p,q,r;
	while(cin>>n>>m,n||m){
		ll mt[m][m],mc[m][m];
		rep(i,m)rep(j,m)mt[i][j]=mc[i][j]=INF;
		rep(i,n){
			cin>>a>>b>>cost>>time;
			mt[a-1][b-1]=mt[b-1][a-1]=time;
			mc[a-1][b-1]=mt[b-1][a-1]=cost;
		}
		rep(k,m){
			rep(i,m){
				rep(j,m){
					mt[i][j]=min(mt[i][j],mt[i][k]+mt[k][j]);
					mc[i][j]=min(mc[i][j],mc[i][k]+mc[k][j]);
				}
			}
		}
		cin>>l;
		rep(i,l){
			cin>>p>>q>>r;
			if(r==0)cout<<mc[p-1][q-1]<<endl;
			else cout<<mt[p-1][q-1]<<endl;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)
#define MIN(a, b) ((a)<(b)?(a):(b))
#define INF 10e6
using namespace std;
 /*temp*/

int main(){
    int n, m, l;
    while(cin>>m>>n,n){
        ll cost[n][n],time[n][n];
        // set table
        REP(i,n){
            REP(j,n){
                cost[i][j]=time[i][j]=INF;
            }
            cost[i][i]=time[i][i]=0;
        }
        // read
        REP(i,n){
            int a,b,c,t;
            cin>>a>>b>>c>>t;
            a--;
            b--;
            cost[a][b]=cost[b][a]=MIN(c,cost[a][b]);
            time[a][b]=time[b][a]=MIN(t,time[a][b]);
        }
        // Warshall???Floyd Algorithm
        REP(a,n){
            REP(b,a+1){
                REP(k,n){
                    cost[a][b]=cost[b][a]=MIN(cost[a][b],cost[a][k]+cost[k][b]);
                    time[a][b]=time[b][a]=MIN(time[a][b],time[a][k]+time[k][b]);
                }
            }
        }
        // 
        int q;
        cin>>q;
        REP(i,q){
            int s,d,type;
            cin>>s>>d>>type;
            s--;
            d--;
            cout<<type==0?cost[s][d]:time[s][d]<<'\n';
        }
    }
    return 0;
}

C++: #include<iostream>

int n, m, a, b, k, p, q, r, *ans;
int **w[2],**d[2],**pre_vertex[2];

void solve(){
	for(int z=0; z<2; z++){
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++)
				d[z][i][j] = w[z][i][j];
		}
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++){
				for(int l=0; l<m; l++){
					int dis = d[z][j][i]+d[z][i][l];
					if(dis<d[z][j][l])
						d[z][j][l] = dis;
				}
			}
		}
	}
}

int main(){
	while(true){
		std::cin >> n >> m;
		if(!n && !m)
			return 0;
		for(int l=0; l<2; l++){
			w[l] = new int*[m];
			d[l] = new int*[m];
			for(int i=0; i<m; i++){
				w[l][i] = new int[m];
				d[l][i] = new int[m];
				for(int j=0; j<m; j++)
					w[l][i][j] = 1000000000;
			}
		}
		for(int i=0; i<n; i++){
			std::cin >> a >> b;
			std::cin >> w[0][a-1][b-1] >> w[1][a-1][b-1];
			w[0][b-1][a-1] = w[0][a-1][b-1];
			w[1][b-1][a-1] = w[1][a-1][b-1];
		}
		solve();
		std::cin >> k;
		ans = new int[k];
		for(int i=0; i<k; i++){
			std::cin >> p >> q >> r;
			ans[i] = d[r][p-1][q-1];
		}
		for(int i=0; i<k; i++)
			std::cout << ans[i] << std::endl;
		for(int i=0; i<2; i++){
			for(int j=0; j<m; j++){
				delete[] w[i][j];
				delete[] d[i][j];
			}
			delete[] w[i];
			delete[] d[i];
		}
		delete[] ans;
	}
}