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>

using namespace std;

int i, j, k, p, q, r, n, m, a, b, c, cost[101], t, dc[200][200], dt[200][200];

int dic(const int d[200][200]) {
  cost[p] = 0;
  int min, f[200] = {};
  for(i = 0; i < m; i++) {
    f[p] = 1;
    for(j = 1; j <= m; j++) {
      if(f[j]) continue;
      if(cost[j] > cost[p] + d[p][j]) cost[j]  = cost[p] + d[p][j];
    }
    min = 200000000;
    for(j = 1; j <= m; j++ ) {
      if(f[j]) continue;
      if(min > cost[j]) min = cost[p = j];
    }
  }
  return cost[q];
}


int main() {
  for(i = 0; i < 200; i++)
    for(j = 0; j < 200; j++) {
      dc[i][j] = 100000000;
      dt[i][j] = 100000000;
    }
  
  cin >> n >> m;
  
  while(n--) {
    cin >> a >> b >> c >> t;
    dc[a][b] = dc[b][a] = c;
    dt[a][b] = dt[b][a] = t;
  }
  
  cin >> k;
  
  while(cin >> p  >> q, p != 0 || q != 0 ) {
    cin >> r;
    for(i = 0; i <= m; i++) cost[i] = 100000000;
    if(r) cout << dic(dt) << endl;
    else cout << dic(dc) << endl;
  }
  
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 1e+8
struct Data {
	int cost,time;
	Data(){}
	Data(int cost, int time):cost(cost),time(time){}
};
int main() {
	int n,m;
	while(scanf("%d%d",&n,&m),(n|m)) {
		Data d[101][101];
		for(int i = 0; i < 101; i++) {
			for(int j = 0; j < 101; j++) {
				d[i][j] = Data(INF,INF);
			}
		}
		for(int i = 0; i < n; i++) {
			int a,b,cost,time;
			scanf("%d%d%d%d",&a,&b,&cost,&time);
			a--;b--;
			d[a][b] = Data(cost,time);
			d[b][a] = Data(cost,time);
		}
		for(int k = 0; k < m; k++) {
			for(int i = 0; i < m; i++) {
				for(int j = 0; j < m; j++) {
					d[i][j].cost = min(d[i][j].cost,d[i][k].cost + d[k][j].cost);		
					d[j][i].cost = min(d[j][i].cost,d[j][k].cost + d[k][i].cost);		
					d[i][j].time = min(d[i][j].time,d[i][k].time + d[k][j].time);		
					d[j][i].time = min(d[j][i].time,d[j][k].time + d[k][i].time);		
				}
			}
		}
		int k;
		scanf("%d",&k);
		for(int i = 0; i < k; i++) {
			int p,q,r;
			scanf("%d%d%d",&p,&q,&r);
			p--;q--;
			if(r == 0)
				printf("%d\n",d[p][q].cost);
			else 
				printf("%d\n",d[p][q].time);
		}
	}
	return 0;
}

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];
					}
					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 s,g,r;
		cin>>s>>g>>r;
		if(r==1){
			cout<<T[s-1][g-1]<<endl;
		}else{
			cout<<C[s-1][g-1]<<endl;
		}
	}
}
	return 0;
}

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;
		int p,q,r;

		for(int i=0;i<k;i++){
			cin>>p>>q>>r;
			if(r==0){
				cout<<C[p-1][q-1]<<endl;
			}
			if(r==1){
				cout<<T[p-1][q-1]<<endl;
			}
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;

int map[2][101][101];
// 0...cost 1...time
int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) && n && m){
    rep(i, 101) rep(j, 101) rep(k, 2) map[k][j][i] = 999999999;
    int a, b, c, t;
    rep(i, n){
      scanf("%d%d%d%d", &a, &b, &c, &t);
      a--;b--;
      map[0][a][b] = map[0][b][a] = c;
      map[1][a][b] = map[1][b][a] = t;
    }
    rep(k, m) rep(i, m) rep(j, m) rep(l, 2) map[l][i][j] = min(map[l][i][j], map[l][i][k] + map[l][k][j]);

    int k, p, q, r;
    scanf("%d", &k);
    while(k--){
      scanf("%d%d%d", &p, &q, &r);
      p--;q--;
      printf("%d\n", map[r][p][q]);
    }
  }
  return 0;
}