Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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
					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 <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define FOR(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rep(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define SYAMU(a) memset(a,0,sizeof(a))
#define pb push_back

const int INF = 114514810;
int n,m;
int cost[3000][3000], _time[3000][3000];

void init() {
    rep(i,3000) rep(j,3000) cost[i][j] = _time[i][j] = INF;
}

int main() {
    while(cin>>n>>m && n) {
        init();
        rep(i,m) {
            int a,b,c,t; scanf("%d%d%d%d",&a,&b,&c,&t);
            --a; --b;
            cost[a][b] = cost[b][a] = c;
            _time[a][b] = _time[b][a] = t;
        }
        
        rep(k,n) rep(i,n) rep(j,n) {
            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
            _time[i][j] = min(_time[i][j], _time[i][k] + _time[k][j]);
        }

        int k; cin >> k;
        rep(i,k) {
            int p,q,r;
            cin>>p>>q>>r;
            if(r==0)
                cout << cost[p][q] << "\n";
            else
                cout << _time[p][q] << "\n";
        }
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#define INF (int)1e9

void warshall_floyd(int m, int p, int q, std::vector<std::vector<int> > &dp) { 
    for (int i = 0; i < m; i++) {
        dp[i][i] = 0;
    }
    for (int k = 0; k < m; k++) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                dp[i][j] = std::min(dp[i][j], dp[i][k] + dp[k][j]);
            }
        }
    }
    return;
}

int main(void) {
    int n;
    int m;
    int a;
    int b;
    int cost;
    int time;
    int k;
    int p;
    int q;
    int r;

    while (true) {
        std::cin >> n >> m;
        if (n == 0 && m == 0) {
            break; 
        }
        std::vector<std::vector<int> > vcost(m, std::vector<int>(m, INF));
        std::vector<std::vector<int> > vtime(m, std::vector<int>(m, INF));

        for (int i = 0; i < n; i++) {
            std::cin >> a >> b >> cost >> time;
            --a;
            --b;
            vcost[a][b] = cost; 
            vcost[b][a] = cost; 
            vtime[a][b] = time; 
            vtime[b][a] = time; 
        }

        warshall_floyd(m, p, q, vcost); 
        warshall_floyd(m, p, q, vtime); 
        std::cin >> k;
        for (int i = 0; i < k; i++) {
            std::cin >> p >> q >> r;
            --p;
            --q;
            if (r == 0) {
                std::cout << vcost[p][q] << std::endl;            
            } else {
                std::cout << vtime[p][q] << std::endl;            
            }
        }
    } return 0;
}

C++: #include <bits/stdc++.h>  

using namespace std;

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; -i)
#define INF (1 << 20)
#define MOD 1000000007

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int N, M, K;
int a, b, Cost, Time;
int tTime[101][101];
int tCost[101][101];

int main() {
	while(scanf("%d %d", &N, &M), N||M) {
		FOR(i, 1, M + 1) FOR(j, 1, M + 1) {
			tTime[i][j] = 1000000;
			tCost[i][j] = 1000000;
		}
		rep(i, N) {
			scanf("%d %d %d %d", &a, &b, &Cost, &Time);
			tTime[a][b] = tTime[b][a] = Time;
			tCost[a][b] = tCost[b][a] = Cost;
		}
		FOR(i, 1, M + 1) FOR(j, 1, M + 1) FOR(k, 1, M + 1) {
			tTime[j][k] = min(tTime[j][k], tTime[j][i] + tTime[i][k]);
			tCost[j][k] = min(tCost[j][k], tCost[j][i] + tCost[i][k]);
		}
		scanf("%d", &K);
		rep(i, K) {
			int p, q, r;
			scanf("%d %d %d", &p, &q, &r);
			printf("%d\n", (r) ? tTime[p][q] : tCost[p][q]);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int dp[105][105][2], n, m;

int main(){
    int k;
    int a, b, c, d;
    int st, en, de;

    while(cin >> n >> m, n||m){
        rep(i, 105)
            rep(j, 105)
                rep(k, 2)
                    dp[i][j][k] = 2 << 24;
        rep(i, n){
            cin >> a >> b >> c >> d;
            dp[a][b][0] = dp[b][a][0] = c;
            dp[a][b][1] = dp[b][a][1] = d;
        }
        rep(i, n){
            rep(j, n){
                rep(k, n){
                    dp[i][j][0] = min(dp[i][k][0] + dp[k][j][0], dp[i][j][0]);
                    dp[i][j][1] = min(dp[i][k][1] + dp[k][j][1], dp[i][j][1]);
                }
            }
        }

        cin >> k;
        rep(i, k){
            cin >> st >> en >> de;
            cout << dp[st][en][de] << endl;
        }
    }
}