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 <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[201][201];
	int time_[201][201];

	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;
		}
	}
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 29)
using namespace std;

struct Data{
    int c, t;
};

void warshallFloyd(Data d[105][105], int m){
    range(k,1,m + 1){
        range(i,1,m + 1){
            range(j,1,m + 1){
                d[i][j].c = min(d[i][j].c, d[i][k].c + d[k][j].c);
                d[i][j].t = min(d[i][j].t, d[i][k].t + d[k][j].t);
            }
        }
    }
}

int main(){
    int n, m;
    Data d[105][105];

    while(cin >> n >> m, n||m){
        rep(i,105) rep(j,105) d[i][j].t = d[i][j].c = INF;
        rep(i,n){
            int a, b, e, f;
            cin >> a >> b >> e >> f;
            d[a][b].c = d[b][a].c = e;
            d[a][b].t = d[b][a].t = f;
        }

        warshallFloyd(d, m);

        int k;
        cin >> k;
        rep(i,k){
            int a, b, c;
            cin >> a >> b >> c;
            if(c) cout << d[a][b].t << endl;
            else cout << d[a][b].c << endl;
        }
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

#define FOR(i,b,n) for(int i=b;i<n;i++)
#define RFOR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))

using namespace std;

static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> paii;

int am[2][500][500] = {0};
int ms[2][500][500] = {0};

int n, m, r;

int solve()
{
	FOR(k,0,2)
		FOR(j,0,m+1)
			FOR(i,0,m+1)
			{
				if( am[k][j][i] != 0 )
					ms[k][j][i] = am[k][j][i];
				else
					ms[k][j][i] = 10000;
			}

	FOR(k, 1, m+1)
		FOR(j, 1, m+1)
			FOR(i, 1, m+1)
			{
				ms[0][j][i] = min(ms[0][j][i], ms[0][j][k] + ms[0][k][i]);
				ms[1][j][i] = min(ms[1][j][i], ms[1][j][k] + ms[1][k][i]);
			}
				
	return 0;
}

int main()
{
	while(cin >> n >> m, (n||m))
	{
		CLR(am);
		CLR(ms);
		
		FOR(i,0,n)
		{
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			
			am[0][a][b] = c;
			am[1][a][b] = t;
			am[0][b][a] = c;
			am[1][b][a] = t;
		}
		
		solve();
		
		cin >> r;
		
		FOR(i,0,r)
		{
			int a, b, t;
			cin >> a >> b >> t;
			cout << ms[t][a][b] << endl;
		}
	}
	
	return 0;
}