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 <algorithm>
using namespace std;

int N, M, K;
int stac[110][110], stat[110][110];
static const int INF = 100000000;

int main() {
	for(;;) {
		cin >> N >> M;
		if(!N && !M) {
			break;
		}
		for(int i = 0; i < 110; i++) {
			for(int j = 0; j < 110; j++) {
				stac[i][j] = stat[i][j] = INF;
			}
		}

		for(int i = 0; i < N; i++) {
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			stac[a][b] = stac[b][a] = cost;
			stat[a][b] = stat[b][a] = time;
		}

		for(int k = 1; k <= M; k++) {
			for(int i = 1; i <= M; i++) {
				for(int j = 1; j <= M; j++) {
					stac[i][j] = min(stac[i][j], stac[i][k] + stac[k][j]);
					stat[i][j] = min(stat[i][j], stat[i][k] + stat[k][j]);
				}
			}
		}

		cin >> K;
		for(int i = 0; i < K; i++) {
			int p, q, r;
			cin >> p >> q >> r;
			if(!r) {
				cout << stac[p][q] << endl;
			} else {
				cout << stat[p][q] << endl;
			}
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <map>

using namespace std;

typedef pair<int, int> pii;

int c[101][101] = { { 0 } };
int t[101][101] = { { 0 } };
int n, m;

int calculate(int mat[101][101], int from, int to){
	map<int, int> locations;
	for(int i = 1; i <= m; i++){
		if(mat[from][i] != 0){
			locations.insert(pii(mat[from][i], i));
		}
	}
	while(!locations.empty()){
		map<int, int>::iterator it = locations.begin();
		int d = (*it).first, f = (*it).second;
		if(f == to){ break; }
		for(int i = 1; i <= m; i++){
			if(mat[f][i] != 0 && (d + mat[f][i] < mat[from][i] || mat[from][i] == 0)){
				int dist = d + mat[f][i];
				mat[from][i] = mat[i][from] = dist;
				locations.insert(pii(dist, i));
			}
		}
		locations.erase(it);
	}
	return mat[from][to];
}

int main(){
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++){
		int a, b, cost, time;
		scanf("%d%d%d%d", &a, &b, &cost, &time);
		c[a][b] = c[b][a] = cost;
		t[a][b] = t[b][a] = time;
	}
	int k;
	scanf("%d", &k);
	while(k--){
		int p, q, r;
		scanf("%d%d%d", &p, &q, &r);
		printf("%d\n", calculate((r == 0) ? c : t, p, q));
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#define int long long
using namespace std;

signed main() {
	int a, b;
	while (cin >>b>>a, a | b) {
		vector<vector<int>>cost(a, vector<int>(a, 2147483647));
		vector<vector<int>>time(a, vector<int>(a, 2147483647)); 
		for (int c = 0; c < b; c++) {
			int d, e, f, g;
			cin >> d >> e >> f >> g;
			d--; e--;
			cost[d][e] = cost[e][d] = f;
			time[d][e] = time[e][d] = g;
		}
		for (auto j : cost) {
			for (int i : j)cout << i << " ";
			cout << endl;
		}
		for (int h = 0; h < a; h++) {
			for (int i = 0; i < a; i++) {
				for (int j = 0; j < a; j++) {
					if (i == j)cost[i][j] = time[i][j] = 0;
					else {
						cost[i][j] = (cost[i][j] < cost[i][h] + cost[h][j]) ? (cost[i][j]) : (cost[i][h] + cost[h][j]);
						time[i][j] = (time[i][j] < time[i][h] + time[h][j]) ? (time[i][j]) : (time[i][h] + time[h][j]);
					}
				}
			}
		}
		for (auto j : cost) {
			for (int i : j)cout << i << " ";
			cout << endl;
		}
		int l;
		cin >> l;
		for (int r = 0; r < l; r++) {
			int x, y, z;
			cin >> x >> y >> z;
			x--; y--;
			if (z) { cout << time[x][y] << endl; }
			else { cout << cost[x][y] << endl; }
		}
	}
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;
  
int min(int x, int y){
	if(x<y){
		return x;
	}else{
		return y;
	}
}

int cost(int x,int y,int n,int c[][101]){
	int i,j,k,d[101],s[101];
	for(i=0;i<=100;i++){
		d[i]=2000;
		s[i]=0;
	}
	for(i=1;i<=n;i++){
		s[i]=1;
	}
	d[x]=0;
	for(i=1;i<=n;i++){
		k=0;
		for(j=1;j<=n;j++){
			if(s[j]==1){
				if(k==0){
					k=j;
				}else if(d[j]<d[k]){
					k=j;
				}
			}
		}
		for(j=1;j<=n;j++){
			if(c[k][j]!=0){
				d[j]=min(d[j],d[k]+c[k][j]);
			}
		}
		s[k]=0;
	}
	return d[y];
}
int time(int x,int y,int n,int t[][101]){
	int i,j,k,d[101],s[101];
	for(i=0;i<=100;i++){
		d[i]=2000;
		s[i]=0;
	}
	for(i=1;i<=n;i++){
		s[i]=1;
	}
	d[x]=0;
	for(i=1;i<=n;i++){
		k=0;
		for(j=1;j<=n;j++){
			if(s[j]==1){
				if(k==0){
					k=j;
				}else if(d[j]<d[k]){
					k=j;
				}
			}
		}
		for(j=1;j<=n;j++){
			if(t[k][j]!=0){
				d[j]=min(d[j],d[k]+t[k][j]);
			}
		}
		s[k]=0;
	}

	return d[y];
}

int main(){
	while(1){
		int i,j,a,b,k,n,m,p,q,r,c[101][101],t[101][101];
		for(i=0;i<=100;i++){
			for(j=0;j<=100;j++){
				c[i][j]=0;
				t[i][j]=0;
			}
		}
		cin>>n>>m;
		if(n==0&&m==0){break;}
		for(i=0;i<n;i++){
			cin>>a>>b;
			cin>>c[a][b]>>t[a][b];
			c[b][a]=c[a][b];
			t[b][a]=t[a][b];
		}
		cin>>k;
		for(i=0;i<k;i++){
			cin>>p>>q>>r;
			if(r==0){
				cout<<cost(p,q,n,c)<<endl;
			}else if(r==1){
				cout<<time(p,q,n,t)<<endl;
			}
		}
	}
    return 0;
}