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
			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
					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<queue>
#include<vector>
#include<map>
using namespace std;
typedef pair<int,int> P;
typedef struct{
	int next;
	int cost[2];
}edge;

int n,m;
vector<edge> mapp[111];
int bfs(int now,int goal,int k);

int main(){
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0) break;
		for(int i=0;i<m+1;i++){
			mapp[i].clear();
		}
		for(int i=0;i<n;i++){
			int a,b,cost,tim;
			cin >> a >> b >> cost >> tim;
			edge x;
			x.next = a; x.cost[0] = cost; x.cost[1] = tim;
			mapp[b].push_back(x);
			x.next = b; x.cost[0] = cost; x.cost[1] = tim;
			mapp[a].push_back(x);
		}
		int k;
		cin >> k;
		int ans;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin >> p >> q >> r;
			if(r == 0) ans = bfs(p,q,r);
			else if(r == 1) ans = bfs(p,q,r);
			cout << ans << endl;
		}
	}
}

int bfs(int now,int goal,int k){
	vector<int> min_cost;
	min_cost.resize(m,-1);
	priority_queue<P, vector<P>,greater<P> > que;
	que.push(P(0,now));
	min_cost[now] = 0;
	while(!que.empty()){
		P p = que.top(); que.pop();
		int no = p.second;
		int co = p.first;
		if(no == goal) return co;
		if(co > min_cost[no]) continue;
		int len=mapp[no].size();
		for(int i=0;i<len;i++){
			edge x = mapp[no][i];
			if(min_cost[x.next] == -1 || co+x.cost[k]<min_cost[x.next]){
				min_cost[x.next] = co + x.cost[k];
				que.push(P(co+x.cost[k],x.next));
			}
		}
	}
	return min_cost[goal];
}

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

const int LEN = 100;
const int INF = 10000;
int time[LEN][LEN];
int cost[LEN][LEN];


int main(void){
	while(1){
		//init
		int rosen,eki,a,b,incost,intime;
		for(int i=0; i<LEN; i++){ 
			for(int j=0; j<LEN; j++)
				time[i][j] = cost[i][j] = INF;
		}
		for(int i=0; i<LEN; i++)
			time[i][i] = cost[i][i] = 0;
	
		//in
		cin >> rosen >> eki; 
		if(rosen==0&&eki==0)break;
		for(int i=0; i<rosen; i++){
			cin >> a >> b >> incost >> intime;
			a--; b--;
			cost[a][b] = cost[b][a] = incost;
			time[a][b] = time[b][a] = intime;
		}

		//calc
		for(int k=0; k<eki; k++){
			for(int i=0; i<eki; i++){
				for(int j=0; j<eki; j++){
					time[i][j] = min(time[i][j],time[i][k]+time[k][j]);
					cost[i][j] = min(cost[i][j],cost[i][k]+cost[k][j]);
				}
			}
		}

		//out
		int toi;
		cin >> toi;
		for(int i=0; i<toi; i++){
			int a,b,r;
			cin >> a >> b >> r;
			a--; b--;
			if(r==0)
				cout << cost[a][b] << endl;
			else
				cout << time[a][b] << endl;
		}
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
#include<algorithm>
#include<string>
#define REP(i,a) for(int  i=0;i<a;i++)
int A[101][101];
int B[101][101];
int main(){
	int a,b=0,c=0,d,n,m;	
	cin>>n>>m;
	while(n!=0||m!=0){
	REP(i,m){
		REP(j,m){
			A[i][j]=1001;
			B[i][j]=1001;
		}
	}
	REP(i,n){
	cin>>a>>b>>c>>d;
	A[a-1][b-1]=c;
	B[a-1][b-1]=d;	
	A[b-1][a-1]=c;
	B[b-1][a-1]=d;
	}
	REP(i,m){
		REP(j,m){
	 		REP(k,m){
		A[i][j]=min(A[i][j],A[i][k]+A[k][j]);
		B[i][j]=min(B[i][j],B[i][k]+B[k][j]);
		A[j][i]=A[i][j];
		B[j][i]=B[i][j];		
			}
		}	
	}
	cin>>n;
	REP(i,n){
	cin>>a>>b>>c;
	if(c==0) cout<<A[a-1][b-1]<<endl;
	else cout<<B[a-1][b-1]<<endl;
	}
	cin>>n>>m;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <ctime>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <functional>
#include <fstream>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
class Timer
{
  private:
    double time_limit;
    //unsigned long long int cycle_per_sec = 2500000000; // Topcoder
    unsigned long long int cycle_per_sec = 2777777777; // AtCoder
    unsigned long long int beginCycle;
    unsigned long long int getCycle()
    {
        unsigned int low, high;
        __asm__ volatile("rdtsc"
                         : "=a"(low), "=d"(high));
        return ((unsigned long long int)low) | ((unsigned long long int)high << 32);
    }

  public:
    Timer(double time_limit) : time_limit(time_limit)
    {
        beginCycle = getCycle();
    }

    double elapsedTime()
    {

        return (double)(getCycle() - beginCycle) / cycle_per_sec;
    }
    bool isTLE()
    {
        return elapsedTime() >= time_limit;
    }
};

Timer gtimer(2.85);

int main()
{
    int m, n;
    while (cin >> m >> n && m)
    {
        int wf[2][110][110] = {};
        rep(i, 2) rep(j, 110) rep(k, 110)
            wf[i][j][k] = j == k ? 0 : 1e9;

        for (int i = 0; i < m; i++)
        {
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            --a, --b;
            wf[0][a][b] = min(wf[0][a][b], c);
            wf[1][a][b] = min(wf[1][a][b], t);
            wf[0][b][a] = min(wf[0][a][b], c);
            wf[1][b][a] = min(wf[1][a][b], t);
        }
        rep(i, 2)
        {
            rep(j, 110) rep(k, 110) rep(l, 110)
            {
                wf[i][k][l] = min(wf[i][k][j] + wf[i][j][l], wf[i][k][l]);
            }
        }
        int Q;
        cin >> Q;
        while (Q--)
        {
            int a, b, c;
            cin >> a >> b >> c;
            --a, --b;
            cout << wf[c][a][b] << endl;
        }
    }
    std::cout.flush();
    return 0;
}