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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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<vector>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<queue>
#include<deque>
#include<iomanip>
#include<sstream>
#include<cmath>
#include<bitset>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(ll i = 1;i <=  n; i++)
#define rreq(i,n) for(ll i = n;i >= 1;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long int ll;
typedef long double ld;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
    std::fill((T*)array, (T*)(array + N), val);
}
const int MOD = 1000000007;
int main(void) {
    int n, m;
    while (cin >> n >> m, n, m) {
        vector<vector<int>> dp1(m, vector<int>(m,MOD)), dp2(m, vector<int>(m, MOD));
        rep(i, n) {
            int a, b, c, t; cin >> a >> b >> c >> t; a--; b--;
            dp1[a][b] = c; dp2[a][b] = t;
            dp1[b][a] = c; dp2[b][a] = t;
        }rep(i, m) rep(j, m) rep(k, m) dp1[j][k] = min(dp1[j][k], dp1[j][i] + dp1[i][k]);
        rep(i, m) rep(j, m) rep(k, m) dp2[j][k] = min(dp2[j][k], dp2[j][i] + dp2[i][k]);
        int k; cin >> k;
        rep(i, k) {
            int a, b, r; cin >> a >> b >> r; a--; b--;
            if (r == 0) {
                cout << dp1[a][b] << endl;
            }
            else {
                cout << dp2[a][b] << endl;
            }
        }
    }
}

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

#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
#define INF 2147483647/2

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

C++: #include <iostream>
#include <algorithm>
#include <climits>

#define INF INT_MAX / 2
#define REP(i, m, n) for(int i = m; i < static_cast<int>(n); ++i)
int main()
{
    int line_num, station_num;
    
    while (std::cin >> line_num >> station_num) {
        
        if (line_num == 0 && station_num == 0) break;

        int map[2][101][101];
        
        REP(i, 0, 2) REP(j, 0, 101) REP(k, 0, 101) map[i][j][k] = INF;
        
        REP(i, 0, line_num) {
            int a, b, cost, time;
            std::cin >> a >> b >> cost >> time;
            --a;
            --b;
            map[0][a][b] = map[0][b][a] = cost;
            map[1][a][b] = map[1][b][a] = time;
        }
        
        REP(k, 0, station_num) {
            REP(i, 0, station_num) {
                REP(j, 0, station_num) {
                    REP(l, 0, 2) {
                        map[l][i][j] = std::min(map[l][i][j], map[l][i][k] +
                                                map[l][k][j]);
                    }
                }
            }
        }

        int input_num;

        std::cin >> input_num;

        REP(i, 0, input_num) {
            int p, q, r;

            std::cin >> p >> q >> r;
            --p;
            --q;
            std::cout << map[r][p][q] << std::endl;
        }
    
    }
    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include <map>
#include<stdio.h>
#include<algorithm>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <iostream>
#include <functional>
#include <time.h>
using namespace std;

const int INF = 100 * 1000 + 2;
const int r_c = 0;
const int r_t = 1;
const int MAX_V = 100;
const int MAX_E = 6000;

int n, m, a, b, c, t;
int k, p, q, r;
int wf[MAX_V][2] = { 0 };

struct edge { int from, to, ct[2]; };
edge edg[MAX_E];

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 0; i <= m * 2; ++i) {
		scanf("%d %d %d %d", &a, &b, &c, &t);
		edg[i].from = a - 1; edg[i].to = b - 1; edg[i].ct[r_c] = c; edg[i].ct[r_t] = t;
		++i;
		edg[i].from = b - 1; edg[i].to = a - 1; edg[i].ct[r_c] = c; edg[i].ct[r_t] = t;
	}
	scanf("%d", &k);
	for (int i = 0; i < k; ++i) {
		scanf("%d %d %d", &p, &q, &r);
		for (int i = 0; i < n; ++i) {
			wf[i][r] = INF;
		}
		wf[p - 1][r] = 0;
		while (1) {
			bool bo = false;
			for (int i = 0; i < m * 2; ++i) {
				edge e = edg[i];
				if (wf[e.from][r] != INF && wf[e.to][r] > wf[e.from][r] + e.ct[r]) {
					wf[e.to][r] = wf[e.from][r] + e.ct[r];
					bo = true;
				}
			}
			if (!bo) break;
		}
		if (wf[q - 1][r] == INF) printf("INF\n");
		else printf("%d\n", wf[q - 1][r]);
	}
}