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<map>
#include<queue>
#include<functional>
#define INF 1000000000//INF=∞
using namespace std;
typedef pair<int, int> P;//firstは最短距離,secondは頂点番号

const int MAX_M = 100;

struct edge {
  int to, cost;
  //edge() : to(0), cost(0) {}
  //edge(int to, int cost) : to(to), cost(cost) {}
};
int V;//頂点の数

vector<vector<edge> > time_graph(MAX_M); //頂点間の距離情報(隣接リスト)
vector<vector<edge> > cost_graph(MAX_M);

int dijkstra_time(int s, int g){
  priority_queue<P, vector<P>, greater<P> > que;
  vector<int> d(V, INF);//最短距離を記録する配列(∞で初期化しておく)
  d[s] = 0;//頂点sから頂点sへの最短距離は0で確定
  que.push(P(0, s));//距離0,頂点sからスタート

  while(!que.empty()){
    /*処理1*/
    //キューの中で最も頂点sからの距離が近い頂点vとその距離cを取得
    P p = que.top();
    que.pop();
    int v = p.second, c = p.first;

    /*処理2*/
    //頂点vの最短距離がすでに確定していたらcontenue
    if(d[v] < c)
      continue;

    /*処理3*/
    //頂点vの最短距離が既に確定していなければ頂点vへの最短経路はcで確定
      d[v]=c;

    /*処理4*/
    //頂点vから移動しうる頂点とその距離をキューに入れる
    for(int i = 0; i < time_graph[v].size(); i++){
      //頂点vはG[v].size()個の行き先を持つ
      edge e = time_graph[v][i];
      if(d[e.to] > d[v] + e.cost){
        que.push(P(d[v] + e.cost, e.to));
      }
    }
  }

  return d[g];//頂点gへの最短距離を返す
}


int dijkstra_cost(int s, int g){
  priority_queue<P, vector<P>, greater<P> > que;
  vector<int> d(V, INF);//最短距離を記録する配列(∞で初期化しておく)
  d[s] = 0;//頂点sから頂点sへの最短距離は0で確定
  que.push(P(0, s));//距離0,頂点sからスタート

  while(!que.empty()){
    P p = que.top();
    que.pop();
    int v = p.second, c = p.first;

    if(d[v] < c)
      continue;

      d[v]=c;

    for(int i = 0; i < cost_graph[v].size(); i++){
      edge e = cost_graph[v][i];
      if(d[e.to] > d[v] + e.cost){
        que.push(P(d[v] + e.cost, e.to));
      }
    }
  }

  return d[g];//頂点gへの最短距離を返す
}


int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    V = m;

    for (int i = 0; i < MAX_M; i++) {
      time_graph[i].clear();
      cost_graph[i].clear();
    }

    for (int i = 0; i < n; i++) {
      int a, b, time, cost;
      cin >> a >> b >> time >> cost;
      a--; b--; // 0-indexedにする
      edge e1,e2,e3,e4;
      e1.to = e3.to = b; e1.cost = e2.cost = time;
      e2.to = e4.to = a; e3.cost = e4.cost = cost;
      time_graph[a].push_back(e1);
      time_graph[b].push_back(e2);
      cost_graph[a].push_back(e3);
      cost_graph[b].push_back(e4);
    }

    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 << dijkstra_time(p, q) << endl;
      else
        cout << dijkstra_cost(p, q) << endl;
    }
  }
}

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

const int INF = 1000000000;

int main()
{
	while(true){
		int n, m;	//線路の情報の数n, 駅の数m
		cin >> n >> m;

		if(n==0 && m==0)
			break;

		int C[100][100];	//costのグラフ
		int T[100][100];	//timeのグラフ
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++){
				if(i==j){
					C[i][j] = T[i][j] = 0;		//対角線は0
				}
				else{
					C[i][j] = T[i][j] = INF;    
				}
			}
		}

		for(int i=0; i<n; i++){
			int a, b, c, t;		//線路の情報a,bとcost,time
			cin >> a >> b >> c >> t;

			C[a-1][b-1] = C[b-1][a-1] = c;
			T[a-1][b-1] = T[b-1][a-1] = t;
		}

		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				for(int k=0;k<m;k++){
					if(C[j][k]>C[j][i]+C[i][k])
						C[j][k] = C[j][i]+C[i][k];
					if(T[j][k]>T[j][i]+T[i][k])
						T[j][k] = T[j][i]+T[i][k];
				}
			}
		}

		int k;	//問い合わせの数
		cin >> k;
		for(int i=0;i<k;i++){
			int p, q, r;	//出発駅p, 到着駅q, 値の種類r
			cin >> p >> q >> r;
			if(r)	//r==1なら最短時間
				cout << T[p-1][q-1] << endl;
			else
				cout << C[p-1][q-1] << endl;
		}	
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define INF 10000000
using namespace std;
int cost[1001][1001];
int t_cost[1001][1001];
int main(){
  int n,m;
  while(cin >> n >> m , n+m ){
    m++;
    for(int i = 0  ; i < m ; i++ ){
      for(int j = 0  ; j < m ; j++ ){
	if(i == j)cost[i][j] = 0;
	else cost[i][j] = INF; 
	if(i == j)t_cost[i][j] = 0;
	else t_cost[i][j] = INF; 
      }
    }
    for(int i = 0 ; i < n ; i++ ){
      int a,b,c,d;
      cin >> a >> b >> c >> d;
      cost[a][b] = cost[b][a] = c;
      t_cost[a][b] = t_cost[b][a] = d;  
    }
    for(int i = 0 ; i < m ; i++ ){
      for(int j = 0 ; j < m ; j++ ){
	for(int k = 0 ; k < m ; k++ ){
	  cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
	  t_cost[i][j] = min(t_cost[i][j],t_cost[i][k] + t_cost[k][j]);
	}
      }
    }
    int k;
    cin >> k;
    for(int i = 0 ; i < k ; i++ ){
      int from,to,r;
      cin >> from >> to >> r; 
      if(r == 0){
	cout << cost[from][to] << endl;
      } else {
	cout << t_cost[from][to] << endl;
      }
    }
  }
  return 0;
}