Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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
					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++: #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;
		}
	}
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<cstring>
using namespace std;
#define INF 100000000
int mini(int a,int b){
	if(a<b){
		return a;
	}else{
		return b;
	}
}

int cost(int p,int q,int m,int co[][101]){
	int i,s[101],a,j,d[101],b;
	for(i=0;i<=m;i++){s[i]=0;d[i]=INF;}
	a=p;s[a]=1;d[a]=0;
	for(i=1;i<=m;i++){
		for(j=1;j<=m;j++){
			d[j]= mini(d[j],d[a]+co[a][j]);
		}
		s[a]=1;b=INF;
		for(j=1;j<=m;j++){
			if(s[j]==0&&d[j]<=b){
				a=j;b=d[j];
			}
		}
		s[a]=1;
	}
	return d[q];
}

int main(){
	while(1){
		int i,a=0,b=0,p,q,r,j,k,n,m,co[101][101],ti[101][101],s[101],ans[200];
		for(i=0;i<101;i++){
			s[i]=0;
			for(j=0;j<101;j++){
				if(i==j){
					co[i][j]=0;
					ti[i][j]=0;
				}else{
					co[i][j]=INF;
					ti[i][j]=INF;
				}
			}
		}
		cin>>n>>m;
		if(n==0&&m==0)break;
		for(i=0;i<n;i++){
			cin>>a>>b;
			cin>>co[a][b]>>ti[a][b];
			co[b][a]=co[a][b];
			ti[b][a]=ti[a][b];
		}
		cin>>k;
		for(i=0;i<k;i++){
			cin>>p>>q>>r;
			if(r==0){
				ans[i]=cost(p,q,m,co);
			}else{
				ans[i]=cost(p,q,m,ti);
			}
		}
		for(i=0;i<k;i++){
			cout<<ans[i]<<endl;
		}
	}

    return 0;
}