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 <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; }
            else 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;

    repl(j,1,m+1)
    {
        int min = M;
        int min_i;
        //最小の接点を探す
        //rep(t,m+1)
        repl(t,1,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); }

        //rep(t, m+1) 
        repl(t,1, m+1) 
        {
            
            if( ( leng[min_i] + costs[min_i][t] ) < leng[t]  )
            {
                leng[t] = leng[min_i] + costs[min_i][t];
            }
        }
    }
    return  leng[q];
}

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)
        repl(t,1,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) 
        repl(t,1, m+1) 
        {
            if( ( leng[min_i] + times[min_i][t] ) < leng[t]  )
            {
                leng[t] = leng[min_i] + times[min_i][t];
            }
        }
    }
    return leng[q];
}

C++: #include <iostream>
using namespace std;
int T[110][110];
int C[110][110];
int main()
{
    int n,m;
    while(cin>>n>>m,n){
        for(int i=0;i<m;++i){
            for(int j=0;j<m;++j){
                if(i==j){
                    T[i][j]=0;
                    C[i][j]=0;
                }else{
                    T[i][j]=100000;
                    C[i][j]=100000;
                }
            }
        }
        for(int i=0;i<n;++i){
            int a,b,c,t;
            cin>>a>>b>>c>>t;
            a--;b--;
            C[a][b]=c;
            C[b][a]=c;
            T[a][b]=t;
            T[b][a]=t;
        }
        for(int k=0;k<m;++k){
            for(int i=0;i<m;++i){
                for(int j=0;j<m;++j){
                    C[i][j]=min(C[i][j],C[i][k]+C[k][j]);
                    T[i][j]=min(T[i][j],T[i][k]+T[k][j]);
                }
            }
        }
        int k;
        cin>>k;
        for(int i=0;i<k;++i){
            int p,q,r;
            cin>>p>>q>>r;
            p--;q--;
            if(!r)
                cout<<C[p][q]<<endl;
            else
                cout<<T[p][q]<<endl;
        }
    }
    return 0;
}