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 zikan[3001][3001];
int cost[3001][3001];


int main(){
	
	int i,j,k,a,b,t,c;
	int rail, station;
	int q,start,goal,dotti;
	int temp;
	
	


	while (1){
		
		cin >> rail >> station;
		
		if (rail == 0 && station == 0){
			break;
		}


		for (i = 0; i < station+1; i++){
			for (j = 0; j < station+1; j++){
				zikan[i][j] = 1000000;
				cost[i][j] = 1000000;
			}
		}

		for (i = 0; i < rail; i++){
			cin >> a >> b >> c >> t;
			zikan[a][b] = t;
			zikan[b][a] = t;
			cost[a][b] = c;
			cost[b][a] = c;
		}


		cin >> q;

		for (;q>0;q--){
			cin >> start >> goal >> dotti;

			for (k = 1; k <= station; k++){
				for (i = 1; i <= station; i++){
					for (j = 1; j <= station; j++){
						cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					}
				}
			}

			for (k = 1; k <= station; k++){
				for (i = 1; i <= station; i++){
					for (j = 1; j <= station; j++){
						zikan[i][j] = min(zikan[i][j], zikan[i][k] + zikan[k][j]);
					}
				}
			}


			if (dotti == 0){
				cout << cost[start][goal] << endl;
			}

			if (dotti == 1){
				cout << zikan[start][goal] << endl;
			}


		}
	


	}



}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
long n,m,a,b,c,d0[101][101],d1[101][101],t,p,q,r,i,j,k;
while(cin>>n>>m,n*n+m*m){
for(i=1;i<=m;i++)
for(j=1;j<=m;j++)d0[i][j]=d1[i][j]=3000000;
for(i=1;i<=m;i++)d0[i][i]=d1[i][i]=0;
for(i=0;i<n;i++){
cin>>a>>b>>c>>t;
d0[a][b]=d0[b][a]=c;
d1[a][b]=d1[b][a]=t;
}
for(k=1;k<=m;k++)
for(i=1;i<=m;i++)
for(j=1;j<=m;j++){
d0[i][j]=min(d0[i][j],d0[i][k]+d0[k][j]);
d1[i][j]=min(d1[i][j],d1[i][k]+d1[k][j]);
}
cin>>k;
for(i=0;i<k;i++){
cin>>p>>q>>r;
if(r==0)cout<<d0[p][q]<<endl;
else cout<<d1[p][q]<<endl;
}
}
return 0;
}

C++: Y

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

const int INF = 1000000000;

int main()
{
	int TA=0;
	int CO=0;
	int z=0;
	int p[200];
	int q[200];
	int r[200];
	int A[200];
	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];
						CO=C[i][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];
						TA=T[i][j];
					}
				}
			}
		}
		cin>>z;
		for(int i=0;i<z;i++){
			cin>>p[i]>>q[i]>>r[i];
		}
		
		
	}
	for(int i=0;i<z;i++){
		if(r[i]==0){
			cout<<CO<<endl;
		}
		if(r[i]==1){
			cout<<TA<<endl;
		}
	}

	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<string>
#include<cmath>

using namespace std;
#define rep(i,j) for(int i=0;i<(j);i++)
int ans[2][100][100];
int n,m;
int main(void){
    for(;;){
        scanf("%d%d",&n,&m);
        if(!n && !m)break;
        rep(i,2)rep(j,100)rep(k,100)if(j!=k)ans[i][j][k] = 99999999;else ans[i][j][k]=0;
        rep(i,n){
            int a,b,c,t;
            scanf("%d%d%d%d",&a,&b,&c,&t);
            a--;
            b--;
            ans[1][a][b] = t;
            ans[1][b][a] = t;
            ans[0][a][b] = c;
            ans[0][b][a] = c;
        }
        rep(k,m)rep(i,m)rep(j,m)rep(l,2)ans[l][i][j]=min(ans[l][i][j],ans[l][i][k]+ans[l][k][j]);
        int k,p,q,r;
        scanf("%d\n",&k);
        rep(i,k){
            scanf("%d%d%d",&p,&q,&r);
            q--;
            p--;
            printf("%d\n",ans[r][p][q]);
        }
    }

	return 0;
}