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 <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cctype>

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define repl(i,l,n) for(int (i)=(int)(l);(i)<(int)(n);(i)++)
#define d_arr(arr, h, w) rep(i,(h) ){ cout << "["; rep(j,(w) ) { cout << (arr)[i][j] << ", "; } cout << "]" << endl;}


/*
1 行目 線路の情報の数 n 駅の数 m (整数 整数;半角空白区切り)
2 行目 第 1 の線路の情報 a b cost time(整数 整数 整数 整数;半角空白区切り)
3 行目 第 2 の線路の情報
      :
n+1 行目 第 n の線路の情報
n+2 行目 問い合わせの数 k(整数)
n+3 行目 第 1 の問い合わせ p q r (整数 整数 整数;半角空白区切り)
n+4 行目 第 2 の問い合わせ
      :
n+2+k 行 第 k の問い合わせ

出発駅 p 、到着駅 q 、出力する 値の種類 r で表され、 r が 0 の時は最小金額を、 1 の時は最短時間を出力
*/
#define M 1000000
typedef pair<int ,int> P;

int n,m;
int a, b, cost, tim;
int times[300][300];
int costs[300][300];

int p,q,r;

int least_cost();
int least_time(); 
int main()
{
    while ( cin >> n >> m )
    {
        if ( n == 0 && m == 0) { break; }
        rep(i,300)rep(j,300) { times[i][j] = M; }
        rep(i,300)rep(j,300) { costs[i][j] = M; }

        rep(i,n)
        {
            cin >> a >> b >> cost >> tim;
            costs[a][b] = cost;
            costs[b][a] = cost;
            times[a][b] = tim;
            times[b][a] = tim;
        }

        //d_arr(costs, n, n);

        int k;
        cin >> k;
        rep(i,k)
        {
            cin >> p >> q >> r;
            //cout << "p = " << p << " q = " << q << " r = " <<r << endl;
            if (r == 0 ) { cout << least_cost() << endl; }
            if (r == 1 ) { cout << least_time() << endl; }
        }

    }
}

int least_cost() 
{
    int leng[m+1];
    int is_cf[m+1];
    rep(i,m+1){ leng[i] = M; is_cf[i] = 0; }
    leng[p] = 0;
    //cout << "p = " << p << endl;

    //rep(j, m)
    repl(j,1,m+1)
    {
        int min = M;
        int min_i;
        //最小の接点を探す
        rep(t,m+1)
        {
            if( is_cf[t] == 0 && leng[t] < min ) { min_i = t; min = leng[t]; } 
            //cout << "min = " << min << endl;
        }
        //最小の接点を確定
        is_cf[min_i] = 1;
        //cout << "min_i = " << min_i << endl; 
         
        if ( min == M ) { cout << "not連結" << endl; exit(1); }

        //mini_iを加えて最短路の更新
        rep(t, m+1) 
        {
            //cout << "costs[" << min_i << "]["<< t << "] = " << costs[min_i][t] <<endl;
            
            if( ( leng[min_i] + costs[min_i][t] ) < leng[t]  )
            {
                leng[t] = leng[min_i] + costs[min_i][t];
                //cout << "leng[t] at t=" <<t << "==" << leng[t] <<endl;
            }
        }
    }
    cout << leng[q] << endl;
}

int least_time() 
{
    int leng[m+1];
    int is_cf[m+1];
    rep(i,m+1){ leng[i] = M; is_cf[i] = 0; }
    leng[p] = 0;
    //cout << "p = " << p << endl;

    //rep(j, m)
    repl(j,1,m+1)
    {
        int min = M;
        int min_i;
        //最小の接点を探す
        rep(t,m+1)
        {
            if( is_cf[t] == 0 && leng[t] < min ) { min_i = t; min = leng[t]; } 
        }
        //最小の接点を確定
        is_cf[min_i] = 1;
         
        if ( min == M ) { cout << "not連結" << endl; exit(1); }

        //mini_iを加えて最短路の更新
        rep(t, m+1) 
        {
            if( ( leng[min_i] + times[min_i][t] ) < leng[t]  )
            {
                leng[t] = leng[min_i] + times[min_i][t];
            }
        }
    }
    cout << leng[q] << endl;
}

C++: #include<iostream>
#include<algorithm>
const int INF = 1<<20;
const int N = 101;
int cost[N][N];
int tim[N][N];
using namespace std;
int main()
{
	int n, m, a, b, co, ti,r;
	while (1){
		cin >> n >> m;
		if (n == 0 && m == 0)return 0;
		for (int i = 0; i < n; i++){
			for (int i2 = 0; i2 < n; i2++){
				cost[i][i2] = INF;
				tim[i][i2] = INF;
			}
		}
		for (int i = 0; i < n; i++){
			cin >> a >> b >> co >> ti;
			cost[a][b] = co;
			cost[b][a] = co;
			tim[a][b] = ti;
			tim[b][a] = ti;
		}
		for (int k = 0; k < m+1; ++k)
			for (int i = 0; i < m+1; ++i)
				for (int j = 0; j < m+1; ++j)
				{
					cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					tim[i][j] = min(tim[i][j], tim[i][k] + tim[k][j]);
				}
		cin >> m;
		for (int i = 0; i < m; i++){
			cin >> a >> b >> r;
			if (r == 0){
				cout << cost[a][b] << endl;
			}
			else
				cout << tim[a][b] << endl;
		}
	}
}

C++: #include <iostream>
using namespace std;
const int MAX = 100,INF = 1000000;
int edge[MAX + 1][MAX + 1][2],node[MAX + 1];
bool check[MAX + 1];

int f(int a,int b,int c,int m){
	int t,x;
	for(int i = 0;i <= MAX;++i){
		check[i] = false;
		node[i] = INF;
	}
	node[a] = 0;
	while(!check[b]){
		x = INF;
		for(int i = 1;i <= m;++i){
			if(!check[i] && node[i] < x) {
				x = node[i];
				t = i;
			}
		}
		check[t] = true;
		for(int i = 0;i <= m;++i){
			if(node[i] > node[t] + edge[t][i][c]){
				node[i] = node[t] + edge[t][i][c];
			}
		} 
	}
	return node[b];
}
int main(){
	int n,m,a,b,c,d;
	while(cin >> n >> m,n){
		for(int i = 0;i <= MAX;++i){
			for(int j = 0;j <= MAX;++j){
				edge[i][j][0] = edge[j][i][0] = INF;
				edge[i][j][1] = edge[j][i][1] = INF;
			}
		}
		for(int i = 0;i < n;++i){
			cin >> a >> b >> c >> d;
			edge[a][b][0] = edge[b][a][0] = c;
			edge[a][b][1] = edge[b][a][1] = d;
		}
		cin >> n;
		for(int i = 0;i< n;++i){
			cin >> a >> b >> c;
			cout << f(a,b,c,m) << endl;
		}
	}
}