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<vector>
#include<queue>
#include<functional>

constexpr int MAX_N = 100, INF = (int)1e8;

using P = std::pair<int, int>;

struct edge {
	int to, cost;
	edge() = default;
	edge(int _t, int _c) : to(_t), cost(_c) {}
};

int n;
std::vector<edge> G[2][MAX_N];//0:??????,1:??????

int d[MAX_N];

int dijkstra(int s, int g, int index) {
	std::fill(d, d + n, INF);
	d[s] = 0;
	std::priority_queue<P, std::vector<P>, std::greater<P>> que;
	que.emplace(d[s], s);
	while (!que.empty()) {
		P p = que.top();
		que.pop();
		if (p.second == g)
			return d[g];
		if (d[p.second] > p.first)
			continue;
		for (int i = 0; i < G[index][p.second].size(); ++i) {
			edge e = G[index][p.second][i];
			if (d[e.to] > d[p.second] + e.cost) {
				d[e.to] = d[p.second] + e.cost;
				que.emplace(d[e.to], e.to);
			}
		}
	}
	return -1;
}

int main() {
	while (1) {
		int m;
		std::cin >> m >> n;
		if (m + n == 0)
			break;
		int a, b, c, t;
		for (int i = 0; i < m; ++i) {
			std::cin >> a >> b >> c >> t;
			--a;
			--b;
			G[0][a].emplace_back(b, c);
			G[0][b].emplace_back(a, c);
			G[1][a].emplace_back(b, t);
			G[1][b].emplace_back(a, t);
		}
		int q, r;
		std::cin >> q;
		int p[2];
		for (int i = 0; i < q; ++i) {
			std::cin >> p[0] >> p[1] >> r;
			--p[0];
			--p[1];
			std::cout << dijkstra(p[0], p[1], r) << std::endl;
		}
		for (int i = 0; i < 2; ++i) {
			for (int j = 0; j < n; ++j)
				G[i][j].clear();
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <queue>
#include <vector>
#include <iostream>
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();
		}
	}
}