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++: #define _USE_MATH_DEFINES
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define INF 10000000
using namespace std;
typedef long long ll;

int cost[110][110];
int time[110][110];

int main(void)
{
	while (1) {
		int n, m;
		cin >> n >> m;

		if (n == 0 && m == 0)break;

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				if (i == j) {
					cost[i][j] = 0;
					time[i][j] = 0;
				}
				else {
					cost[i][j] = INF;
					time[i][j] = INF;
				}
			}
		}

		for (int i = 0; i < n; i++) {
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a - 1][b - 1] = cost[b - 1][a - 1] = c;
			time[a - 1][b - 1] = time[b - 1][a - 1] = t;
		}

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

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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)

const int INF = 1<<28, MAXM = 101, COST = 0, TIME = 1;

int cost[2][MAXM][MAXM];

int main()
{
    int n, m, a, b, c, t;
    while(cin >> n >> m)
    {
        REP(2)rep(j,m)rep(k,m)cost[i][j][k] = INF;
        //fill(cost, sizeof(cost), INF);
        REP(n)
        {
            cin >> a >> b >> c >> t;
            a--; b--;
            cost[TIME][a][b] = cost[TIME][b][a] = t;
            cost[COST][a][b] = cost[COST][b][a] = c;
        }

        int k;
        cin >> k;
        int p, q, r;
        rep(s,k)
        {
            cin >> p >> q >> r; p--; q--;

            //rep(i,m)rep(j,m) d[i][j] = cost[r][i][j];

            rep(l,m)rep(i,m)rep(j,m)
            {
                cost[TIME][i][j] = min(cost[TIME][i][j], cost[TIME][i][l] + cost[TIME][l][j]);
                cost[COST][i][j] = min(cost[COST][i][j], cost[COST][i][l] + cost[COST][l][j]);
            }
            cout << cost[r][p][q] << endl;

        }
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 100000000;
const int MAX_N = 100;
int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0)break;
		int t[MAX_N + 1][MAX_N + 1];
		int yen[MAX_N + 1][MAX_N + 1];
		for (int i = 0; i < MAX_N; i++) {
			for (int j = 0; j < MAX_N; j++) {
				if (i == j) t[i][j] = 0, yen[i][j] = 0;
				else t[i][j] = INF, yen[i][j] = INF;
			}
		}
		for (int i = 0; i < n; i++) {
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			a--, b--;
			t[a][b] = time, t[b][a] = time;
			yen[a][b] = cost, yen[b][a] = cost;

		}
		for (int k = 0; k < m; k++) {
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < m; j++) {
					t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
					yen[i][j] = min(yen[i][j], yen[i][k] + yen[k][j]);
				}
			}
		}
		int k;
		cin >> k;
		for (int i = 0; i < k; i++) {
			int p, q, r;
			cin >> p >> q >> r;
			if (r == 0) cout << yen[p-1][q-1] << endl;
			else cout << t[p-1][q-1] << endl;
		}
	}
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#define INF 30100
#define rep(i,n) for(int i=0;i<n;i++)
#define foreach(r,p,it) for(r::iteartor it=p.begin();it!=p.end();++it)
using namespace std;
struct E
{
    unsigned int a,b,c;
    E(){};
    E(int a, int b,int c):a(a),b(b),c(c){};
};
E cs[610];
E ts[610];

unsigned int d[110];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)break;
        
        int a,b,c,t;
        rep(i,n)
        {
            cin>>a>>b>>c>>t;
            cs[i*2] = E(a,b,c);
            cs[i*2+1] = E(b,a,c);
            
            ts[i*2] = E(a,b,t);
            ts[i*2+1] = E(b,a,t);

        }
        int p,q,r,k;
        cin >> k;
        rep(i,k)
        {
            cin >> p >> q >> r;
            memset(d,INF,sizeof(d));
            d[p] = 0;
            bool update = false;
            if(r == 0)
            {
                while(1)
                {
                    update = false;
                    rep(j,n*2)
                    {
                        if(d[ cs[j].b] != INF && d[cs[j].b] > d[cs[j].a] + cs[j].c )
                        {
                            d[cs[j].b] =  d[cs[j].a] + cs[j].c;
                            update = true;
                        }


                    }
                    if(!update) break;
                }
                cout << d[q] << endl;
            }
            else
            {
                while(1)
                {
                    update = false;
                    rep(j,n*2)
                    {
                        if(d[ ts[j].b] != INF && d[ts[j].b] > d[ts[j].a] + ts[j].c)
                        {
                            d[ts[j].b] =  d[ts[j].a] + ts[j].c;
                            update = true;
                        }
                    }
                    if(!update) break;
                }
                cout << d[q] << endl;
            }
        }
    }
    return 0;
}