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 <iostream>
#include <vector>
#include <cmath>

#define INF 100000000

using namespace std;

int main()
{
    int n, m;
    while(cin >> n >> m) {
        if(n == 0 && m == 0) break;
        int times[n+1][n+1], costs[n+1][n+1];
        int a,b,cost,time;
        for(int i = 1; i <= n; i++) 
            for(int j = 1; j <= n; j++) 
                times[i][j] = costs[i][j] = (i==j) ? 0 : INF; 
        
        for(int i = 0; i < n; i++) {
            cin >> a >> b >> cost >> time;
            costs[a][b] = costs[b][a] = cost;
            times[a][b] = times[b][a] = time;
        }

        for(int a = 1; a <= n; a++) {
            for(int b = 1; b <= n; b++) {
                for(int c = 1; c <= n; c++) {
                    times[a][b] = min(times[a][b], times[a][c] + times[c][b]);
                    costs[a][b] = min(costs[a][b], costs[a][c] + costs[c][b]);
                }
            }
        }

        int p,q,r,qn;
        cin >> qn;
        for(int i = 0; i < qn; i++) {
            cin >> p >> q >> r;
            if(r == 1) 
                cout << times[p][q] << endl;
            else 
                cout << costs[p][q] << endl;
                     
        }
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)
#define MIN(a, b) ((a)<(b)?(a):(b))
#define MAX(a, b) ((a)<(b)?(a):(b))
#define INF 10e6
using namespace std;
 /*temp*/

int main(){
    int n, m, l;
    while(cin>>m>>n,n){
        int cost[n][n],time[n][n];
        // set table
        REP(i,n){
            REP(j,n){
                cost[i][j]=time[i][j]=INF;
            }
            cost[i][i]=time[i][i]=0;
        }
        // read
        REP(i,n){
            int a,b,c,t;
            a--;
            b--;
            cost[a][b]=cost[b][a]=MIN(c,cost[a][b]);
            time[a][b]=time[b][a]=MIN(t,time[a][b]);
        }
        // Warshall???Floyd Algorithm
        REP(a,n){
            REP(b,a+1){
                REP(k,n){
                    cost[a][b]=cost[b][a]=MIN(cost[a][b],cost[a][k]+cost[k][b]);
                    time[a][b]=time[b][a]=MIN(time[a][b],time[a][k]+time[k][b]);
                }
            }
        }
        // 
        cin>>q;
        REP(i,q){
            int s,d,type;
            cin>>s>>d>>type;
            s--;
            d--;
            cout<<type==0?cost[s][d]:time[s][d]<<'\n';
        }
    }
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };

struct edge
{
	int to, cost;
	edge(int to, int cost)
		: to(to), cost(cost) {}
};
typedef vector<vector<edge> > graph;
void add_edge(graph& g, int a, int b, int cost)
{
	g[a].push_back(edge(b, cost));
	g[b].push_back(edge(a, cost));
}
void add_dir_edge(graph& g, int a, int b, int cost)
{
	g[a].push_back(edge(b, cost));
}
vector<int> dijkstra(const graph& g, int s)
{
	const int _INF = 1e9;
	typedef pair<int, int> pint;

	vector<int> dis(g.size(), _INF);
	priority_queue<pint, vector<pint>, greater<pint> > q;

	dis[s] = 0;
	q.push(pint(0, s));
	while (!q.empty())
	{
		pint t = q.top(); q.pop();
		int cur = t.second, d = t.first;

		if (d > dis[cur])
			continue;

		for (int i = 0; i < g[cur].size(); ++i)
		{
			const edge& e = g[cur][i];
			int next_d = d + e.cost;
			if (next_d < dis[e.to])
			{
				dis[e.to] = next_d;
				q.push(pint(next_d, e.to));
			}
		}
	}

	return dis;
}

int main()
{
	int n, m;
	while (cin >> n >> m, n | m)
	{
		graph cost(m), time(m);
		for (int i = 0; i < n; ++i)
		{
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			--a, --b;

			add_edge(cost, a, b, c);
			add_edge(time, a, b, t);
		}

		vector<int> min_cost[128], min_time[128];
		for (int i = 0; i < m; ++i)
		{
			min_cost[i] = dijkstra(cost, i);
			min_time[i] = dijkstra(time, i);
		}

		int k;
		cin >> k;
		while (k--)
		{
			int p, q, r;
			cin >> p >> q >> r;
			--p, --q;
			int res = r == 0 ? min_cost[p][q] : min_time[p][q];
			cout << res << endl;
		}
	}
}