Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

struct edge
{
	int cost, to;
};

void WarshallFroyd(vector<vint>& d)
{
	int n = d.size();

	rep(k, n) rep(i, n) rep(j, n)
		chmin(d[i][j], d[i][k] + d[k][j]);
}

const int INF = 1 << 28;
int main()
{
	int n, m;
	while(cin >> n >> m && (n || m))
	{
		vector<vint> time(m, vint(m, INF)), cost(m, vint(m, INF));

		rep(i, n)
		{
			int s, t, ti, co;
			cin >> s >> t >> co >> ti; s--, t--;

			cost[s][t] = cost[t][s] = co;
			time[s][t] = time[t][s] = ti;
		}

		WarshallFroyd(time);
		WarshallFroyd(cost);

		int k;
		cin >> k;
		rep(i, k)
		{
			int s, t, f;
			cin >> s >> t >> f; s--, t--;

			cout << (f ?time[s][t] : cost[s][t]) << endl;
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define mod 1000000007
#define lol(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
int main(){
	int n,m;
	while(cin>>m>>n&&n+m){
		priority_queue<P,vector<P>,greater<P> >Q;
		vector<PP> v[110];
		int dis[110];
		lol(i,m){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			v[a].push_back(make_pair(b,make_pair(c,d)));
			v[b].push_back(make_pair(a,make_pair(c,d)));
		}
		cin>>m;
		lol(u,m){
			lol(i,n)dis[i+1]=mod;
			int s,t,c;cin>>s>>t>>c;
			Q.push(make_pair(0,s));
			while(!Q.empty()){
				int b=Q.top().first,a=Q.top().second;
				Q.pop();
				if(dis[a]<mod)continue;
				dis[a]=b;
				lol(i,v[a].size()){
					int to=v[a][i].first,co=b+(c?v[a][i].second.second:v[a][i].second.first);
					if(dis[to]==mod)Q.push(make_pair(co,to));
				}
			}
			cout<<dis[t]<<endl;
		}
	}
	return 0;
}

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

const int INF = 1000000000;

int main()
{
	while(true){
		int n, m;
		cin >> n >> m;

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

		int C[100][100];
		int T[100][100];
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++){
				if(i==j){
					C[i][j] = T[i][j] = 0;
				}
				else{
					C[i][j] = T[i][j] = INF;
				}
			}
		}

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

			C[a-1][b-1] = C[b-1][a-1] = c;
			T[a-1][b-1] = T[b-1][a-1] = t;
		}

		/*int N,p,q,r;
		int d[100][100];
		cin >> N;
		for(int I=0;I<N;I++){
			cin >> p >> q >> r;
			if(r){
				for(int i=0;i<m;i++){
					for(int j=0;j<m;j++){
						d[i][j]=T[i][j];
					}
				}
			}else{
				for(int i=0;i<m;i++){
					for(int j=0;j<m;j++){
						d[i][j]=C[i][j];
					}
				}
			}
			for(int k=0;k<m;k++){
				for(int i=0;i<m;i++){
					for(int j=0;j<m;j++){
						if(d[i][j]>d[i][k]+d[k][j]){
							d[i][j]=d[i][k]+d[k][j];
						}
					}
				}
			} 
			cout << d[p-1][q-1] << endl;
		}*/
		
		int k,p,q,r;
		cin >> k;
		for(int I=0;I<k;I++){
			int d[m];
			bool u[m];
			cin >> p >> q >> r;
			for(int i=0;i<m;i++){
				d[i] = INF;
				u[i] = false;
			}
			d[p-1]=0;

			while(1){
				int v = -1;
				for(int i=0;i<m;i++){
					if(u[i])continue;
					if(v==-1||d[i]<d[v]){
						v = i;
					}
				}
				if(v==-1)break;
				u[v]=true;

				for(int i=0;i<m;i++){
					if(r){
						if(d[i]>d[v]+T[v][i]){
							d[i]=d[v]+T[v][i];
						}
					}else{
						if(d[i]>d[v]+C[v][i]){
							d[i]=d[v]+C[v][i];
						}
					}
				}
			}
		
			cout << d[q-1] << endl;
		}		
	}

	return 0;
}

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

int main(){
	vector< vector<short> > cost, time;
	vector<int> station;
	int n, m;
	while( cin >> n >> m, n||m ){
		cost.resize(m+1);
		time.resize(m+1);
		station.resize(m+1);
		for( int i=0; i<m; i++ ){
			cost[i].resize(m+1);
			time[i].resize(m+1);
			for( int j=1; j<=m; j++ ){
				cost[i][j] = time[i][j] = -1;
			}
		}
		station.resize(m);
		for( int i=0; i<n; i++ ){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a][b] = cost[b][a] = c;
			time[a][b] = time[b][a] = t;
		}
		int k;
		cin >> k;
		for( int i=0; i<k; i++ ){
			int p, q, r;
			cin >> p >> q >> r;
			for( int j=1; j<=m; j++ ){
				station[j] = -1;
			}
			station[p] = 0;
			for( int z=0; z<m; z++ ){
				int ny=-1, nextVal=1000000;
				for( int x=1; x<=m; x++ ){
					if( station[x] != -1 ){
						for( int y=1; y<=m; y++ ){
							if( (station[y]==-1) && (cost[x][y] != -1) ){
								int val = station[x];
								if( r==0 )	// cost
									val += cost[x][y];
								else if( r==1 )	// time
									val += time[x][y];
								if( val < nextVal ){
									nextVal = val;
									ny = y;
								}
							}
						}
					}
				}
				station[ny] = nextVal;
				if( ny == q )	break;
			}
			cout << station[q] << endl;
		}
	}	
	return 0;
}