Go: // Warshall–Floyd Algorithm

package main

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

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
					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 <iostream>
#include <algorithm>
using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long

int main()
{

	int cost[101][101];
	int time_[101][101];

	while (1)
	{
		int n, m;
		cin >> n >> m; if (n == 0 && m == 0) break;
		REP(i, 101)REP(j, 101)
		{
			if (i == j)
			{
				cost[i][j] = 0;
				time_[i][j] = 0;
			}
			else
			{
				cost[i][j] = INF;
				time_[i][j] = INF;
			}
		}

		REP(i, n)
		{
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			a--; b--;
			cost[a][b] = cost[b][a] = c;
			time_[a][b] = time_[b][a] = t;
		}

		REP(i, m)REP(j, m)REP(k, m)
		{
			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 k;
		cin >> k;
		REP(i, k)
		{
			int p, q, r;
			cin >> p >> q >> r;
			p--; q--;
			if (r == 0) cout << cost[p][q] << endl;
			else cout << time_[p][q] << endl;
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <cctype>

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <algorithm>


using namespace std;

#define Max INT_MAX
#define Inf INT_MIN


#define rep(i, n) for(int i = 0; i < n; i++)
#define repi(i,l,n) for(int (i)=(int)(l);(i)<(int)(n);(i)++)
// s <= x < g ?
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
// 0 <= x < w && 0 <= y < h ?
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))



struct edge {
    int to, cost;
    edge(int t, int c){
        to = t;
        cost = c;
    }
};

//first=最短距離 second=頂点の番号
typedef pair<int, int> P;

int d[100];


//sからの最短距離をdに格納する
void dijkstra(int s, vector<edge> *G ){
    //フロンティア頂点集合
    priority_queue<P, vector<P>, greater<P> > que; //小さい順指定
    fill(d, d+100, Max);

    d[s] = 0;
    que.push( P(0,s) ); //s -> sは距離0

    while(!que.empty() ) {
        P p = que.top(); que.pop(); //フロンティアの中から一番小さい辺を取り出す 
        int v = p.second;
        if( d[v] < p.first) continue;

        rep(i, G[v].size() ) {
            edge e = G[v][i];
            //cout << "i = " << i << endl;
            //cout << "G[v][i].to = " << G[v][i].to<< endl;
            //cout << "G[v][i].cost = " << G[v][i].cost << endl;

            //cout << "v = " << v << endl; 
            //cout << "d[v] = " << d[v] << endl;
            //cout << "e.to = " << e.to << endl;
            //cout << "e.cost = " << e.to << endl;
            //cout << "d[e.to] = " << d[e.to] << endl;
            if( d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                que.push( P(d[e.to], e.to) );
            }
        }
    }
}

vector<edge> costs[100];
vector<edge> times[100];

int main()
{
    int tmp = 0;

    int n,m;
    while(cin >> n >> m){
        if( n == 0 && m == 0 ) { break; }
        rep(i,n){
            int a,b, cost_v,time_v;
            cin >> a >> b >> cost_v >> time_v;
            //edge ee = edge(b,cost_v);
            //cout << "ee.to = "  << ee.to << endl; 
            //cout << "ee.cost = " << ee.cost << endl;
            costs[a-1].push_back( edge(b-1, cost_v) );
            times[a-1].push_back( edge(b-1, time_v) );

            //cout << "_cost = " << cost_v << endl;
            //cout << "_time = " << time_v << endl;
            //cout << "a,b = " << a << " " << b << endl;
            //cout << endl;
        }

        int k;
        cin >> k;

        rep(i,k){
            int p,q,r;
            cin >> p >> q >> r;
            if( r == 0){
                dijkstra(p-1, costs);
            } else {
                dijkstra(p-1, times);
            }
            cout << d[q-1] << endl;
        }
    }
    return 0;


}

C++: #include<iostream>
#include<utility>
#include<queue>
#include<functional>
#include<cstring>
#include<vector>
#include<climits>
using namespace std;
#define FOR(i,n) for(int i=0;i<(int)n;i++)
#define FORI(i,k,n) for(int i=k;i<(int)n;i++)
typedef pair<int,int> pii;

int main()
{
	const int M = 200;
	const int INF=INT_MAX/10;
	int n,m;
	while(cin>>n>>m&&m)
	{
		int cost[M][M];
		int time[M][M];
		FOR(i,M)FOR(j,M)cost[i][j]=time[i][j]=INF;
		FOR(i,n)
		{
			int a,b;
			cin>>a>>b;
			int C,T;
			cin>>C>>T;
			cost[a][b]=cost[b][a]=C;
			time[a][b]=time[b][a]=T;
		}
		
		FORI(k,1,m+1)
		{
			FORI(i,1,m+1)
			{
				FORI(j,1,m+1)
				{
					if(cost[i][k]!=INF&&cost[k][j]!=INF)cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
					if(time[i][k]!=INF&&time[k][j]!=INF)time[i][j]=min(time[i][j],time[i][k]+time[k][j]);
				}
			}
		}
		int k;cin>>k;
		FOR(i,k)
		{
			int p,q,r;
			cin>>p>>q>>r;
			if(r)cout<<time[p][q]<<endl;
			else cout<<cost[p][q]<<endl;
		}
		
	}
}