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 <iostream>
#include <algorithm>
#include <array>
#include <queue>

using namespace std;

int const Inf = 10000000;
int const N = 100;

typedef array<int, 2> line;

int main()
{
    int n, m;
    while( cin >> n >> m, n||m ) {
        line table[N][N] = {};

        for( int i=0; i<n; ++i ) {
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;

            int const from = a-1, to = b-1;
            line const in = {{cost, time}};
            table[from][to] = ( table[to][from] = in );
        }

        int k;
        cin >> k;
        for( int i=0; i<k; ++i ) {
            int p, q, r;
            cin >> p >> q >> r;
            int const from = p-1, to = q-1;

            bool v[N][N] = {};
            int d[N];
            fill( d, d+N, Inf );

            priority_queue<int, vector<int>, greater<int>> w;
            w.push( from );
            d[from] = 0;

            while( !w.empty() ) {
                int const cur = w.top();
                w.pop();

                for( int ii=0; ii<m; ++ii ) {
                    if ( table[cur][ii][0] == 0 || v[ii][cur] || v[cur][ii] )
                        continue;

                    if ( d[cur] + table[cur][ii][r] < d[ii] ) {
                        d[ii] = d[cur] + table[cur][ii][r];
                        v[cur][ii] = true;
                        w.push( ii );
                    }

//                    cout << cur+1 << " -> " << ii+1 << " c " << d[ii] << endl;
                }
            }

            cout << d[to] << endl;
        }
    }
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back


int n,m;

int C[200][200];
int T[200][200];
int d[200];


int dijk(int s,int g,int r){
    bool used[200];
    rep(i,200)used[i]=false;
    rep(i,200)d[i]=999999999;
    
    d[s]=0;
    
    while(1){
        int v=-1;
        rep(u,n){
            if( !used[u]&&(v==-1||d[u]<d[v]) )v=u;
        }
        if(v==-1)break;
        
        used[v]=true;
        
        rep(u,n){
            if(r==0)
                d[u] = min(d[u],d[v]+C[v][u]);
            else
                d[u] = min(d[u],d[v]+T[v][u]);
        }
    }
    return d[g];
}

int main(){
    while(cin>>n>>m){
        if(n==0&&m==0)break;
        
        rep(i,200)rep(j,200){
            C[i][j]=999999999;
            T[i][j]=999999999;
        }
        rep(i,n){
            int a,b,c,t;
            cin>>a>>b>>c>>t;
            a--,b--;
            C[a][b]=c;
            C[b][a]=c;
            T[a][b]=t;
            T[b][a]=t;
        }
        
        int k;
        cin>>k;
        rep(i,k){
            int s,g,r;
            cin>>s>>g>>r;
            s--,g--;
            cout<<dijk(s,g,r)<<endl;
        }
    }
}

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;
      }
    }
  }
}