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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
#define inf 1e9


int main(){
	while(1){
	int n,m;
	cin>>n>>m;
	if(n==0)break;
	vector<vector<mp> > gt(m);
	vector<vector<mp> > gc(m);
	vector<vector<ll> > dpt(m,vector<ll> (m,inf) );
	vector<vector<ll> > dpc(m,vector<ll> (m,inf) );
	for(int i=0;i<n;i++){
		ll a,b,c,t;
		cin>>a>>b>>c>>t;
		a--,b--;
		dpc[a][b]=dpc[b][a] = c;
		dpt[a][b] = dpt[b][a] = t;
	}
	for(int i=0;i<m;i++)dpc[i][i] = dpt[i][i] = 0;
	for(int k=0;k<m;k++)
		for(int i=0;i<m;i++)
			for(int j=0;j<m;j++)
				if(dpc[i][k] != inf && dpc[k][j] != inf){
					dpc[i][j] = min(dpc[i][j],dpc[i][k]+dpc[k][j]);
					dpt[i][j] = min(dpt[i][j],dpt[i][k]+dpt[k][j]);
				}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		int p,q,r;
		cin>>p>>q>>r;
		p--,q--;
		if(r==0){
			cout<<dpc[p][q]<<endl;
		}else{
			cout<<dpt[p][q]<<endl;
		}
	}
	}

	return 0;
}

C++: // AOJ 0200 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0200)
#include<cstdio>
#include<algorithm>
#include<limits>
#define rep(i,a) for(int i=0;i<(a);++i)

const int MAX_M = 100, INF = std::numeric_limits<int>::max()>>4;

int n, m;
int d[2][MAX_M][MAX_M];

int main()
{
	while( scanf( "%d%d", &n, &m ), n|m )
	{
		rep( k, 2 ) rep( i, m ) rep( j, m )
			d[k][i][j] = i==j?0:INF;

		rep( i, n )
		{
			int a, b, c, t;
			scanf( "%d%d%d%d", &a, &b, &c, &t );
			--a; --b;
			d[0][a][b] = d[0][b][a] = c;
			d[1][a][b] = d[1][b][a] = t;
		}

		rep( r, 2 ) rep( k, m ) rep( i, m ) rep( j, m )
			d[r][i][j] = std::min( d[r][i][j], d[r][i][k]+d[r][k][j] );

		int k;
		scanf( "%d", &k );

		rep( i, k )
		{
			int p, q, r;
			scanf( "%d%d%d", &p, &q, &r );

			printf( "%d\n", d[r][p-1][q-1] );
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <cstring>

using namespace std;

// 線路の情報の構造体

typedef struct _lnInfo{
	int cost;
	int time;
}lnInfo;

// 問い合わせの構造体

typedef struct _Req{
	int p;
	int q;
	int r;
}Req;


int main(void){
	int n, m, k, i, j, a, b, cost, time, ans;
	lnInfo st[100][100];
	Req rq;
	while(1){
		scanf("%d%d",&n,&m);
		if(n == 0 && m == 0){
			break;
		}
		memset(st,-1,sizeof(st));
		for(i = 0;i < n;i++){
			scanf("%d%d%d%d",&a,&b,&cost,&time);
			st[a - 1][b - 1].cost = st[b - 1][a - 1].cost = cost;
			st[a - 1][b - 1].time = st[b - 1][a - 1].time = time;
			
		}
		for(i = 0;i < m;i++){
			for(j = 0;j < m;j++){
				for(k = 0;k < m;k++){
					if(st[j][i].cost != -1 && st[i][k].cost != -1 && (st[j][k].cost == -1 || st[j][k].cost > st[j][i].cost + st[i][k].cost)){
						st[j][k].cost = st[j][i].cost + st[i][k].cost;
					}
					if(st[j][i].time != -1 && st[i][k].time != -1 && (st[j][k].time == -1 || st[j][k].time > st[j][i].time + st[i][k].time)){
						st[j][k].time = st[j][i].time + st[i][k].time;
					}
				}
			}
		}
		scanf("%d",&k);
		for(i = 0;i < k;i++){
			scanf("%d%d%d",&rq.p,&rq.q,&rq.r);
			if(rq.r == 0){
				ans = st[rq.p - 1][rq.q - 1].cost;
			}else{
				ans = st[rq.p - 1][rq.q - 1].time;
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Line{
        int a;
        int b;
        int cost;
        int time;
};
int Min(int x,int y){
        return x<y?x:y;
}
#define Max 2000000000
int main(){
        int N,M;
        Line lines[3000];
        cin >> N >>M;
        for(int i=0 ; i<N ; i++ ){
                cin >> lines[i].a >> lines[i].b ;
                cin >> lines[i].cost >> lines[i].time;
        }
        int K;
        cin >> K;
        for(int i=0 ; i<K ; i++ ){
                int to,from,r;
                cin >> from >> to>>r;
                queue <int> circle;
                circle.push(from);
                int dp[3000];
                for(int i=0 ; i<=M ; i++ )
                        dp[i]=Max;
                dp[from]=0;
                while(circle.size()!=0){
                        int now=circle.front();
                        for(int i=0 ; i<N ; i++ ){
                                if(now==lines[i].a){
                                        int next=lines[i].b;
                                        if(!r)
                                                dp[next]=Min(dp[next],dp[now]+lines[i].cost);
                                        else
                                                dp[next]=Min(dp[next],dp[now]+lines[i].time);
                                        circle.push(lines[i].b);
                                }
                        }
                        circle.pop();
                }
                cout << dp[to] << endl;
        }
        return 0;
}