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 <stdio.h>
#define INF 10000000

int main(void){
	int n,m;
	
	while(scanf("%d%d",&m,&n),n,m){
		int i,j;
		int a,b,cost,time,c;
		int DPc[256][256];
		int DPt[256][256];
		int k,ans[256];
		
		for(i=0;i<256;i++){
			for(j=0;j<256;j++){
				if(i==j){
					DPc[i][j]=0;
					DPt[i][j]=0;
				}
				else{
					DPc[i][j]=INF;
					DPt[i][j]=INF;
				}
			}
		}
		
		for(i=0;i<m;i++){
			scanf("%d%d%d%d",&a,&b,&cost,&time);
			DPc[a][b]=cost;
			DPc[b][a]=cost;
			DPt[a][b]=time;
			DPt[b][a]=time;
		}
		for(i=0;i<=n;i++){
			for(j=0;j<=n;j++){
				for(k=0;k<=n;k++){
					if(DPt[i][k]+DPt[k][j]<DPt[i][j]){
						DPt[i][j]=DPt[i][k]+DPt[k][j];
					}
					if(DPc[i][k]+DPc[k][j]<DPc[i][j]){
						DPc[i][j]=DPc[i][k]+DPc[k][j];
					}
				}
			}
		}
		scanf("%d", &m);
		for(i=0;i<m;i++){
			scanf("%d%d%d",&a,&b,&c);
			if(c==1){
				ans[i]=DPt[a][b];
			}
			else {
				ans[i]=DPc[a][b];
			}
		}
		for(i=0;i<m;i++){
			printf("%d\n",ans[i]);
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int INF = 1000000;
int cost[301][301],time[301][301];
int main(){
	int n,m;
	while(cin >> n >> m,n || m){
		int a,b,icost,itime,c,r;
		for(int i = 0;i <= m;++i){
			for(int j = 0;j <= m;++j){
				if(i == j) {
					cost[i][i] = 0;
					time[i][i] = 0;
				}
				else {
					cost[i][j] = cost[j][i] = INF;
					time[i][j] = time[j][i] = INF;
				}
			}
		}
		for(int i = 0;i < n;++i){
			cin >> a >> b >> icost >> itime;
			cost[a][b] = cost[b][a] = icost;
			time[a][b] = time[b][a] = itime;
		}
		cin >> c;
		for(int i = 0;i < c;++i){
			cin >> a >> b >> r;
			for(int k = 0;k <= m;++k){
				for(int i = 0;i <= m;++i){
					for(int j = 0;j <= m;++j){
						if(r == 1) time[i][j] = min(time[i][j],time[i][k] + time[k][j]);
						else cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
					}
				}
			}
			if(r == 1) cout << time[a][b] << endl;
			else cout << cost[a][b] << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class home {
public:
	int time;
	int cost;
	home() :time(-1), cost(-1) {}
};

int main() {
	int n, m;
	while (cin >> n >> m&&n != 0) {
		vector<vector<home> > map(m + 1, vector<home>(m + 1));
		for (int i = 0; i < n; ++i) {
			int a, b, c, d;
			cin >> a >> b >> c >> d;
			map[a][b].cost = c;
			map[b][a].cost = c;
			map[a][b].time = d;
			map[b][a].time = d;
		}
		bool flag = true;
		while (flag) {
			flag = false;
			for (int i = 1; i <= m - 1; ++i) {
				for (int j = i + 1; j <= m; ++j) {
					for (int k = 1; k <= m; ++k) {
						if (i == k || j == k)continue;
						if (map[i][k].cost == -1 || map[j][k].cost == -1)continue;
						if (map[i][k].time == -1 || map[j][k].time == -1)continue;
						int cost = map[i][k].cost + map[j][k].cost;
						int time = map[i][k].time + map[j][k].time;
						if (map[i][j].cost == -1 || map[i][j].cost > cost) {
							map[j][i].cost = map[i][j].cost = cost;
						}
						if (map[i][j].time == -1 || map[i][j].time > time) {
							map[i][j].time = map[j][i].time = time;
						}
					}
				}
			}
		}
		int num;
		cin >> num;
		for (int i = 0; i < num; ++i) {
			int a, b, c;
			cin >> a >> b >> c;
			if (c == 0) {
				cout << map[a][b].cost << endl;
			}
			else {
				cout << map[a][b].time << endl;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 24;
int c[200][200];
int t[200][200];
int cost[200][200];
int tim[200][200];

int main(){
  int n, m, K;
  int p, q, r;
  while(cin >> n >> m && (n || m)){
    fill(&cost[0][0], &cost[199][199] + 1, INF);
    fill(&tim[0][0], &tim[199][199] + 1, INF);
    for(int i = 1; i <= m; i++){
      cost[i][i] = tim[i][i] = 0;
      c[i][i] = t[i][i] = 0;
    }

    for(int i = 0; i < n; i++){
      int a, b;
      int d, e;
      cin >> a >> b;
      cin >> d >> e;
      cost[b][a] = min(cost[b][a], d);
      cost[a][b] = min(cost[a][b], d);
      tim[b][a] = min(tim[b][a], e);
      tim[a][b] = min(tim[a][b], e);
    }

    for(int k = 1; k <= m; k++){
      for(int i = 1; i <= m; i++){
	for(int j = 1; j <= m; j++){
	  if(cost[i][j] > cost[i][k] + cost[k][j]){
	    cost[i][j] = cost[i][k] + cost[k][j];
	  }
	  if(tim[i][j] > tim[i][k] + tim[k][j]){
	    tim[i][j] = tim[i][k] + tim[k][j];
	  }
	}
      }
    }
    cin >> K;    
    for(int i = 0; i < K; i++){
      cin >> p >> q >> r;
      if(!r){
	cout << cost[p][q] << endl;
      }else{
	cout << tim[p][q] << endl;
      }
    }
    
  }
  return 0;
}

C++: //2013/11/15(Fri)

#include <iostream>
using namespace std;
int main()
{
	int n,m;
	int a,b,cost[100][100],time[100][100];
	int k;
	int p,q,r;
	int INF = 1001;

	int dcost[100][100],dtime[100][100];
	while(true){
		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;
					time[i][j] = 0;
				}		
				else{
					cost[i][j] = INF;
					time[i][j] = INF;
				}
			}

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

		for(int h=0; h<m; h++)
			for(int i=0; i<m; i++)
				for(int j=0; j<m; j++){
					if(cost[i][j] > cost[i][h] + cost[h][j])
						cost[i][j] = cost[i][h] + cost[h][j];
					if(time[i][j] > time[i][h] + time[h][j])
						time[i][j] = time[i][h] + time[h][j];
				}

		cin >> k;
		for(int i=0; i<k; i++){
			cin >> p >> q >> r;

			if(r == 0)
				cout << cost[p-1][q-1] << endl;
			else if(r == 1)
				cout << time[p-1][q-1] << endl;
		}
	}
	return 0;
}