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
			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;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename Q_temp>
using smaller_queue = priority_queue <Q_temp, vector<Q_temp>, greater<Q_temp> >;

const ll MOD = (ll) 998244353;
const int INF = (int) 1e9;
const ll LINF = (ll) 4e18;
const double PI = acos(-1.0);

#define REP(i,m,n) for(ll i = m; i < (ll)(n); ++i)
#define rep(i,n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbg2(x_) for(auto a_ : x_) cerr << a_ << " "; cerr << endl;
#define dbg3(x_ , sx_) rep(i, sx_) cerr << x_[i] << " "; cerr << endl;
vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

inline ll CEIL(ll a, ll b) {
    return (a + b - 1) / b;
}

void finish() {
    cout << -1 << endl;
    exit(0);
}

//------------------------------------------------------
vector<pll> v;
vector<ll> va, vb, sva, svb;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20) << setiosflags(ios::fixed);

    int n, m;

    while (cin >> n >> m) {
        if (n == 0 && m == 0) break;

        int cost[110][110], time[110][110];

        rep(i, 110) rep(j, 110) {
            cost[i][j] = time[i][j] = (i == j ? 0 : INF);
        }

        rep(i, n) {
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            cost[a][b] = cost[b][a] = c;
            time[a][b] = time[b][a] = t;
        }

        rep(k, 110) rep(i, 110) rep(j, 110) {
            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] << endl;
            else cout << time[p][q] << endl;
        }
    }

    return 0;
}

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

using namespace std;

int main() {
	int n, m, k;

	int costs[100][100];
	int times[100][100];

	while(cin >> n >> m ) {

		if (n == 0 && m == 0) return 0;

		for(int i = 0; i < 100; i++) {
			for(int j = 0; j < 100; j++) {
				costs[i][j] = 1e5;
				times[i][j] = 1e5;
			}
		}

		for(int i = 0; i < n; i++) {
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			a -= 1;
			b -= 1;
			costs[a][b] = costs[b][a] = cost;
			times[a][b] = times[b][a] = time;
		}

		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				for(int k = 0; k < n; k++) {
					costs[j][k] = min(costs[j][k], costs[j][i] + costs[i][k]);
					times[j][k] = min(times[j][k], times[j][i] + times[i][k]);
				}
			}
		}

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

C++: #include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int INFTY=1<<29;

struct Edge{
	int src,dst,weight;
	Edge(){}
	Edge(int s,int d,int w):src(s),dst(d),weight(w){}
	bool operator<(const Edge& e)const{return weight<e.weight;}
	bool operator>(const Edge& e)const{return weight>e.weight;}
};
typedef vector<vector<Edge> > Graph;

void dijkstra(const Graph& g,int start,vector<int>& dist)
{
	priority_queue<Edge,vector<Edge>,greater<Edge> > pq;
	pq.push(Edge(-1,start,0));
	while(pq.size()){
		Edge cur=pq.top(); pq.pop();
		if(dist[cur.dst]!=INFTY) continue;
		dist[cur.dst]=cur.weight;
		for(int i=0;i<g[cur.dst].size();i++){
			Edge e=g[cur.dst][i];
			if(dist[e.dst]!=INFTY) continue;
			pq.push(Edge(e.src,e.dst,cur.weight+e.weight));
		}
	}
}

int main()
{
	for(int n,m;scanf("%d%d",&n,&m),n|m;){
		Graph g1(m),g2(m);
		for(int i=0;i<n;i++){
			int a,b,cost,time; scanf("%d%d%d%d",&a,&b,&cost,&time);
			a--; b--;
			g1[a].push_back(Edge(a,b,cost));
			g1[b].push_back(Edge(b,a,cost));
			g2[a].push_back(Edge(a,b,time));
			g2[b].push_back(Edge(b,a,time));
		}
		vector<vector<int> > w1(m,vector<int>(m,INFTY)),w2=w1;
		for(int i=0;i<m;i++){
			dijkstra(g1,i,w1[i]);
			dijkstra(g2,i,w2[i]);
		}
		int k; scanf("%d",&k);
		for(int i=0;i<k;i++){
			int p,q,r; scanf("%d%d%d",&p,&q,&r);
			if(r==0)
				printf("%d\n",w1[p-1][q-1]);
			else
				printf("%d\n",w2[p-1][q-1]);
		}
	}
	
	return 0;
}