Go: // Warshall–Floyd Algorithm

package main

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

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
				} else {
					c_dist[i][j] = c_matrix[i][j]
				}
				if c_matrix[i][j] == 0{
					c_prev[i][j] = -1
				} else {
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0{
					t_dist[i][j] = INF
				} else {
					t_dist[i][j] = t_matrix[i][j]
				}
				if t_matrix[i][j] == 0{
					t_prev[i][j] = -1
				} else {
					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>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define vi vector<int>
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const ll int INF = 1e9;
ll int ti[101][101];//time
ll int co[101][101];//cost
int n,m;
int main(){
	while(1){
		cin >> n >> m; 
		if(n == 0 && m == 0) break;
		REP(i,101){
			REP(j,101){
				if(i == j){
					ti[i][i] = co[i][i] = 0;
				}else{
					ti[i][j] = co[i][j] = INF;
				}
			}
		}
		REP(i,n){
			ll int a,b,cost,tim;
			cin >> a >> b >> cost >> tim;
			a--; b--;
			co[a][b] = co[b][a] = cost;
			ti[a][b] = ti[b][a] = tim;
		}
		//Warshall???Floyd Algorithm
		REP(i,m){
			REP(j,m){
				REP(k,m){
					ti[i][j] = min(ti[i][j], ti[i][k] + ti[k][j]);
					co[i][j] = min(co[i][j], co[i][k] + co[k][j]);
				}
			}
		}
		int k;
		cin >> k;
		REP(i,k){
			int a,b,r;
			cin >> a >> b >> r;
			a--; b--;
			if(r == 0) cout << co[a][b] << endl;
			else cout << ti[a][b] << endl;
		}
	}
	return 0;
}

C++: // 2014/06/16 Tazoe

#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;
		}
//???????????§????????£?????????
//????????????
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(C[i][j]>C[i][k]+C[k][j]){
						C[i][j]=C[i][k]+C[k][j];
					}
				}
			}
		}
//????????????
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(T[i][j]>T[i][k]+T[k][j]){
						T[i][j]=T[i][k]+T[k][j];
					}
				}
			}
		}
//???????????????
		int k;
		cin>>k;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin>>p>>q>>r;
			if(r==0)cout<<C[p-1][q-1]<<endl;
			if(r==1)cout<<T[p-1][q-1]<<endl;
		
		}
	}

	return 0;
}

C++: #include <climits>
#include <iostream>
#include <vector>

struct Edge {
    Edge(int f, int t, int c, int tm) {
        from = f;
        to = t;
        cost[0] = c;
        cost[1] = tm;
    }
    int from, to, cost[2];
};

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);

    std::vector<Edge> edges;
    int n, m, k;
    while (std::cin >> n >> m && n != 0 && m != 0) {
        for (int i = 0; i < n; i++) {
            int from, to, cost, time;
            std::cin >> from >> to >> cost >> time;
            edges.push_back(Edge(from - 1, to - 1, cost, time));
        }

        std::cin >> k;
        for (int i = 0; i < k; i++) {
            auto sts = new int[m]();
            auto used = new bool[m]();
            for (int j = 0; j < m; j++) {
                sts[j] = INT_MAX;
                used[j] = false;
            }

            int start, end, query;
            std::cin >> start >> end >> query;

            int cur = start - 1;
            sts[cur] = 0;
            used[cur] = true;
            while (!used[end - 1]) {
                for (auto e : edges) {
                    if (e.to == cur && sts[e.from] > sts[cur] + e.cost[query])
                        sts[e.from] = sts[cur] + e.cost[query];
                    if (e.from == cur && sts[e.to] > sts[cur] + e.cost[query])
                        sts[e.to] = sts[cur] + e.cost[query];
                }
                for (int j = 0; j < m; j++) std::cout << sts[j] << ' ';
                std::cout << std::endl;

                int min_i, min_c = INT_MAX;
                for (int j = 0; j < m; j++)
                    if (!used[j] && min_c > sts[j]) {
                        min_i = j;
                        min_c = sts[j];
                    }
                used[min_i] = true;
                cur = min_i;
            }
            std::cout << sts[end - 1] << std::endl;
            delete[] sts;
            delete[] used;
        }
        edges.clear();
    }
}

C++: #include<stdio.h>
#define INF 10000000
int e1[100][100],e2[100][100];
int d[100];
bool used[100];
int m,n;
int p,q,r;
int k;
int i,j;
int a,b,t,c;
int v;
int mc,mt;

int min(int x,int y){
	if(x<=y)return x;
	else return y;
}

void solve(int s,int mo){
	for(j=0;j<m;j++){
		used[j]=false;
		d[j]=INF;
	}
	d[s]=0;
	while(true){
		v=-1;
		for(int u=0;u<m;u++)if(!used[u]&&(v==-1||d[u]<d[v]))v=u;
		if(v==-1)break;
		used[v]=true;
		for(int u=0;u<m;u++){
			if(mo==0)d[u]=min(d[u],d[v]+e1[v][u]);
			if(mo==1)d[u]=min(d[u],d[v]+e2[v][u]);
		}
	}
}


int main(){
	while(1){
		scanf("%d %d",&n,&m);
		if(n==0&&m==0)break;
		mt=INF;
		mc=INF;
		for(i=0;i<m;i++)for(j=0;j<m;j++){
			e1[i][j]=INF;
			e2[i][j]=INF;
		}
		for(i=0;i<n;i++){
			scanf("%d %d %d %d",&a,&b,&c,&t);
			e1[a-1][b-1]=c;
			e1[b-1][a-1]=c;
			e2[a-1][b-1]=t;
			e2[b-1][a-1]=t;
		}
		scanf("%d",&k);
		for(i=0;i<k;i++){
			scanf("%d %d %d",&p,&q,&r);
			solve(p-1,r);
			if(r==0)mc=min(mc,d[q-1]);
			if(r==1)mt=min(mt,d[q-1]);
		}
		printf("%d\n%d\n",mc,mt);
	}
}