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 <stdio.h>
#include <limits.h>

const int INF = INT_MAX;

int main(void)
{
	int n, m, a, b, k, t;
	int tcost[105][105], mcost[105][105], node[105], used[105];
	int i, j, min;
	
	while (1){
		scanf("%d %d", &n, &m);
		
		if (n == 0 && m == 0){
			break;
		}
		
		for (i = 0; i < m; i++){
			for (j = 0; j < m; j++){
				mcost[i][j] = INF;
				tcost[i][j] = INF;
			}
		}
		
		for (i = 0; i < n; i++){
			scanf("%d %d", &a, &b);
			a--; b--;
			scanf("%d %d", &mcost[a][b], &tcost[a][b]);
			mcost[b][a] = mcost[a][b];
			tcost[b][a] = tcost[a][b]; 
		}
		
		scanf("%d", &k);
		
		for (i = 0; i < k; i++){
			scanf("%d %d %d", &a, &b, &t);
			a--; b--;
			for (j = 0; j < m; j++){
				node[j] = INF;
				used[j] = 0;
			}
			
			node[a] = 0;
			while (1){
				used[a] = 1;
				min = INF;
				
				for (j = 0; j < m; j++){
					if (t == 1){
						if (used[j] == 0 && tcost[a][j] != INF && node[j] > node[a] + tcost[a][j]){
							node[j] = node[a] + tcost[a][j];
						}
						if (used[j] == 0 && min > node[j]){
							min = j;
						}
					}
					else{
						if (used[j] == 0 && tcost[a][j] != INF && node[j] > node[a] + mcost[a][j]){
							node[j] = node[a] + mcost[a][j];
						}
						if (used[j] == 0 && min > node[j]){
							min = j;
						}
					}
				}
				a = min;
				if (a == b){
					break;
				}
			}
			printf("%d\n", node[b]);
		}
	}
	return (0);
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(cin >> n >> m,n){
		int a,b,c,d;
		int co[n][n];
		int ti[n][n];
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++){
				co[i][j] = 100000000;
				ti[i][j] = 100000000;
			}
		for(int i = 0; i < n; i++){
			cin >> a >> b >> c >> d;
			a--; b--;
			co[a][b] = c;
			co[b][a] = c;
			ti[a][b] = d;
			ti[b][a] = d;
		}
		vector <int> used;
		vector <int> dist;
		int k,p,q,r;
		cin >> k;
		for(int i = 0; i < k; i++){
			cin >> p >> q >> r;
			p--; q--;
			used = vector <int> (n,100000000);
			dist = vector <int> (n,100000000);
			dist[p] = 0;
			while(1){
				int va = -1;
				for(int j = 0; j < n; j++)
					if(used[j] == 100000000 && (va == -1 || dist[va] > dist[j]))
						va = j;
				if(va == -1)
					break;
				used[va] = 1;
				for(int j = 0; j < n; j++)
					dist[j] = min(dist[j] , (r == 0 ? co[j][va] : ti[j][va]) + dist[va]);
			}
			cout << dist[q] << endl;
		}
	}
	return 0;
}

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

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

typedef pair<int,int> pii;
const int INF=(int)1e9, MAXN=128;
int C[2][MAXN][MAXN], visited[MAXN], N, M;

int dk(int p,int q,int r)
{
        priority_queue<pii,vector<pii>,greater<pii> > Q;
        Q.push(pii(0,p));

        while(!Q.empty())
        {
                const int cc=Q.top().first, pp=Q.top().second; Q.pop();
                if(pp==q){ return cc; } else { visited[pp]=true; }

                rep(i,M) if(!visited[i]){ Q.push(pii(cc+C[r][pp][i],i)); }
        }
        return 0;
}

int main()
{
        int a,b,c,d;
        cin>>N>>M;
        rep(i,M) rep(j,N) C[0][i][j]=C[1][i][j]=(i==j?0:INF);
        rep(i,N){ cin>>a>>b>>c>>d; --a; --b; C[0][a][b]=C[0][b][a]=c; C[1][a][b]=C[1][b][a]=d; }

        cin>>d;
        rep(i,d){ fill(visited,visited+N,0); cin>>a>>b>>c; cout<<dk(a-1,b-1,c)<<endl; }
        return 0;
}

C++: #include <iostream>

using namespace std;

const int MAX_N = 110;
const int INF = 10000;
int d_cost[MAX_N][MAX_N];
int d_time[MAX_N][MAX_N];
int n, m;

void warshall_floyd(int d[MAX_N][MAX_N])
{
    for (int k = 0; k < m; k++)
        for (int i = 0; i < m; i++)
            for (int j = 0; j < m; j++)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main()
{
    while (cin >> n >> m, n || m) {
        int a, b, cost, time;
        int k, p, q, r;

        // 初期化
        for (int i = 0; i < m; i++)
            for (int j = 0; j < m; j++) {
                if (i == j) 
                    d_cost[i][j] = d_time[i][j] = 0;
                else
                    d_cost[i][j] = d_time[i][j] = INF;
            }

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

        warshall_floyd(d_cost);
        warshall_floyd(d_time);

        cin >> k;
        for (int i = 0; i < k; i++) {
            cin >> p >> q >> r;

            p--, q--;
            if (r == 0)
                cout << d_cost[p][q] << endl;
            else
                cout << d_time[p][q] << endl;
        }
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
const int INF=(1<<21);
main(){
  int n,m,h;
  int from,to,cost,time;
  int roadmoney[101][101];
  int roadtime[101][101];
  while(1){
    cin>>n>>m;
    if(n==0&&m==0)break;
    for(int i=0;i<=m;i++){
      for(int j=0;j<=m;j++){
	roadmoney[i][j]=INF;
	roadtime[i][j]=INF;
      }
    }
    for(int i=0;i<n;i++){
      cin>>from>>to>>cost>>time;
      roadmoney[from][to]=roadmoney[to][from]=cost;
      roadtime[from][to]=roadtime[to][from]=time;
    }
    for(int i=1;i<=m;i++){
      for(int j=1;j<=m;j++){
	for(int k=1;k<=m;k++){
	  roadmoney[j][k]=min(roadmoney[j][k],roadmoney[j][i]+roadmoney[i][k]);
	  roadtime[j][k]=min(roadtime[j][k],roadtime[j][i]+roadtime[i][k]);
	}
      }
    }
    cin>>h;
    int q,p,r;
    for(int i=0;i<h;i++){
      cin>>q>>p>>r;
      if(r==0){
	cout<<roadmoney[q][p]<<endl;
      }
      else{
	cout<<roadtime[q][p]<<endl;
      }
    }
  }
}