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 <stdio.h>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int,int> P;//P.first:最小コスト P.second:頂点番号

//cost[x][i][j]:iからjへの種類xのコスト
//xが1なら時間,0なら金額をあらわす
int cost[2][301][301];

//smore[x][i][j]:iからjへの最短距離
int smore[2][301][301];

//somerode[x][i]:iから移動できる頂点の番号を保持
queue <int> somerode[2][301];

//m:駅の数
int m;

//seeked[i]:頂点iについてダイクストラしたかのフラグ
bool seeked[2][301];

//dijkstra(int):ダイクストラ法をstartを始点に行う
void dijkstra(int start,int type);

int main()
{
	int n,a,b,C,T,p,q,r,ans[300],k;

	scanf("%d%d",&n,&m);

	while(n != 0 && m != 0){

		//フラグ、コスト、最小値の初期化
		for(int i = 0;i <= 300; i++){
			seeked[0][i] = false;
			seeked[1][i] = false;
			for(int j = 0;j <= 300; j++){
				smore[0][i][j] = INT_MAX;
				smore[1][i][j] = INT_MAX;
				cost[0][i][j] = 10000;
				cost[1][i][j] = 10000;
			}
		}
		
		//路線情報入力
		for(int i = 0;i < n; i++){
			scanf("%d%d%d%d",&a,&b,&C,&T);
			if(cost[0][a][b] > C){
				cost[0][a][b] = C;
				somerode[0][a].push(b);
				//printf("出発:%d 目的:%d コスト:%d 記録コスト:%d\n",a,b,C,cost[0][a][b]);
			}
			if(cost[0][b][a] > C){
				cost[0][b][a] = C;
				somerode[0][b].push(a);
				//printf("出発:%d 目的:%d コスト:%d 記録コスト:%d\n",a,b,C,cost[0][b][a]);
			}
			if(cost[1][a][b] > T){
				cost[1][a][b] = T;
				somerode[1][a].push(b);
				//printf("出発:%d 目的:%d 時間:%d 記録時間:%d\n",a,b,T,cost[1][a][b]);
			}
			if(cost[1][b][a] > T){
				cost[1][b][a] = T;
				somerode[1][b].push(a);
				//printf("出発:%d 目的:%d 時間:%d 記録時間:%d\n",a,b,T,cost[1][b][a]);
			}
		}

		//問い合わせ
		scanf("%d",&k);
		//printf("問い合わせ個数:%d\n",k);
		for(int i = 0;i < k; i++){
			scanf("%d%d%d",&p,&q,&r);
			//printf("p:%d q:%d r:%d",p,q,r);
			if(seeked[r][q])	ans[i] = smore[r][q][p];
			else if (seeked[r][p])	ans[i] = smore[r][p][q];
			else{
				dijkstra(p,r);
				ans[i] = smore[r][p][q];
				seeked[r][p] = true;
			}
		}
		for(int i = 0; i < k; i++){
			printf("%d\n",ans[i]);
		}
		scanf("%d%d",&n,&m);
	}

	return 0;
}

void dijkstra(int start,int type){
	//printf("キュー宣言前\n");
	priority_queue< P,vector<P>,greater<P> > que;
	//printf("キュー宣言後 %d %d\n",type,start);
	smore[type][start][start] = 0;
	//printf("キュー代入前\n");
	que.push(P(0,start));
	//printf("while前\n");

	while(!que.empty()){
		P p = que.top(); que.pop();	//キューから最小コストの頂点の番号とコストを取り出す
		int v = p.second;			//vにコストが最小の頂点の番号
		int size = somerode[type][v].size();
		//printf("最小頂点:%d\n",v);
		if(smore[type][start][v] < p.first)continue; //もし最小コストが取り出したコストより小さければ更新の必要がないので次のループへ
		for(int i = 0; i < size; i++){	//somerode[x][i]:iからいける頂点の番号をもつキュー
			//もし現在の(start-i間最小コスト)より(移動コスト+現在までかかったコスト)が小さければ更新
			//printf("更新する点:%d 更新候補前:%d 更新候補後:%d\n",somerode[type][v].front(),smore[type][start][ somerode[type][v].front() ],smore[type][start][v] + cost[type][v][ somerode[type][v].front() ]);
			if(smore[type][start][ somerode[type][v].front() ] > smore[type][start][v] + cost[type][v][ somerode[type][v].front() ]){
				smore[type][start][ somerode[type][v].front() ] = smore[type][start][v] + cost[type][v][ somerode[type][v].front() ];
				//printf("更新したsmore:%d 更新した頂点:%d\n",smore[type][start][ somerode[type][v].front() ],somerode[type][v].front());
				que.push(P(smore[type][start][ somerode[type][v].front() ],somerode[type][v].front()));	//キュー更新
			}
			somerode[type][v].push( somerode[type][v].front() );
			somerode[type][v].pop();
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(cin >> n >> m,n){
		int a,b,c,d;
		int co[m][m];
		int ti[m][m];
		for(int i = 0; i < m; i++)
			for(int j = 0; j < m; j++){
				co[i][j] = 100000000;
				ti[i][j] = 100000000;
			}
		for(int i = 0; i < n; i++){
			cin >> a >> b >> c >> d;
			a--; b--;
			co[a][b] = c;
			co[b][a] = c;
			ti[a][b] = d;
			ti[b][a] = d;
		}
		vector <int> used;
		vector <int> dist;
		int k,p,q,r;
		cin >> k;
		for(int i = 0; i < k; i++){
			cin >> p >> q >> r;
			p--; q--;
			used = vector <int> (m,100000000);
			dist = vector <int> (m,100000000);
			dist[p] = 0;
			while(1){
				int va = -1;
				for(int j = 0; j < m; j++)
					if(used[j] == 100000000 && (va == -1 || dist[va] > dist[j]))
						va = j;
				if(va == -1)
					break;
				used[va] = 1;
				for(int j = 0; j < m; j++)
					dist[j] = min(dist[j] , (r == 0 ? co[j][va] : ti[j][va]) + dist[va]);
			}
			cout << dist[q] << endl;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#define INF (1<<21)
using namespace std;

int main(){
  int i,j,k,n,m,a,b,d,dd,c[101][101],t[101][101];
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    for(i=0;i<=m;i++){
      for(j=0;j<=m;j++){
	c[i][j] = INF;
	t[i][j] = INF;
	if(i == j){
	  c[i][j] = 0;
	  t[i][j] = 0;
	}
      }
    }

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

    for(i=0;i<=m;i++){
      for(j=0;j<=m;j++){
	for(k=0;k<=m;k++){
	  if(t[i][j] > t[i][k] + t[k][j]) t[i][j] = t[i][k] + t[k][j];
	  if(c[i][j] > c[i][k] + c[k][j]) c[i][j] = c[i][k] + c[k][j];
	}
      }
    }

    cin >> k;
    for(i=0;i<k;i++){
      cin >> a >> b >> d;
      if(d) cout << t[a][b] << endl;
      else cout << c[a][b] << endl;
    }

  }
  return 0;
}