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<math.h>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#define SIZE 1000
#define INF 1000000
using namespace std;

int main(){

  int h,i,j,k;
  int p,q,r;
  int n,m;
  int a,b,c,d;
  int cost[SIZE][SIZE],time[SIZE][SIZE];

  while(cin>>n>>m,(n!=0 && m!=0)){

    for(i=1;i<=101;i++){
      for(j=1;j<=101;j++){
	cost[i][j]=INF;
	time[i][j]=INF;
      }
    }

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

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

	cin>>k;
	for(i=0;i<k;i++){
	  cin>>p>>q>>r;
	  
	  if(r==0){
	    cout<<cost[p][q]<<endl;
	  }
	  else {
	    cout<<time[p][q]<<endl;
	  }
	}
	
  }
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;
const int MAX = 101;

int mindis[101];

struct E{
	E(int from, int to, int cost, int time):from(from), to(to){
		ct[0] = cost;
		ct[1] = time;
	}
	int from, to;
	int ct[2];	//ct[0]がcost、ct[1]がtime
};

int bf(int s, int e, int mode, vector<E> tb){
	mindis[1] = 0;
	while(1){
		bool update = false;
		for(int i = 0; i < tb.size(); i++){
			if(mindis[tb[i].to] > mindis[tb[i].from] + tb[i].ct[mode]){
				mindis[tb[i].to] = mindis[tb[i].from] + tb[i].ct[mode];
				update = true;
			}
		}
		if(!update) break;
	}
	return mindis[e]-mindis[s];
}

int main(){
	int n, m;	//nが線路数、mが駅数
	while(cin >> n >> m && n|m){
		vector<E> tb;
		for(int i = 0; i < n; i++){
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			tb.push_back(E(a, b, cost, time));
		}
		int k;
		cin >> k;
		for(int i = 0; i < k; i++){
			fill(mindis, mindis+101, 1<<29);
			int s, e, mode;
			cin >> s >> e >> mode;
			cout << bf(s, e, mode, tb) << endl;
		}
	}
	return 0;
}

C++: #include <iostream>

#define N 101
#define INF 1e+9

using namespace std;

int cost[N][N], times[N][N];

int main(void){
	int n,m;
	while(cin>>n>>m,n||m){
		for(int i=0;i<n;++i)for(int j=0;j<n;++j){cost[i][j]=times[i][j]=INF;}
		for(int i=0,a,b,c,t;i<n;++i){
			cin>>a>>b>>c>>t;
			cost[a][b]=cost[b][a]=c;
			times[a][b]=times[b][a]=t;
		}
		int k;cin>>k;
		for(int i=0,p,q,r;i<k;++i){
			cin>>p>>q>>r;
			if(r){//time
				int d[N];
				for(int j=0;j<n;++j) d[j]=INF;
				d[p]=0;
				bool f=false;
				for(;;){
					for(int j=0;j<n;++j){//from
						for(int k=0;k<n;++k){//to
							if(d[j]!=INF && d[j] + times[j][k] < d[k]){
								f==true;
								d[k] = d[j] + times[j][k];
							}
						}
					}
					if(f==false) break;
					f=false;
				}
				cout<<d[q]<<endl;
			}else{//cost
				int d[N];
				for(int j=0;j<n;++j) d[j]=INF;
				d[p]=0;
				bool f=false;
				for(;;){
					for(int j=0;j<n;++j){//from
						for(int k=0;k<n;++k){//to
							if(d[j]!=INF && d[j] + cost[j][k] < d[k]){
								f==true;
								d[k] = d[j] + cost[j][k];
							}
						}
					}
					if(f==false) break;
					f=false;
				}
				cout<<d[q]<<endl;
			}
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int MAX=100000;
int main(){
  int n,m;
  cin>>n>>m;
  int a,b,cost,timer;
  int costs[m][m];
  int timers[m][m];
  for(int i=0;i<m;i++){
    for(int j=0;j<m;j++){
      costs[i][j]=MAX;
      timers[i][j]=MAX;
    }
    costs[i][i]=0;
    timers[i][i]=0;
  }

  for(int i=0;i<n;i++){
    cin>>a>>b>>cost>>timer;
    costs[a-1][b-1]=cost;
    timers[a-1][b-1]=timer;
    costs[b-1][a-1]=cost;
    timers[b-1][a-1]=timer;
  }
  /*
  cout<<endl<<endl<<endl;
  for(int i=0;i<m;i++){
    for(int j=0;j<m;j++){
      cout<<costs[i][j]<<"            ";
    }
    cout<<endl;
  }
  cout<<endl<<endl<<endl<<endl<<endl;*/
  for(int i=0;i<m;i++){
    for(int j=0;j<m;j++){
      for(int k=0;k<m;k++){
        if(costs[j][k]>(costs[j][i]+costs[i][k])){
          costs[j][k]=costs[j][i]+costs[i][k];
          costs[k][j]=costs[j][i]+costs[i][k];
        }
        if(timers[j][k]>(timers[j][i]+timers[i][k])){
          timers[j][k]=timers[j][i]+timers[i][k];
          timers[k][j]=timers[j][i]+timers[i][k];
        }
      }
    }
  }
  int k;
  int p;
  int q;
  bool r;
  cin>>k;
  for(int i=0;i<k;i++){
    cin>>p>>q>>r;
    if(r){
      cout<<timers[p-1][q-1]<<endl;
    }else if(!r){
      cout<<costs[p-1][q-1]<<endl;
    }
  }
  return 0;
}

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

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

int N,M;
int maze[110][110],tim[110][110];
int main(void){
	while(cin>>N>>M,N+M){
		rep(i,M)rep(j,M)maze[i][j] = tim[i][j] = (1<<29);
		while(N--){
			int a,b,c,t;
			cin>>a>>b>>c>>t;
			maze[--a][--b] = c;
			maze[b][a] = c;
			tim[a][b] = t;
			tim[b][a] = t;
		}
		rep(k,M){
			rep(i,M){
				rep(j,M){
					tim[i][j] = min(tim[i][j],tim[i][k] + tim[k][j]);
					maze[i][j] = min(maze[i][j],maze[i][k] + maze[k][j]);	
				}
			}
		}
		//rep(i,M)rep(j,M)cout<<tim[i][j]<<(j==M-1?'\n':' ');
		cin>>N;
		while(N--){
			int a,b,t;
			cin>>a>>b>>t;
			--a;--b;
			cout<<(t?tim[a][b]:maze[a][b])<<endl;
		}
	}
}