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 <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;
typedef vector<P> vp;
typedef vector<vp> mp;

int N, M;

int solve(mp& m, int s, int t) {

    priority_queue<P, vector<P>, greater<P> > que;
    vector<int> dp(M, 1 << 28);
    dp[s] = 0;

    for (int i = 0; i < m[s].size(); i++) {
        que.push(m[s][i]);
        dp[m[s][i].second] = m[s][i].first;
    }

    while (que.size()) {
        P p  = que.top(); que.pop();
        int now = p.second;
        int cost = p.first;

        if (dp[now] < cost) continue;

        for (int i = 0; i < m[now].size(); i++) {
            int cost_ = cost + m[now][i].first;
            int next = m[now][i].second;

            if (dp[next] > cost_) {
                que.push(P(cost_, next));
                dp[next] = cost_;
            }
        }
    }

    return dp[t];
}

int main() {

    while (cin >> N >> M, N || M) {
        mp mc(M), mt(M);

        for (int i = 0; i < N; i++) {
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            a--, b--;
            mc[a].push_back(P(c, b));
            mc[b].push_back(P(c, a));
            mt[a].push_back(P(t, b));
            mt[b].push_back(P(t, a));
        }

        int Q;
        cin >> Q;

        while (Q--) {
            int a, b, t;
            cin >> a >> b >> t;
            a--, b--;

            if (!t) {
                cout << solve(mc, a, b) << endl;
            } else {
                cout << solve(mt, a, b) << endl;
            }
        }
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n,m;
  int a,b,c,d;
  int x;
  int p,q,r;
  int cost[111][111];
  int time[111][111];
  while(1){
    cin >> n >> m;
    if(n==0 && m==0)break;
    for(int i=0;i<111;i++){
      for(int j=0;j<111;j++){
	cost[i][j]=9999999;
	time[i][j]=9999999;
      }
    }
    for(int i=0;i<n;i++){
      cin >> a >> b >> c >> d;
      cost[a-1][b-1]=cost[b-1][a-1]=c;
      time[a-1][b-1]=time[b-1][a-1]=d;
    }
    for(int k=0;k<m;k++){
      for(int i=0;i<m;i++){
	for(int j=0;j<m;j++){
	  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]);
	}
      }
    }
    cin >> x;
    for(int i=0;i<x;i++){
      cin >> p >> q >> r;
      cout << (r==0 ? cost[p-1][q-1] : time[p-1][q-1]) << endl;
    }
  }
}

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

const int INF = 1000000000;

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

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

		int C[100][100];
		int T[100][100];
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++){
				if(i==j){
					C[i][j] = T[i][j] = 0;
				}
				else{
					C[i][j] = T[i][j] = INF;
				}
			}
		}

		for(int i=0; i<n; i++){
			int a, b, c, t;
			cin >> a >> b >> c >> t;

			C[a-1][b-1] = C[b-1][a-1] = c;
			T[a-1][b-1] = T[b-1][a-1] = t;
		}
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(C[i][j]>C[i][k]+C[k][j]){
						C[i][j]=C[i][k]+C[k][j];
					}
				}
			}
		}
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(T[i][j]>T[i][k]+T[k][j]){
						T[i][j]=T[i][k]+T[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<<C[p][q]<<endl;
			if(r==1)
				cout<<T[p][q]<<endl;
		}
		}
	return 0;
}

C++: //2013-5

#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <time.h>
#define mp make_pair
#define INF 1e8
#define fs first
#define sc second
#define rep(x,n) for(int x = 0;x < n;x ++)
#define Rep(x,n) for(int x = 1;x <= n;x ++)
using namespace std;

int d[2][105][105]; //行き先、cost...0 time...1

int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    rep(i,m){
        rep(j,m){
            d[0][i][j]=INF;
            d[1][i][j]=INF;
        }
    }
    rep(i,m){
        d[0][i][i]=0;
        d[1][i][i]=0;
    }

    rep(i,n){
        int a,b,cost,time;
        scanf("%d %d %d %d",&a,&b,&cost,&time);
        a--;b--;
        d[0][a][b]=cost;
        d[0][b][a]=cost;
        d[1][a][b]=time;
        d[1][b][a]=time;
    }
    
    
    rep(i,2){
        rep(j,m){
            rep(k,m){
                rep(l,m){
                    d[i][k][l]=min(d[i][k][l],d[i][k][j]+d[i][j][l]);
                    if(d[i][k][l]>INF)
                        d[i][k][l]=INF;
                }
            }
        }
    }
    
    int k;
    scanf("%d",&k);
    rep(i,k){
        int p,q,r;
        scanf("%d %d %d",&p,&q,&r);
        p--;q--;
        printf("%d\n",d[r][p][q]);
    }
}