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<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
	while(true) {
		int n, m;
		cin>>n>>m;
		if(n == 0 && m == 0) break;
		vector<vector<pair<ll, P> > > l(m, vector<pair<ll, P> >(0));
		REP(i, n) {
			ll a, b, c, t;
			cin>>a>>b>>c>>t;
			--a;
			--b;
			l[a].pb(pair<ll, P>(b, P(c, t)));
			l[b].pb(pair<ll, P>(a, P(c, t)));
		}
		vector<vector<ll> > d0(m, vector<ll>(m, INF));
		vector<vector<ll> > d1(m, vector<ll>(m, INF));
		REP(i, m) {
			REP(j, l[i].size()) {
				d0[i][l[i][j].first] = l[i][j].second.first;
				d0[l[i][j].first][i] = l[i][j].second.first;
				d1[i][l[i][j].first] = l[i][j].second.second;
				d1[l[i][j].first][i] = l[i][j].second.second;
			}
		}
		REP(k, m) {
			REP(i, m) {
				REP(j, m) {
					d0[i][j] = min(d0[i][j], d0[i][k] + d0[k][j]);
					d1[i][j] = min(d1[i][j], d1[i][k] + d1[k][j]);
				}
			}
		}
		int k;
		cin>>k;
		REP(i, k) {
			int p, q, r;
			cin>>p>>q>>r;
			--p;
			--q;
			if(r == 0) {
				cout<<d0[p][q]<<endl;
			} else {
				cout<<d1[p][q]<<endl;
			}
		}
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

const int MAX=100;
const int INF=(1<<21);

int main(){

  int n, m;
  int a, b, cost, time;
  int p, q, r;
  int rail_cost[MAX][MAX];
  int rail_time[MAX][MAX];
  int k;
  while( cin >> n >> m, n+m ) {
 
    
    for(int i=0; i<m; i++) {
      for(int j=0; j<m; j++) {
	rail_cost[i][j] = INF;
	rail_time[i][j] = INF;
      }
    }
    
    for(int i=0; i<n; i++) {
      cin >> a >> b >> cost >> time; 
      rail_cost[a-1][b-1] = rail_cost[b-1][a-1] = cost;
      rail_time[a-1][b-1] = rail_time[b-1][a-1] = time;
    }
    
    for(int i=0; i<m; i++) {
      for(int j=0; j<m; j++) {
	for(int k=0; k<m; k++) {
	  rail_cost[j][k] = min(rail_cost[j][k], rail_cost[j][i]+rail_cost[i][k]);
	  rail_time[j][k] = min(rail_time[j][k], rail_time[j][i]+rail_time[i][k]);
	}
      }
    }
    
    cin >> k;
    for(int i=0; i<k; i++) {
      cin >> p >> q >> r;
      if( !r ) {
	cout << rail_cost[p-1][q-1] << endl;
      } 
      else {
	cout << rail_time[p-1][q-1] << endl;
      }
    }
  }
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
const int INF = INT_MAX/3;
const int MAXN = 2000;
const int TIME = 1,COST = 0;

int graph[2][MAXN][MAXN];

int n,m,a,b,c,t;

void solve();

void init(){
  for(int i = 0;i < m;i++)
    for(int j = 0;j < m;j++)
      graph[0][i][j] =graph[0][j][i] = graph[1][j][i] = graph[1][i][j] = INF;
}
int main(){
  cin >> n >> m;
  init();
  for(int i = 0;i < n;i++){
    cin >> a >> b >> c >> t;
    graph[TIME][a-1][b-1] = graph[TIME][b-1][a-1] = t;
    graph[COST][a-1][b-1] = graph[COST][b-1][a-1] = c;
  }
  solve();

  int X;
  cin >> X;
  for(int i = 0;i < X;i++){
    cin >> a >> b >> c;
    cout << graph[c][a-1][b-1] << endl;
  }
  return 0;
}

void solve(){
  for(int k = 0;k < m;k++){
    for(int i = 0;i < m;i++){
      for(int j = 0;j < m;j++){
	graph[0][i][j] = min(graph[0][i][j],graph[0][i][k] + graph[0][k][j]);
	graph[1][i][j] = min(graph[1][i][j],graph[1][i][k] + graph[1][k][j]);
      }
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T1, class T2> inline void minup(T1& m, T2 x){ if(m>x) m=static_cast<T2>(x); }
template<class T1, class T2> inline void maxup(T1& m, T2 x){ if(m<x) m=static_cast<T2>(x); }

#define INF 1000000000LL
#define MOD 1000000009
#define EPS 1E-9

const int MAX_V = 1000;
ll dp[2][MAX_V][MAX_V];
int E, V;
int a, b;
ll c, t;
int k;
int p, q, r;

int main()
{
  //  while((cin >> E >> V) && E && V){
  while(scanf(" %d %d", &E, &V) && E && V){
    rep(i, V) rep(j, V) dp[0][i][j] = dp[1][i][j] = INF;

    rep(i, E){
      //      cin >> a >> b >> c >> t;
      scanf(" %d %d %d %d", &a, &b, &c, &t);
      a-=1;  b-=1;
      dp[0][a][b] = dp[0][b][a] = c;
      dp[1][a][b] = dp[0][b][a] = t;
    }

    rep(b, 2) rep(k, V) rep(i, V) rep(j, V) minup(dp[b][i][j], dp[b][i][k] + dp[b][k][j]);

    //    cin >> k;
    scanf(" %d", &k);
    while(k--){
      //      cin >> p >> q >> r;
      scanf(" %d %d %d", &p, &q, &r);
      //      cout << dp[r][p-1][q-1] << endl;
      printf("%lld\n", dp[r][p-1][q-1]);
    }
  }

  return 0;
}