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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int cost[2][101][101];
int n, m;


int main(void){
	while(cin >> n >> m, n|m){
		fill_n(**cost, 2*101*101, 10000000);
		for(int i = 0; i < n; i++){
			int a, b,c,t;
			cin >> a >> b >> c >> t;
			cost[0][a][b] = cost[0][b][a] = c;
			cost[1][a][b] = cost[1][b][a] = t;
		}
		for(int k=0;k<n;++k)for(int i=0;i<n;++i)for(int j=0;j<n;++j){
			cost[0][i][j] = min(cost[0][i][j], cost[0][i][k] + cost[0][k][j]);
			cost[1][i][j] = min(cost[1][i][j], cost[1][i][k] + cost[1][k][j]);
		}
		int k;
		cin >> k;
		rep(i, k){
			int a, b, c;
			cin >> a >> b >> c;
			cout << cost[c][a][b] << endl;
		}
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include <stdio.h>
#include <vector>

using namespace std;

class Edge{
public:
	int from,to,cost,time;
	void swap(){
		int self = from;
		from = to;
		to = self;
	}
	int getvalue(int r){
		if(r == 0){
			return cost;
		}else{
			return time;
		}
	}
};

vector<Edge> edge;
int n,m;

int solve(const int p,const int q,const int r);

int main(){
	edge.clear();
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
		Edge ed;
		scanf("%d %d %d %d",&ed.from,&ed.to,&ed.cost,&ed.time);
		edge.push_back(ed);
		ed.swap();
		edge.push_back(ed);
	}
	
	int k;
	scanf("%d",&k);
	for(int i=0;i<k;i++){
		int p,q,r;
		scanf("%d %d %d",&p,&q,&r);
		printf("%d\n",solve(p,q,r));
	}
}

int solve(const int p,const int q,const int r){
	const int INF = 100000000;
	int a[100];
	for(int i=0;i<100;i++){
		a[i] = INF;
	}
	a[p] = 0;
	bool update;
	while(true){
		update = false;
		vector<Edge>::iterator it = edge.begin();
		while(it != edge.end()){
			if(a[(*it).from] != INF && a[(*it).to] > a[(*it).from] + (*it).getvalue(r)){
				a[(*it).to] = a[(*it).from] + (*it).getvalue(r);
				update = true;
			}
			it++;
		}
		if(!update){
			break;
		}
	}
	
	int ans = a[q];
	return a[q];
}

C++: #include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>

#define NMAX 3000
#define MMAX 100
#define TMAX 1000
#define CMAX 1000
#define KMAX 200

using namespace std;

int main(){
	std::ios_base::sync_with_stdio(false);

	int n,m;
	int a,b,time,cost;
	int data[MMAX*40][MMAX*40][2];
	int k;
	int p,q,r;
	int dp[MMAX*40];
	queue<int> now;

	while(1){
		for(int i=0;i<MMAX*40;i++){
			for(int j=0;j<MMAX*40;j++){
				for(int k=0;k<2;k++){
					data[i][j][k]=-1;
				}
			}
		}

		for(int i=0;i<MMAX*40;i++){
			dp[i]=-1;
		}

		cin>>n>>m;
		if(n==0&&m==0) break;
		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			data[a][b][1]=time;
			data[b][a][1]=time;
			data[a][b][0]=cost;
			data[b][a][0]=cost;
		}

		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;
			now.push(p);
			dp[p]=0;
			while(!now.empty()){
				for(int i=0;i<n;i++){
					if(data[now.front()][i][r]!=-1){
						if(dp[i]==-1||dp[i]>dp[now.front()]+data[now.front()][i][r]){
							dp[i]=dp[now.front()]+data[now.front()][i][r];
							now.push(i);
						}
					}
				}
				now.pop();
			}
			cout<<dp[q]<<endl;
		}

	}



	return 0;
}

C++: #include <iostream>
const int INF = 10000;
using namespace std;

int cost[100][100];
int tim[100][100];

void init()
{
	for(int i=0;i<100;i++){
		for(int j=0;j<100;j++){
			if(i == j){
				cost[i][j] = 0;
				tim[i][j] = 0;
			}else{
				cost[i][j] = INF;
				tim[i][j] = INF;
			}
		}
	}
}

int main()
{
	int n,m;
	while(cin >> n >> m){
		if(n == 0 && m == 0)
			break;
		init();
		int a,b,c,t;
		for(int i=0;i<n;i++){
			cin >> a >> b >> c >> t;
			cost[a-1][b-1] = c;
			cost[b-1][a-1] = c;
			tim[a-1][b-1] = t;
			tim[b-1][a-1] = t;
		}
		
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				for(int k=0;k<m;k++){
					int cdis = cost[j][i] + cost[i][k];
					if(cdis < cost[j][k]){
						cost[j][k] = cdis;
					}
					int tdis = tim[j][i] + tim[i][k];
					if(tdis < tim[j][k]){
						tim[j][k] = tdis;
					}
				}
			}
		}
		
		int n;
		cin >> n;
		int p,q,r;
		for(int i=0;i<n;i++){
			cin >> p >> q >> r;
			if(r == 0){
				cout << cost[p-1][q-1] << endl;
			}else if(r == 1){
				cout << tim[p-1][q-1] << endl;
			}
		}
	}
	return 0;
}