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 <algorithm>
using namespace std;
int main(){
  int n,m;
  int a,b,c,d;
  int x;
  int p,q,r;
  int cost[3001][3001];
  int time[3001][3001];
  while(1){
    cin >> n >> m;
    if(n==0 && m==0)break;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cost[i][j]=999999;
	time[i][j]=999999;
      }
    }
    for(int i=0;i<n;i++){
      cin >> a >> b >> c >> d;
      cost[a-1][b-1]=c;
      cost[b-1][a-1]=c;
      time[a-1][b-1]=d;
      time[b-1][a-1]=d;
    }
    for(int k=0;k<n;k++){
      for(int i=0;i<n;i++){
	for(int j=0;j<n;j++){
	  cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
	  time[i][j]=min(time[i][j],time[i][k]+time[k][j]);
	}
      }
    }
    cin >> x;
    for(int i=0;i<x;i++){
      cin >> p >> q >> r;
      if(r==0){
	cout << cost[p-1][q-1] << endl;
      }else{
	cout << time[p-1][q-1] << endl;
      }
    }
  }
}

C++: //AOJ 0200
//菴ソ縺?婿繧ゅｏ縺九ｉ縺ェ縺?¥縺帙↓include縺励∪縺上ｋ莠コ髢薙?繧ッ繧コ
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<list>
#include<deque>
using namespace std;
#define INF 1 << 30
#define MAX_B 101

int a, b, i, j, k, money[MAX_B][MAX_B], time[MAX_B][MAX_B], n, m, c, d;
bool judge;
stack<int> left, right;


int main(){
  while(1){
    scanf("%d %d", &n, &m);
    if(n == 0 && m == 0) break;
    for(i = 1; i <= m; i++){
      for(j = 1; j <= m; j++){
	if(i == j){
	  money[i][j] = 0;
	  time[i][j] = 0;
	  continue;
	}
	money[i][j] = INF;
	time[i][j] = INF;
      }
    }
    for(i = 0; i < n; i++){
      scanf("%d %d %d %d", &a, &b, &c, &d);
      money[a][b] = c;
      money[b][a] = c;
      time[a][b] = d;
      time[b][a] = d;
    }
    for(i = 1; i <= m; i++){
      for(j = 1; j <= m; j++){
	for(k = 1; k <= m; k++){
	  if(money[j][k] > money[j][i] + money[i][k]) money[j][k] = money[j][i] + money[i][k];
	  if(time[j][k] > time[j][i] + time[i][k]) time[j][k] = time[j][i] + time[i][k];
	}
      }
    }
    scanf("%d", &a);
    for(i = 0; i < a; i++){
      scanf("%d %d %d", &b, &c, &d);
      if(d == 1) printf("%d\n", time[b][c]);
      else printf("%d\n", money[b][c]);
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,n)for(int i=0;i<n;i++)
int n,m,a,b,cost,time,k,p,q,r;
int c[100][100];
int t[100][100];
int main() {
	while(scanf("%d %d",&n,&m),n) {
		fill(c[0],c[0]+100*100,1<<27);
		fill(t[0],t[0]+100*100,1<<27);
		rep(i,n) {
			scanf("%d %d %d %d",&a,&b,&cost,&time);
			a--;b--;
			c[a][b]=c[b][a]=cost;
			t[a][b]=t[b][a]=time;
		}
		rep(k,m)rep(i,m)rep(j,m) {
			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]);
		}
		scanf("%d",&k);
		rep(i,k) {
			scanf("%d %d %d",&p,&q,&r);
			p--;q--;
			if(r)printf("%d\n",t[p][q]);
			else printf("%d\n",c[p][q]);
		}
	}
}

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

int cost(int p,int q,int n,int co[][101]){
	int i,s[101],a,j,d[101],b;
	for(i=0;i<=n;i++){s[i]=0;d[i]=2000;}
	a=p;s[a]=1;d[a]=0;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			d[j]= mini(d[j],d[a]+co[a][j]);
		}
		s[a]=1;b=2000;
		for(j=1;j<=n;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];
		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]=2000;
					ti[i][j]=2000;
				}
			}
		}
		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){
				cout<<cost(p,q,m,co)<<endl;
			}else{
				cout<<cost(p,q,m,ti)<<endl;
			}
		}
	}

    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 1000000000
struct data{
	int cost,time;
};
int main(){
	int n,m;
	while(cin >> n >> m,m+n){
		data d[110][110];
		for(int i = 0; i < 110; i++){
			for(int j = 0; j < 110; j++){
				d[i][j].cost = INF;
				d[i][j].time = INF;
			}
		}
		for(int i = 1; i <= n; i++){
			int a,b,c,t;
			cin >> a >> b >> c >> t;
			d[a][b].cost = c;
			d[a][b].time = t;
			d[b][a].cost = c;
			d[b][a].time = t;
		}
		for(int k = 1; k <= m; k++){
			for(int i = 1; i <= m; i++){
				for(int j = 1; j <= m; j++){
					d[i][j].cost = min(d[i][j].cost,d[i][k].cost+d[k][j].cost);
					d[i][j].time= min(d[i][j].time,d[i][k].time+d[k][j].time);
				}
			}
		}
		int k;
		int p,q,r;
		cin >> k;
		for(int i = 0; i < k; i++){
			cin >> p >> q >> r;
			if(r == 0)cout << d[p][q].cost << endl;
			else cout << d[p][q].time << endl;
		}
	}
	return 0;
}