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>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 29)
using namespace std;

struct Data{
    int c, t;
};

void warshallFloyd(Data d[105][105], int m){
    range(k,1,m + 1){
        range(i,1,m + 1){
            range(j,1,m + 1){
                d[i][j].c = min(d[i][j].c, d[i][k].c + d[k][j].c);
                d[i][j].t = min(d[i][j].t, d[i][k].t + d[k][j].t);
            }
        }
    }
}

int main(){
    int n, m;
    Data d[105][105];

    while(cin >> n >> m, n||m){
        rep(i,105) rep(j,105) d[i][j].t = d[i][j].c = INF;
        rep(i,n){
            int a, b, e, f;
            cin >> a >> b >> e >> f;
            d[a][b].c = d[b][a].c = e;
            d[a][b].t = d[b][a].t = f;
        }

        warshallFloyd(d, m);

        int k;
        cin >> k;
        rep(i,k){
            int a, b, c;
            cin >> a >> b >> c;
            if(c) cout << d[a][b].t << endl;
            else cout << d[a][b].c << endl;
        }
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#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>

#define FOR(i,b,n) for(int i=b;i<n;i++)
#define RFOR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))

using namespace std;

static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> paii;

int am[2][500][500] = {0};
int ms[2][500][500] = {0};

int n, m, r;

int solve()
{
	FOR(k,0,2)
		FOR(j,0,m+1)
			FOR(i,0,m+1)
			{
				if( am[k][j][i] != 0 )
					ms[k][j][i] = am[k][j][i];
				else
					ms[k][j][i] = 10000;
			}

	FOR(k, 1, m+1)
		FOR(j, 1, m+1)
			FOR(i, 1, m+1)
			{
				ms[0][j][i] = min(ms[0][j][i], ms[0][j][k] + ms[0][k][i]);
				ms[1][j][i] = min(ms[1][j][i], ms[1][j][k] + ms[1][k][i]);
			}
				
	return 0;
}

int main()
{
	while(cin >> n >> m, (n||m))
	{
		CLR(am);
		CLR(ms);
		
		FOR(i,0,n)
		{
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			
			am[0][a][b] = c;
			am[1][a][b] = t;
			am[0][b][a] = c;
			am[1][b][a] = t;
		}
		
		solve();
		
		cin >> r;
		
		FOR(i,0,r)
		{
			int a, b, t;
			cin >> a >> b >> t;
			cout << ms[t][a][b] << endl;
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

struct edge{
	ll to, cost, time;
	edge(ll _to, ll _cost, ll _time) :to(_to), cost(_cost), time(_time){};
};

ll n, m, a, b, cost, time, k, s, g, f;

ll mincost[1000], mintime[1000];
vector<edge> G[1000];

void dijkstra0(ll start){
	fill(mincost, mincost + 1000, INF);
	mincost[start] = 0;
	priority_queue<P> que;
	que.push(P(0, start));

	while (!que.empty()){
		P p = que.top(); que.pop();
		ll v = p.second;
		if (mincost[v] < p.first)continue;

		for (int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if (mincost[e.to] > mincost[v] + e.cost){
				mincost[e.to] = mincost[v] + e.cost;
				que.push(P(mincost[e.to], e.to));
			}
		}
	}
}

void dijkstra1(ll start){
	fill(mintime, mintime + 1000, INF);
	mintime[start] = 0;
	priority_queue<P> que;
	que.push(P(0, start));

	while (!que.empty()){
		P p = que.top(); que.pop();
		ll v = p.second;
		if (mincost[v] < p.first)continue;

		for (int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if (mintime[e.to] > mintime[v] + e.time){
				mintime[e.to] = mintime[v] + e.time;
				que.push(P(mintime[e.to], e.to));
			}
		}
	}
}

int main(){
	while (cin >> n >> m&&n + m){
		REP(i, n){
			cin >> a >> b >> cost >> time;
			G[a].push_back(edge(b, cost, time));
			G[b].push_back(edge(a, cost, time));
		}
		cin >> k;
		REP(i, k){
			cin >> s >> g >> f;
			if (f == 0){
				dijkstra0(s);
				cout << mincost[g] << endl;
			}
			else{
				dijkstra1(s);
				cout << mintime[g] << endl;
			}
		}
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
const int INF = 111111111;
int main(void){
  int n,m,a,b,cost,time,l,p,q,r;
  int c[105][105],t[105][105];
  for( ; ; ){
    cin >> m >> n;
    if(!m && !n) break;

    for(int i = 1;i <= n; i++){
      for(int j = 1;j <= n; j++){
	if(i == j) c[i][j] = t[i][j] = 0;
	else c[i][j] = t[i][j] = INF;
      }
    }

    for(int i = 0;i < m; i++){
      cin >> a >> b >> cost >> time;
      c[a][b] = c[b][a] = cost;
      t[a][b] = t[b][a] = time;
    }

    for(int k = 1;k <= n; k++)
      for(int i = 1;i <= n; i++)
	for(int j = 1;j <= n; j++) c[i][j] = min(c[i][j], c[i][k] + c[k][j]);

    for(int k = 1;k <= n; k++)
      for(int i = 1;i <= n; i++)
	for(int j = 1;j <= n; j++) t[i][j] = min(t[i][j], t[i][k] + t[k][j]);

    cin >> l;
    for(int i = 0;i < l; i++){
      cin >> p >> q >> r;
      cout << (r ? t[p][q] : c[p][q]) << endl;
    }
  }
}