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]
					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 <cstring>
#include <vector>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define MEM(arr,i) memset(arr,i,sizeof(arr))
#define pb push_back
using namespace std;

struct edge {
    int to,from,money,time;
};

vector<edge> E;
int V;

int d[111];

int wf(int p, int q, bool r){
    FOR(i,0,111)d[i]=100000000;
    d[p]=0;
    while(1){
        bool update=0;
        FOR(i,0,E.size()){
            edge e=E[i];
            int cost=r?e.time:e.money;
            if(d[e.to]>cost+d[e.from]){
                d[e.to]=cost+d[e.from];
                update=1;
            }
        }
        if(!update)break;
    }
    return d[q];
}

int main(){
    int n;
    while(1){
        E.clear();
        cin>>n>>V;
        if(n==0&&V==0) break;
        FOR(i,0,n){
            int a,b,m,t;
            cin>>a>>b>>m>>t;
            E.pb({a,b,m,t});
            E.pb({b,a,m,t});
        }
        int k;cin>>k;
        FOR(i,0,k){
            int p,q,r;
            cin>>p>>q>>r;
            cout<<wf(p,q,r)<<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] = 3000;
				else
					ms[k][j][i] = am[k][j][i];
			}
	
	FOR(t, 0, 2)
		FOR(k, 1, m+1)
			FOR(j, 1, m+1)
				FOR(i, 1, m+1)
				{
					ms[t][j][i] = min(ms[t][j][i], ms[t][j][k] + ms[t][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;
		}
		
		solve();
		
		cin >> r;
		
		FOR(i,0,r)
		{
			int a, b, t;
			cin >> a >> b >> t;
			cout << ms[t][a][b] << endl;
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#define INF 100000000
typedef long long int lli;
using namespace std;
int dc[101][101];
int dt[101][101];

int main(){
	int n,m;
	while(cin>>n>>m&&n&&m){
		for(int i=0; i<101; ++i){
			for(int j=0; j<101; ++j){
				if(i==j){
					dc[i][j]=0;
					dt[i][j]=0;
				} else {
					dc[i][j]=INF;
					dt[i][j]=INF;
				}
			}
		}
		for(int i=0; i<n; ++i){
			int a,b,cost,time;
			cin >> a >> b >> cost >> time;
			dc[a][b]=cost;
			dc[b][a]=cost;
			dt[a][b]=time;
			dt[b][a]=time;
		}
		for(int k=1; k<=m; ++k){
			for(int i=1; i<=m; ++i){
				for(int j=1; j<=m; ++j){
					dc[i][j]=min(dc[i][j],dc[i][k]+dc[k][j]);
					dt[i][j]=min(dt[i][j],dt[i][k]+dt[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 << dc[p][q] << "\n";
			} else {
				cout << dt[p][q] << "\n";
			}
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define MAX 1000000
int main(){
	int n, m, a, b, cost, ti, k, p, q, r, point = 0;
	int cost_[101][101], time_[101][101];
	while ( cin >> n >> m && (n != 0 && m != 0)){
		for (int i = 0; i < m; i++)
		for (int j = 0; j < m; j++) { cost_[i][j] = time_[i][j] = MAX; }
		for (int i = 0; i < n; i++){
			cin >> a >> b >> cost >> ti;
			cost_[a-1][b-1] = cost;
			time_[a-1][b-1] = ti;
		}
		for (int i = 0; i < m; i++){
			for (int j = 0; j < m; j++){
				for (int o = 0; o < m; o++){
					if ((cost_[j][i] != MAX && cost_[i][o] != MAX) &&
						(time_[j][i] != MAX && time_[i][o] != MAX)){
						cost_[j][o] = min(cost_[j][o], cost_[j][i] + cost_[i][o]);
						time_[j][o] = min(time_[j][o], time_[j][i] + time_[i][o]);
					}
				}
			}
		}cin >> k;
		for (int i = 0; i < k; i++){
			cin >> p >> q >> r;
			if (r != 1) cout << cost_[p-1][q-1] << endl;
			else cout << time_[p-1][q-1] << endl;
		}
	}
	return 0;
}