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 <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;

const int INF = 1<<29;

int n, m, a, b, c, t, k;
int dp[2][100][100];

int main(){
    for(; scanf("%d%d", &n, &m), n||m;){
        for(int x = 0; x < m; x++){
            fill(dp[0][x], dp[0][x]+m, INF);
            fill(dp[1][x], dp[1][x]+m, INF);
        }
        for(int x = 0; x < m; x++){
            dp[0][x][x] = dp[1][x][x] = 0;
        }
        for(int x = 0; x < n; x++){
            scanf("%d%d%d%d", &a, &b, &c, &t);
            --a; --b;
            dp[0][a][b] = c;
            dp[1][a][b] = t;
        }
        for(int z = 0; z < m; z++){
            for(int x = 0; x < m; x++){
                for(int y = 0; y < m; y++){
                    dp[0][x][y] = min(dp[0][x][y],
                            dp[0][x][z] + dp[0][z][y]);
                    dp[1][x][y] = min(dp[1][x][y],
                            dp[1][x][z] + dp[1][z][y]);
                }
            }
        }
        scanf("%d", &k);
        for(int x = 0; x < k; x++){
            scanf("%d%d%d", &a, &b, &c);
            printf("%d\n", dp[c][--a][--b]);
        }
    }
    return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#define M 1000000000
using namespace std;
void Warshall_Floyd();
void GYORETU();
int cc[1001][1001],ti[1001][1001];
int n,m,a,b,cost,time,;
int main(void)
{
	while(1) {
		scanf("%d %d",&n,&m);
		if(n==0&&m==0) break;
		GYORETU();
		Warshall_Floyd();
		int s,g,w,kk;
		scanf("%d",&kk);
		for(int i=0;i<kk;i++) {
			scanf("%d %d %d",&s,&g,&w);
			if(w==0) printf("%d\n",cc[s-1][g-1]);
			else printf("%d\n",ti[s-1][g-1]);
		}
	}
}
void GYORETU()
{
	for(int i=0;i<m;i++) {
		for(int j=0;j<m;j++) {
			cc[i][j]=M;
			ti[i][j]=M;
		}
	}
	for(int i=0;i<m;i++) {
		cc[i][i]=0;
		ti[i][i]=0;
	}
	for(int i=0;i<n;i++) {
		scanf("%d %d %d %d",&a,&b,&cost,&time);
		cc[a][b]=cost;
		cc[b][a]=cost;
		ti[a][b]=time;
		ti[b][a]=time;
	}
}
void Warshall_Floyd()
{
	for(int k=0;k<m;k++) {
		for(int i=0;i<m;i++) {
			for(int j=0;j<m;j++) {
				cc[i][j]=min(cc[i][j],cc[i][k]+cc[k][j]);
			}
		}
	}
	for(int k=0;k<m;k++) {
		for(int i=0;i<m;i++) {
			for(int j=0;j<m;j++) {
				ti[i][j]=min(ti[i][j],ti[i][k]+ti[k][j]);
			}
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <array>
#include <stack>

using namespace std;

int const Inf = 10000000;
int const N = 100;

typedef array<int, 2> line;

int main()
{
    int n, m;
    stack<int> w;

    while( cin >> n >> m, n||m ) {
        line table[N][N] = {};

        for( int i=0; i<n; ++i ) {
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;

            int const from = a-1, to = b-1;
            line const in = {{cost, time}};
            table[from][to] = ( table[to][from] = in );
        }

        int k;
        cin >> k;
        for( int i=0; i<k; ++i ) {
            int p, q, r;
            cin >> p >> q >> r;
            int const from = p-1, to = q-1;

            bool v[N] = {};
            int d[N];
            fill( d, d+N, Inf );

            w.push( from );
            d[from] = 0;

            while( !w.empty() ) {
                int const cur = w.top();
                w.pop();

                for( int ii=0; ii<m; ++ii ) {
                    if ( table[cur][ii][0] == 0 )
                        continue;

                    if ( d[cur] + table[cur][ii][r] < d[ii] ) {
                        d[ii] = d[cur] + table[cur][ii][r];
                        w.push( ii );
                    }

                    //cout << cur+1 << " -> " << ii+1 << " c " << d[ii] << endl;
                }
                

            }

            cout << d[to] << endl;
        }
    }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define MAX 1000000
int main(){
	int n, m, a, b, cost, ti, k, p, q, r, point = 0;
	int cost_[101][101], time_[101][101];
	while ( cin >> n >> m && (n != 0 && m != 0)){
		for (int i = 0; i < m; i++)
		for (int j = 0; j < m; j++) { cost_[i][j] = time_[i][j] = MAX; }
		for (int i = 0; i < n; i++){
			cin >> a >> b >> cost >> ti;
			cost_[a-1][b-1] = cost;
			cost_[b-1][a-1] = cost;
			time_[a-1][b-1] = ti;
			time_[b-1][a-1] = ti;
		}
		for (int i = 0; i < m; i++){
			for (int j = 0; j < m; j++){
				for (int o = 0; o < m; o++){
						cost_[j][o] = min(cost_[j][o], cost_[j][i] + cost_[i][o]);
						time_[j][o] = min(time_[j][o], time_[j][i] + time_[i][o]);
				}
			}
		}cin >> k;
		for (int i = 0; i < k; i++){
			cin >> p >> q >> r;
			if (r != 1) cout << cost_[p-1][q-1] << endl;
			else cout << time_[p-1][q-1] << endl;
		}
	}
	return 0;
}