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]
					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 <vector>
#include <utility>
#include <queue>
#include <functional>
#define N 101
#define INF 2000000000
using namespace std;

int dist[N];
typedef pair<int,int> pii;

std::vector<pii> e1[N];
std::vector<pii> e2[N];

int dfs(int now,int c){
	if(dist[now]>=c)dist[now]=c;
	else return 0;
	for(int i=0;i<e1[now].size();i++){
		dfs(e1[now][i].second,c+e1[now][i].first);
	}
	return 0;
}

int dfs1(int now,int c){
	if(dist[now]>=c)dist[now]=c;
	else return 0;
	for(int i=0;i<e2[now].size();i++){
		dfs1(e2[now][i].second,c+e2[now][i].first);
	}
	return 0;
}

int main(void){
	int a,b,c,d;
	int n,m;
	while(cin>>n>>m,n||m){
		for(int i=0;i<n;i++){
			cin>>a>>b>>c>>d;
			e1[a].push_back(make_pair(c,b));
			e2[a].push_back(make_pair(d,b));
		}
		int k;
		cin>>k;
		while(k--){
			for(int i=1;i<=m;i++)dist[i]=INF;
			cin>>a>>b>>c;
			if(c) dfs1(a,0);
			else dfs(a,0);
			cout<<dist[b]<<endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

const int INF = 1<<25;

using namespace std;

int main()
{
	int n,m,i,j;
	int cost[101][101],time[101][101];
	
	for(;;)
	{
		cin >> n >> m ;
		if(n==0 && m==0)
			break;
		
		for(i=0;i<m;i++)
		{
			for(j=0;j<m+1;j++)
			{
				if(i!=j)
					cost[i][j]=time[i][j]=INF;
				else
					cost[i][j]=time[i][j]=0;
			}
		}
		
		for(i=0;i<n;i++)
		{
			int a,b,co,ti;
			cin >> a >> b >> co >> ti ;
			cost[a][b]=cost[b][a]=co;
			time[a][b]=time[b][a]=ti;
		}
		
		int inp;
		cin >> inp ;
		
		for(int k=0;k<m+1;k++)
		{
			for(i=0;i<m+1;i++)
			{
				for(j=0;j<m+1;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]);
				}
			}
		}
		
		for(i=0;i<inp;i++)
		{
			int st,en,se;
			cin >> st >> en >> se ;
			
			if(se==0)
				cout << cost[st][en] << endl;
			else
				cout << time[st][en] << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#define INF 99999999
using namespace std;


struct order{
	int start;
	int end;
	int kind;
};

struct edge{
	int from;
	int to;
	int cost;
	int time;
};


int dis[2][100][100];//種類、出発駅、各駅 cost:0 time:1
int counter=0;
//答えのカウンタ
edge Eg[3000];

int ans[100000];//答えの保存先
int flag=0;

//グローバルな配列 :dis, eg,ans

void shortest(int start,int N,int E,int KIND){//出発駅,N:フィールド幅,E:エッジ本数
	for(int i=0;i<N;i++)
	dis[KIND][start][i]=INF;
	
	dis[KIND][start][start]=0;
	
	if(KIND==0){
	while(true){
		bool update=false; //updateあればtrue
		
		for(int j=0;j<E;j++){//エッジでまわす
			edge es=Eg[j];
			if(dis[KIND][start][es.from]!=INF && 
						dis[KIND][start][es.to]>dis[KIND][start][es.from]+es.cost){
				dis[KIND][start][es.to]=dis[KIND][start][es.from]+es.cost;
				update=true;
			}
		}
		for(int j=0;j<E;j++){//エッジでまわす
			edge es=Eg[j];
			if(dis[KIND][start][es.to]!=INF && 
						dis[KIND][start][es.from]>dis[KIND][start][es.to]+es.cost){
				dis[KIND][start][es.from]=dis[KIND][start][es.to]+es.cost;
				update=true;
			}
		}
		if(!update) break;
		}
	}
	
	if(KIND==1){
	while(true){
		bool update=false; //updateあればtrue
		
		for(int j=0;j<E;j++){//エッジでまわす
			edge es=Eg[j];
			if(dis[KIND][start][es.from]!=INF && 
						dis[KIND][start][es.to]>dis[KIND][start][es.from]+es.time){
				dis[KIND][start][es.to]=dis[KIND][start][es.from]+es.time;
				update=true;
			}
		}
		for(int j=0;j<E;j++){//エッジでまわす
			edge es=Eg[j];
			if(dis[KIND][start][es.to]!=INF && 
						dis[KIND][start][es.from]>dis[KIND][start][es.to]+es.time){
				dis[KIND][start][es.from]=dis[KIND][start][es.to]+es.time;
				update=true;
			}
		}
		if(!update) break;
		}
	}
}

void solve(){
	int E,N,k;
	cin >> E >> N;
	if(E==0) {flag=1; return;}
	
	for(int i=0;i<E;i++){
		cin >> Eg[i].from >> Eg[i].to >> Eg[i].cost >> Eg[i].time;
		Eg[i].from--; Eg[i].to--; //駅の番号-1
	}
	cin>>k;
	order O[200];
	for(int i=0;i<k;i++){
		cin >> O[i].start >> O[i].end >> O[i].kind;
		O[i].start--; O[i].end--;//駅の番号-1
	}
	

	
	for(int x=0;x<k;x++){
		shortest(O[x].start,N,E,O[x].kind);
		ans[counter]=dis[O[x].kind][O[x].start][O[x].end];
		counter++;
	}
}



int main (){
	
	while(flag==0){
		solve();
	}
	
	for(int i=0;i<counter;i++)
	cout << ans[i]<<"\n";
}

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

#define INF (1<<12)

typedef struct{
  int cost,time;
}T;

int n,m;
T dp[111][111];

void init();
void warshall();

int main(){
  int a,b,cost,time;
  int k;
  int p,q,r;
  
  while(true){
    
    scanf(" %d %d",&n,&m);
    if(n == 0 && m == 0)break;
    
    // 初期処理
    init();
    
    for(int i=0;i<n;i++){
      scanf(" %d %d %d %d",&a,&b,&cost,&time);
      dp[a][b].cost = cost;
      dp[a][b].time = time;
      dp[b][a].cost = cost;
      dp[b][a].time = time;
    }
    
    warshall();
    
    scanf(" %d",&k);
    for(int i=0;i<k;i++){
      scanf(" %d %d %d",&p,&q,&r);
      if(r){
	printf("%d\n",dp[p][q].time);
      } else {
	printf("%d\n",dp[p][q].cost);
      }
    }
  }
  
  return 0;
}

void init(){
  
  for(int i=0;i<=m;i++){
    for(int j=0;j<=m;j++){
      if(i==j){
	// 今いるところから今いるところまではcost,timeともに0
	dp[i][j].cost = 0;
	dp[i][j].time = 0;
      } else {
	// 他はINF
	dp[i][j].cost = INF;
	dp[i][j].time = INF;
      }
    }
  }
  
  return;
}

void warshall(){
  
  for(int k=1;k<=m;k++){
    for(int i=1;i<=m;i++){
      for(int j=1;j<=m;j++){
	dp[i][j].cost = min(dp[i][j].cost,dp[i][k].cost + dp[k][j].cost);
	dp[i][j].time = min(dp[i][j].time,dp[i][k].time + dp[k][j].time);
      }
    }
  }
  
  return;
}