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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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 time[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++){
				time[i][j] = 1000000;
				cost[i][j] = 1000000;
			}
		}

		for (i = 0; i < rail; i++){
			cin >> a >> b >> c >> t;
			time[a][b] = t;
			time[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++){
						time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
					}
				}
			}


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

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


		}
	


	}



}

C++: #include<bits/stdc++.h>
using namespace std;
#define M 100000007
typedef pair<int,int> P;
int main(void){
	int n,m,k;
	while(1){
		cin>>n>>m;
		if(n==0)break;
		vector<P> cost[m+1],time[m+1];
		for(int i=0;i<n;i++){
			int a,b,c,t;
			cin>>a>>b>>c>>t;
			cost[a].push_back(P(b,c)); cost[b].push_back(P(a,c));
			time[a].push_back(P(b,t)); time[b].push_back(P(a,t));
		}
		cin>>k;
		for(int i=0;i<k;i++){
			int s,g,f;
			int leng[m+1];
			priority_queue<P,vector<P>,greater<P> > que;
			fill(leng,leng+m+1,M);
			cin>>s>>g>>f;
			leng[s]=0;
			que.push(P(0,s));
			if(f==0){
				while(!que.empty()){
					P p=que.top(); que.pop();
					int v=p.second;
					if(leng[v]<p.first) continue;
					for(int j=0;j<cost[v].size();j++){
						P x=cost[v][j];
						if(leng[x.first]>leng[v]+x.second){
							leng[x.first]=leng[v]+x.second;
							que.push(P(leng[x.first],x.first));
						}
					}
				}
				cout<<leng[g]<<endl;
			}else{
				while(!que.empty()){
					P p=que.top(); que.pop();
					int v=p.second;
					if(leng[v]<p.first) continue;
					for(int j=0;j<time[v].size();j++){
						P x=time[v][j];
						if(leng[x.first]>leng[v]+x.second){
							leng[x.first]=leng[v]+x.second;
							que.push(P(leng[x.first],x.first));
						}
					}
				}
				cout<<leng[g]<<endl;
			}
		}
	}
	return 0;
}

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

#define MAX 256
#define INF 1000000

struct edge {
	int money;
	int time;
};

int main(){
	int n, m;
	int count;
	edge G[100][100];

	while(cin >> n >> m, n || m){
		for(int i=0; i < 100; i++){
			for(int j=0; j < 100; j++){
				G[i][j].money = INF;
				G[i][j].time = INF;
			}
		}
		for(int i=0; i < 100; i++){
			G[i][i].money = 0;
			G[i][i].time = 0;
		}

		for(int i=0; i < n; i++){
			int a, b, mo, t;
			cin >> a >> b >> mo >> t;

			G[a][b].time=t;
			G[a][b].money=mo;
			G[b][a].time=t;
			G[b][a].money=mo;
		}

		for(int k=0; k < n; k++){
			for(int i=0; i < n; i++){
				for(int j=0; j < n; j++){
					G[i][j].time = min(G[i][j].time, G[i][k].time+G[k][j].time);
					G[i][j].money = min(G[i][j].money, G[i][k].money+G[k][j].money);
				}
			}
		}

		cin >> count;
		for(int i=0; i < count; i++){
			int p;
			cin >> n >> m >> p;

			if(p) cout << G[n][m].time << endl;
			else cout << G[n][m].money << endl;
		}
	}

}

C++: #include <stdio.h>
#include <limits.h>

const int INF = INT_MAX;

int main(void)
{
	int n, m, a, b, k, t;
	int tcost[105][105], mcost[105][105], node[105], used[105];
	int i, j, min, cost, time;
	
	while (1){
		scanf("%d %d", &n, &m);
		
		if (n == 0 && m == 0){
			break;
		}
		
		for (i = 0; i < m; i++){
			for (j = 0; j < m; j++){
				mcost[i][j] = INF;
				tcost[i][j] = INF;
			}
		}
		
		for (i = 0; i < n; i++){
			scanf("%d %d", &a, &b);
			a--; b--;
			scanf("%d %d", &cost, &time);
			
			if (cost < mcost[a][b]){
				mcost[a][b] = cost;
				mcost[b][a] = cost;
			}
			if (time < tcost[a][b]){
				tcost[a][b] = time;
				tcost[b][a] = time;
			}
			
		}
		
		scanf("%d", &k);
		
		for (i = 0; i < k; i++){
			scanf("%d %d %d", &a, &b, &t);
			a--; b--;
			for (j = 0; j < m; j++){
				node[j] = INF;
				used[j] = 0;
			}
			
			node[a] = 0;
			while (1){
				used[a] = 1;
				min = INF;
				
				for (j = 0; j < m; j++){
					if (t == 1){
						if (used[j] == 0 && tcost[a][j] != INF && node[j] > node[a] + tcost[a][j]){
							node[j] = node[a] + tcost[a][j];
						}
						if (used[j] == 0 && min > node[j]){
							min = j;
						}
					}
					else{
						if (used[j] == 0 && mcost[a][j] != INF && node[j] > node[a] + mcost[a][j]){
							node[j] = node[a] + mcost[a][j];
						}
						if (used[j] == 0 && min > node[j]){
							min = j;
						}
					}
				}
				a = min;
				if (a == b){
					break;
				}
			}
			printf("%d\n", node[b]);
		}
	}
	return (0);
}