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 <cstdio>
#include <limits.h>
using namespace std;


				
int main()
{
	int n,m,a,b,co,ti,p,r;
	int money[100][100];
	int time[100][100];

	while(cin >> n >> m && n && m)
	{
		/*[m-1][m-1]ツづ慊づづ個配ツ療アツづーツ渉可甘コツ可サ*/
		for(int i=0;i<100;i++)
			for(int j=0;j<100;j++)
				time[i][j]=money[i][j]= (i-j) ? (1<<22) : 0;

		for(int i=0;i<n;i++)
		{
			cin >> a >> b >> co >> ti;
			money[a-1][b-1]=money[b-1][a-1]=co;
			time[a-1][b-1]=time[b-1][a-1]=ti;
		}
		/*ツδ渉ーツシツδδδ仰フツδ債イツドツ法 for time*/
		for(int k=0;k<m;k++)
			for(int i=0;i<m;i++)
				for(int j=0;j<m;j++)
					time[i][j]=time[j][i]=min(time[i][j],time[i][k]+time[k][j]);
					
		/*ツδ渉ーツシツδδδ仰フツδ債イツドツ法 for cost*/	
		for(int k=0;k<m;k++)
			for(int i=0;i<m;i++)
				for(int j=0;j<m;j++)
					money[i][j]=money[j][i]=min(money[i][j],money[i][k]+money[k][j]);
		cin >> p;
		
		/*
		for(int i=0;i<m;i++)
		{
			
			for(int j=0;j<m;j++)printf("%10d",money[i][j]);
			cout << endl;
		}
		*/
		
		for(int i=0;i<p;i++)
		{
			cin >> a >> b >> r;
			cout <<(r?time[a-1][b-1]:money[a-1][b-1])<< endl;
		}
	}
}

C++: #include<stdio.h>
int mt[100][100]={{0}};
int mm[100][100]={{0}};
int ct[101][101];
int cm[101][101];
int Q[10000];
int main(){
  int n,m,q,r,i,j,k,l,s,g,h,c;
  int a,b,cost,time;
  while(1){
    scanf("%d %d",&n,&m);
    if(n==0&&m==0)break;
    for(i=0;i<n;i++){
      scanf("%d %d %d %d",&a,&b,&cost,&time);
      mt[--a][--b]=time;
      mt[b][a]=time;
      mm[a][b]=cost;
      mm[b][a]=cost;
    }
    for(i=0;i<m;i++){
      for(j=0;j<m;j++){
	cm[i][j]=1000000000;
	ct[i][j]=1000000000;
      }
    }
    for(s=0;s<m;s++){
      for(g=0;g<m;g++){
	//scanf("%d",&k);
	//for(i=0;i<k;i++){
	//scanf("%d %d %d",&s,&g,&h);
	Q[0]=s;
	q=0;
	r=1;
	//s--;
	//g--;
	c=0;
	int checkm[100]={0};
	for(j=0;j<m;j++){
	  if(mm[g][j]!=0)c++;
	  checkm[j]=1;
	}
	checkm[0]=0;
	cm[s][s]=0;
	ct[s][s]=0;
	//if(h==0){
	while(q!=r){
	  for(j=0;j<m;j++){//printf("%d,%d,%d\n",Q[q],j,mm[Q[q]][j]);
	    if(mm[Q[q]][j]!=0){
	      if(checkm[j]==1){
		Q[r]=j;
		//check[j]=0;
		//printf("%d %d %d %d %d\n",Q[q],j,cm[j],cm[Q[q]],mm[Q[q]][j]);
		if(cm[s][j]>cm[s][Q[q]]+mm[Q[q]][j])cm[s][j]=cm[s][Q[q]]+mm[Q[q]][j];
		r++;
	      }
	    }
	  }
	  if(Q[q]==g)c--;
	  checkm[Q[q]]=0;
	  q++;
	  if(c==0)break;
	}
	//printf("%d\n",cm[g]);
	//}
	//else{
	Q[0]=s;
	q=0;
	r=1;
	c=0;
	int checkt[100]={0};
	for(j=0;j<m;j++){
	  if(mm[g][j]!=0)c++;
	  checkt[j]=1;
	}
	checkt[0]=0;
	cm[s][s]=0;
	ct[s][s]=0;
	while(q!=r){
	  for(j=0;j<m;j++){
	    if(mt[Q[q]][j]!=0){
	      if(checkt[j]==1){
		Q[r]=j;
		//check[j]=0;
		if(ct[s][j]>ct[s][Q[q]]+mt[Q[q]][j])ct[s][j]=ct[s][Q[q]]+mt[Q[q]][j];
		r++;
	      }
	    }
	  }
	  if(Q[q]==g)c--;
	  checkt[Q[q]]=0;
	  q++;
	  if(c==0)break;
	}
	//printf("%d\n",ct[g]);
	//}
      }
    }
    scanf("%d",&k);
    for(i=0;i<k;i++){
      scanf("%d %d %d",&s,&g,&h);
      if(h==0)printf("%d\n",cm[s-1][g-1]);
      else    printf("%d\n",ct[s-1][g-1]);
    }
  }

  return 0;
}

