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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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 <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 k;
        for (cin >> k; k; k--) {
            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;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Line{
        int a;
        int b;
        int cost;
        int time;
};
int Min(int x,int y){
        return x<y?x:y;
}
#define Max 2000000000
int main(){
        int N,M;
        while(true){
                Line lines[3000];
                cin >> N >>M;
                if(N+M==0)      break;
                for(int i=0 ; i<N ; i++ ){
                        cin >> lines[i].a >> lines[i].b ;
                        cin >> lines[i].cost >> lines[i].time;
                }
                int K;
                cin >> K;
                for(int i=0 ; i<K ; i++ ){
                        int to,from,r;
                        cin >> from >> to>>r;
                        queue <int> circle;
                        circle.push(from);
                        int dp[3000];
                        for(int i=0 ; i<=M ; i++ )
                                dp[i]=Max;
                        dp[from]=0;
                        while(circle.size()!=0){
                                int now=circle.front();
                                for(int i=0 ; i<N ; i++ ){
                                        if(now==lines[i].a){
                                                int next=lines[i].b;
                                                if(!r)
                                                        dp[next]=Min(dp[next],dp[now]+lines[i].cost);
                                                else
                                                        dp[next]=Min(dp[next],dp[now]+lines[i].time);
                                                circle.push(lines[i].b);
                                        }
                                }
                                circle.pop();
                        }
                        cout << dp[to] << endl;
                }
        }
        return 0;
}

C++: #include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<sstream>
#include<functional>
#include<map>
#include<set>
#include<cmath>

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

using namespace std;

#define INF 10000

int m,x,y;
int c[100][100],ct[100][100];
int d[100],dt[100];
bool b[100],bt[100];

void mc(int s){
rep(i,m){
d[i]=INF; b[i]=false;
}
d[s]=0;

while(1){
int M=INF;
rep(i,m){
if(!b[i] && d[i]<M){
	M=d[i]; x=i;
}
}

if(M==INF) break;

b[x]=true;
rep(i,m) d[i]=min(d[i],d[x]+c[x][i]);

}
}

void mt(int s){
rep(i,m){
dt[i]=INF; bt[i]=false;
}
dt[s]=0;

while(1){
int M=INF;
rep(i,m){
if(!bt[i] && dt[i]<M){
	M=dt[i]; y=i;
}
}

if(M==INF) break;

bt[y]=true;
rep(i,m) dt[i]=min(dt[i],dt[y]+ct[y][i]);

}
}

int main(){

rep(i,105){
rep(j,105){
	c[i][j]=INF;
	ct[i][j]=INF;
}
}

int n,a,b,cs,tm,k,p,q,r;

while(1){

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

rep(i,n){
cin >> a >> b >> cs >> tm;

c[a-1][b-1]=cs; c[b-1][a-1]=cs;
ct[a-1][b-1]=tm; ct[b-1][a-1]=tm;
}

cin >> k;

rep(i,k){
cin >> p >> q >> r;

if(r==0){
	mc(p-1); cout << d[q-1] << endl;


}

else{
	mt(p-1); cout << dt[q-1] << endl;


}
}


}


	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 28;
int c[200][200];
int t[200][200];
int cost[200][200];
int tim[200][200];

int main(){
  int n, m, K;
  int p, q, r;
  while(cin >> n >> m && (n || m)){
    fill(&cost[0][0], &cost[199][199] + 1, INF);
    fill(&tim[0][0], &tim[199][199] + 1, INF);
    fill(&c[0][0], &c[199][199] + 1, INF);
    fill(&t[0][0], &t[199][199] + 1, INF);
    for(int i = 1; i <= m; i++){
      cost[i][i] = tim[i][i] = 0;
    }
    for(int i = 0; i < n; i++){
      int a, b;
      cin >> a >> b;
      cin >> c[a][b] >> t[a][b];
      c[b][a] = c[a][b];
      t[b][a] = t[a][b];
    }
    for(int k = 1; k <= m; k++){
      for(int i = 1; i <= m; i++){
	for(int j = 1; j <= m; j++){
	  if(cost[i][j] > cost[i][k] + c[k][j]){
	    cost[i][j] = cost[i][k] + c[k][j];
	  }
	  if(tim[i][j] > tim[i][k] + t[k][j]){
	    tim[i][j] = tim[i][k] + t[k][j];
	  }
	}
      }
    }
    cin >> K;    
    for(int i = 0; i < K; i++){
      cin >> p >> q >> r;
      if(!r){
	cout << cost[p][q] << endl;
      }else{
	cout << tim[p][q] << endl;
      }
    }
  }
  return 0;
}