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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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<functional>
#include<vector>
#include<queue>

using namespace std;

#define MMAX 100
#define INF 10000000

typedef pair<int,int> PII;

struct side{
	int dir,wei[2];
};

int main(void){

	int n,m;
	int a,b,cost,time;
	int k;
	int p,q,r;
	int es;
	int nod[MMAX+1];
	vector<side> ed[MMAX+1];
	priority_queue<PII,vector<PII>,greater<PII> > pq;
	side tmp;
	PII pii;
	int nm;

	while(1){

		for(int i=0;i<=MMAX;i++) nod[i]=0;
		for(int i=0;i<=MMAX;i++){
			if(!ed[i].empty()) ed[i].clear();
		}
		while(!pq.empty()) pq.pop();

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

		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			tmp.dir=b,tmp.wei[0]=cost,tmp.wei[1]=time;
			ed[a].push_back(tmp);
			tmp.dir=a;
			ed[b].push_back(tmp);
		}

		cin>>k;

		for(int i=0;i<k;i++){

			cin>>p>>q>>r;
			for(int j=0;j<=m;j++) nod[j]=INF;
			nod[p]=0;
			pq.push(PII(0,p));

			while(!pq.empty()){
				pii=pq.top();
				pq.pop();
				nm=pii.second;
				if(nod[nm]<pii.first) continue;
				es=ed[nm].size();
				for(int j=0;j<es;j++){
					tmp=ed[nm][j];
					if(nod[tmp.dir]>nod[nm]+tmp.wei[r]){
						nod[tmp.dir]=nod[nm]+tmp.wei[r];
						pq.push(PII(nod[tmp.dir],tmp.dir));
					}
				}
			}
			cout<<nod[q]<<endl;

		}

	}

}

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

const int INF = 1000000000;

int main()
{
	int C[100][100];
	int T[100][100];
	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;

		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];
						
					}
				}
			}
		}
		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];
						
					}
				}
			}
		}
		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<<C[p[i]-1][q[i]-1]<<endl;
		}
		if(r[i]==1){
			cout<<T[p[i]-1][q[i]-1]<<endl;
		}
	}

	return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;

int main(){

	int n,m,min_time[101][101],min_cost[101][101],from,to,cost,time,num,r;

	while(true){
		scanf("%d %d",&n,&m);
		if(n == 0 && m == 0)break;

		for(int i = 1; i <= m; i++){
			for(int k = 1; k <= m; k++){
				if(i != k){
					min_time[i][k] = 2000000000;
					min_cost[i][k] = 2000000000;
				}else{
					min_time[i][k] = 0;
					min_cost[i][k] = 0;
				}
			}
		}

		for(int i = 0; i < n; i++){
			scanf("%d %d %d %d",&from,&to,&cost,&time);
			min_time[from][to] = time;
			min_cost[from][to] = cost;
			min_time[to][from] = time;
			min_cost[to][from] = cost;
		}

		for(int k = 1; k <= m; k++){
			for(int i = 1; i <= m; i++){
				if(min_time[i][k] == 2000000000)continue;
				for(int p = 1; p <= m; p++){
					if(min_time[k][p] == 2000000000)continue;
					min_time[i][p] = min(min_time[i][p],min_time[i][k] + min_time[k][p]);
				}
			}

			for(int i = 1; i <= m; i++){
				if(min_cost[i][k] == 2000000000)continue;
				for(int p = 1; p <= m; p++){
					if(min_cost[k][p] == 2000000000)continue;
					min_cost[i][p] = min(min_cost[i][p],min_cost[i][k] + min_cost[k][p]);
				}
			}
		}

		scanf("%d",&num);

		for(int i = 0; i < num; i++){
			scanf("%d %d %d",&from,&to,&r);
			if(r == 1){
				printf("%d\n",min_time[from][to]);
			}else{
				printf("%d\n",min_cost[from][to]);
			}
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>  

using namespace std;

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; -i)
#define INF (1 << 30)
#define MOD 1000000007

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int N, M, K;
int a, b, Cost, Time;
int tTime[101][101];
int tCost[101][101];

int main() {
	while(scanf("%d %d", &N, &M), N||M) {
		memset(tTime, 0x3, sizeof(tTime));
		memset(tCost, 0x3, sizeof(tCost));
		rep(i, N) {
			scanf("%d %d %d %d", &a, &b, &Cost, &Time);
			tTime[a][b] = tTime[b][a] = Time;
			tCost[a][b] = tCost[b][a] = Cost;
		}
		FOR(i, 1, M + 1) FOR(j, 1, M + 1) FOR(k, 1, M + 1) {
			tTime[j][k] = min(tTime[j][k], tTime[j][i] + tTime[i][k]);
			tCost[j][k] = min(tCost[j][k], tCost[j][i] + tCost[i][k]);
		}
		scanf("%d", &K);
		rep(i, K) {
			int p, q, r;
			scanf("%d %d %d", &p, &q, &r);
			printf("%d\n", (r) ? tTime[p][q] : tCost[p][q]);
		}
	}
	return 0;
}