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]
					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>
#include<vector>
#include<map>
#include<queue>
#define INF (1<<30)
#define COST 0
#define TIME 1

using namespace std;

typedef pair<int, int> p;

vector<p> costs[105], times[105];
int dist[105];
void dijkstra(vector<p> *edge, int s){
	fill(dist, dist + 105, INF);
	dist[s] = 0;
	priority_queue<p, vector<p>, greater<p> > q;
	q.push(p(0, s));
	while(!q.empty()){
		p tmp = q.top();q.pop();
		int d = tmp.first, from = tmp.second;
		if(dist[from] < d)continue;		
		for(int i = 0; i < edge[from].size(); i++){
			int to = edge[from][i].first, c = edge[from][i].second;
			if(dist[to] < dist[from] + c)continue;
			dist[to] = dist[from] + c;
			q.push(p(dist[to], to));
		}
	}
}

int main(){
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < n; i++){
		int a, b, c, t;
		cin >> a >> b >> c >> t;
		costs[a].push_back(p(b, c));
		costs[b].push_back(p(a, c));
		times[a].push_back(p(b, t));
		times[b].push_back(p(a, t));
	}
	int k;
	cin >> k;
	for(int i = 0; i < k; i++){
		int p, q, r;
		cin >> p >> q >> r;
		if(r == COST){
			dijkstra(costs, p);
		}else{
			dijkstra(times, p);
		}
		cout << dist[q] << endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define INF 10000000
using namespace std;
typedef long long ll;

int cost[110][110];
int tim[110][110];

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

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

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				if (i == j) {
					cost[i][j] = 0;
					tim[i][j] = 0;
				}
				else {
					cost[i][j] = INF;
					tim[i][j] = INF;
				}
			}
		}

		for (int i = 0; i < n; i++) {
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a - 1][b - 1] = cost[b - 1][a - 1] = c;
			tim[a - 1][b - 1] = tim[b - 1][a - 1] = t;
		}

		for (int k = 0; k < m; k++) {
			for (int j = 0; j < m; j++) {
				for (int i = 0; i < m; i++) {
					cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					tim[i][j] = min(tim[i][j], tim[i][k] + tim[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 << cost[p - 1][q - 1] << endl;
			else if (r == 1)
				cout << tim[p - 1][q - 1] << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int n,m;
int a[100][100],b[100][100];
main()
{
	while(cin>>m>>n,n)
	{
    	for(int i=0;i<n;i++)for(int j=0;j<n;j++)
    	{
    		if(i==j)continue;
    		a[i][j]=1e9;
    		b[i][j]=1e9;
    	}
    	for(int i=0;i<m;i++)
    	{
    		int s,t,u,v;cin>>s>>t>>u>>v;
    		s--,t--;
    		a[s][t]=min(a[s][t],u);
    		a[t][s]=min(a[t][s],u);
    		b[s][t]=min(b[s][t],v);
    		b[t][s]=min(b[t][s],v);
    	}
    	for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)
    	{
    		a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
    		b[i][j]=min(b[i][j],b[i][k]+b[k][j]);
    	}
    	int k;cin>>k;
    	for(;k--;)
    	{
    		int p,q,r;cin>>p>>q>>r;
    		p--,q--;
    		if(r)cout<<b[p][q]<<endl;
    		else cout<<a[p][q]<<endl;
    	}
	}
}

C++: #include<iostream>
using namespace std;
#define INFTY (1<<21)
#define MAX 100
void floyd(int m, int C[MAX][MAX], int T[MAX][MAX]){
for(int k = 0;k<m;k++)
for(int i = 0;i<m;i++)
for(int j = 0;j<m;j++){
C[i][j] = min( C[i][j], C[i][k] + C[k][j] );
T[i][j] = min( T[i][j], T[i][k] + T[k][j] );
}
}
main(){
int n, m, a, b, c, t, k, q, C[MAX][MAX], T[MAX][MAX];
while( cin >> n >> m && !(n == 0 && m == 0) ){
for(int i = 0;i<m;i++)
for(int j = 0;j<m;j++) { C[i][j] = T[i][j] = INFTY; }
for(int i = 0;i<n;i++){
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;
}
floyd(m, C, T);
cin >> k;
for(int i = 0;i<k;i++){
cin >> a >> b >> q;
if ( q ) cout << T[a-1][b-1] << endl;
else cout << C[a-1][b-1] << endl;
}
}
}

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

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

int main(){
	int mon[100][100];
	int tim[100][100];
	int n,m;
	while(cin >> m >> n && n ){
		rep(i,n)rep(j,n) mon[i][j] = tim[i][j] = 1e9;
		for(int i = 0 ; i < m ; i++){
			int a,b,c,d;
			cin >> a >> b >> c >> d;
			a--,b--;
			mon[a][b] = mon[b][a] = c;
			tim[a][b] = tim[b][a] = d;
		}
		rep(i,n)rep(j,n)rep(k,n){
			mon[j][k] = min(mon[j][k],mon[j][i]+mon[i][k]);
			tim[j][k] = min(tim[j][k],tim[j][i]+tim[i][k]);
		}
		int R;
		cin >> R;
		rep(i,R){
			int a,b,c;
			cin >> a >> b >> c;
			a--,b--;
			if(c) cout << tim[a][b];
			else cout << mon[a][b];
			cout << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>

using namespace std;

#define INF 1e9

void wf(vector< vector<int> >& m) {
	int N = m.size();
	for (int k = 0; k < N; ++k) for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) {
		m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
	}
}

int main() {
	int n, N;
	while (cin >> n >> N, n || N) {
		vector< vector<int> > cost(N, vector<int>(N, INF));
		vector< vector<int> > time(N, vector<int>(N, INF));
		for (int i = 0; i < N; ++i) cost[i][i] = time[i][i] = 0;
		for (int i = 0; i < n; ++i) {
			int a, b, c, t;
			cin >> a >> b >> c >> t; --a, --b;
			cost[a][b] = cost[b][a] = c;
			time[a][b] = time[b][a] = t;
		}

		wf(cost);
		wf(time);

		int k; cin >> k;
		for (int t = 0; t < k; ++t) {
			int p, q, r; cin >> p >> q >> r; --p, --q;
			vector< vector<int> >& m = r == 0 ? cost : time;
			cout << m[p][q] << endl;
		}
	}
}