C++: //青春の片道切符：：ワーシャルフロイド法

	/*for(int k=0;k<N;k++){//経由箇所(N<200)
		for(int i=0;i<N;i++){//スタート(N<200)
			for(int j=0;j<N;j++){//目的(N<200)
				d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
			}
		}
	}*/


#include<iostream>
#include<string>
#include<queue>
#include<utility>
#include<algorithm>
#include<cctype>
using namespace std;

long long gc[110][110];
long long gt[110][110];
int n,m;

void warshallfroid(){
	for(int k=0;k<n;k++){//経由箇所(N<200)
		for(int i=0;i<n;i++){//スタート(N<200)
			for(int j=0;j<n;j++){//目的(N<200)
				gc[i][j]=min(gc[i][j],gc[i][k]+gc[k][j]);
				gt[i][j]=min(gt[i][j],gt[i][k]+gt[k][j]);
			}
		}
	}
}



int main(){
	
	while(cin>>n>>m,n||m/*n,mが両方0のときお終わり*/){
		for(int i=0;i<n;i++){//スタート(N<200)
			for(int j=0;j<n;j++){//目的(N<200)
				gc[i][j]=9999999;//初期化
				gt[i][j]=9999999;//初期化
			}
		}
	

	
	for(int i=0;i<n;i++){
		int a,b,c,t;
		cin>>a>>b>>c>>t;
		a--,b--;
		gc[a][b]=gc[b][a]=c;
		gt[a][b]=gt[b][a]=t;
	}
	warshallfroid();
	
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
	int p,q,r;
		cin>>p>>q>>r;
		p--,q--;
		if(r==0)cout<<gc[p][q]<<endl;
		else cout<<gt[p][q]<<endl;
	}
	
	}

}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#define N 100
#define M 10000

using namespace std;

int Dijkstra(vector< vector<int> > data, int s, int g){
  vector<bool> is_reached(data.size(), false);
  vector<int> ans(data.size(), M);
  ans[s] = 0;

  int i, j;
  i = s;
  while(i != g){
    is_reached[i] = true;
    for(j=0; j<data[i].size(); ++j){
      ans[j] = min(ans[j], ans[i] + data[i][j]);
    }
    for(i=-1, j=0; j<is_reached.size(); ++j){
      if(!is_reached[j] && (i < 0 || ans[i] > ans[j])) i = j;
    }
  }
  return ans[g];
}

int main(){
  int n, m, a, b, c, t;
  int p, q, r;

  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    vector< vector<int> > cost(m, vector<int>(m, M));
    vector< vector<int> > time(m, vector<int>(m, M));

    for(int i=0; i<n; ++i){
      cin >> a >> b >> c >> t;
      --a;
      --b;
      cost[a][b] = c;
      cost[b][a] = c;
      time[a][b] = t;
      time[b][a] = t;
    }
    cin >> n;
    for(int i=0; i<n; ++i){
      cin >> p >> q >> r;
      --p;
      --q;
      if(r == 0) cout << Dijkstra(cost, p, q) << endl;
      else cout << Dijkstra(time, p, q) << endl;
    }
  }
  return 0;
}