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 <cstdio>
#include <iostream>
#include <algorithm>

#define MAX_STATIONS 100+1
#define INF 1<<15

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



using namespace std;

class Stations{
private:
    int time[MAX_STATIONS][MAX_STATIONS];
    int cost[MAX_STATIONS][MAX_STATIONS];
public:
    void solve(int a[MAX_STATIONS][MAX_STATIONS], int r){
        //wayは文系をあらわす
        rep(way,MAX_STATIONS){
            rep(to,MAX_STATIONS){
                rep(from,MAX_STATIONS){
                    a[from][to] = min(a[from][to], a[from][way] + a[way][to]);
                    a[to][from] = a[from][to];
                }
            }
        }
        if(r==1){
            rep(i,MAX_STATIONS){
                rep(k,MAX_STATIONS){
                    time[k][i] = a[k][i];
                }
            }
        }
        else{
            rep(i,MAX_STATIONS){
                rep(k,MAX_STATIONS){
                    cost[k][i] = a[k][i];
                }
            }
        }
    }

    int getTime(int from, int to){
        return time[from][to];
    }

    int getCost(int from, int to){
        return cost[from][to];
    }
};

int main(void){
    int n,m;
    int t[MAX_STATIONS][MAX_STATIONS], c[MAX_STATIONS][MAX_STATIONS];
    while(cin >> n >> m,n+m){
        rep(i,MAX_STATIONS){
            rep(k,MAX_STATIONS){
                t[i][k] = INF;
                c[i][k] = INF;
            }
        }

        rep(i,n){
            int from,to,cost,time;
            cin >> from >> to >> cost >> time;
            t[from][to] = t[to][from] = time;
            c[from][to] = c[to][from] = cost;
        }

        Stations station;
        station.solve(c,0);
        station.solve(t,1);

        int q;
        cin >> q;

        rep(i,q){
            int from,to,r;
            cin >> from >> to >> r;
            if(r==1){
                cout << station.getTime(from,to) << endl;
            }else{
                cout << station.getCost(from,to) << endl;
            }
        }
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<set>

using namespace std;

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

int main(){
	while(1){
		int m,n;
		cin>>m>>n;
		if(m==0)break;
		
		int dp[2][111][111];
		
		rep(p,2){
			rep(i,111){
				rep(j,111){
					dp[p][i][j]=1000000000;
				}
			}
			rep(i,111)dp[p][i][i]=0;
		}
		
		rep(i,m){
			int a,b,c,t;
			cin>>a>>b>>c>>t;
			
			a--;b--;
			
			dp[0][a][b]=c;
			dp[0][b][a]=c;
			
			dp[1][a][b]=t;
			dp[1][b][a]=t;
		}
		
		rep(p,2){
			rep(k,n){
				rep(i,n){
					rep(j,n){
						dp[p][i][j] = min(dp[p][i][j],dp[p][i][k]+dp[p][k][j]);
					}
				}
			}
		}
		
		int T;
		cin>>T;
		rep(i,T){
			int a,b,c;
			cin>>a>>b>>c;
			printf("%d\n",dp[c][a-1][b-1]);
		}
	}
}

C++: #include <cstdio>
#include <algorithm>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
#define INF (1<<29)
using namespace std;
int C[2][100][100];
int main()
{
	while (true) {
		int n, m; scanf("%d %d", &n, &m);
		if (n == 0 && m == 0) break;
		rep(k, 2) rep(i, 100) {
			rep(j, 100) { C[k][i][j] = INF; }
			C[k][i][i] = 0;
		}
		rep(i, n) {
			int a, b, c, t; scanf("%d%d%d%d", &a, &b, &c, &t);
			a--, b--;
			C[0][a][b] = C[0][b][a] = c;
			C[1][a][b] = C[1][b][a] = t;
		}
		rep(k, m) rep(i, m) rep(j, m) rep(l, 2) {
			C[l][i][j] = min(C[l][i][k] + C[l][k][j], C[l][i][j]);
		}
		int k; scanf("%d", &k);
		rep(i, k) {
			int p, q, r; scanf("%d%d%d", &p, &q, &r);
			printf("%d\n", C[r][p-1][q-1]);
		}
	}
	return 0;
}

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

array<array<int, 110>, 110> costs;
array<array<int, 110>, 110> times;

int main() {
    int n, m;
    while (cin >> n >> m, n || m) {
        for (auto& ar : costs) ar.fill(1000000);
        for (auto& ar : times) ar.fill(1000000);
        for (int i = 0; i < n; i++) {
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            a--; b--;
            costs[a][b] = costs[b][a] = c;
            times[a][b] = times[b][a] = t;
        }
        for (int i = 0; i < n; i++) {
            costs[i][i] = 0;
            times[i][i] = 0;
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    costs[j][k] = min(costs[j][k], costs[j][i] + costs[i][k]);
                    times[j][k] = min(times[j][k], times[j][i] + times[i][k]);
                }
            }
        }
        int n;
        for (cin >> n; n; n--) {
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            if (r == 0) {
                cout << costs[p][q] << endl;
            }
            else {
                cout << times[p][q] << endl;
            }
        }
    }
    return 0;
}