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 <vector>
#include <queue>

using namespace std;

struct Edge {
	int to, cost, time;
	Edge(int _t, int _c, int _ti) : to(_t), cost(_c), time(_ti) {}
};

bool operator > (const Edge& e1, const Edge& e2) {
	if (e1.cost > e2.cost) return true;
	if (e1.cost == e2.cost) {
		if (e1.time > e2.time) return true;
		if (e1.to > e2.to) return true;
	}
	return false;
}

int n, m, k;
vector< vector<Edge> > G;

int INF = (int)1e9;
int dist[110];

int dks(int s, int t, int r) {
	fill(dist, dist + 110, INF);
	
	priority_queue<Edge, vector<Edge>, greater<Edge> > que;
	que.push(Edge(s, 0, 0));
	
	while (!que.empty()) {
		Edge e1 = que.top(); que.pop();
		
		for (int i = 0; i < (int)G[e1.to].size(); i++) {
			Edge e2 = G[e1.to][i];
			
			if (r%2) {
				if (dist[e2.to] > e1.time + e2.time) {
					dist[e2.to] = e1.time + e2.time;
					que.push(Edge(e2.to, 0, e1.time + e2.time));
				}
			} else {
				if (dist[e2.to] > e1.cost + e2.cost) {
					dist[e2.to] = e1.cost + e2.cost;
					que.push(Edge(e2.to, e1.cost + e2.cost, 0));
				}
			}
		}
	}
	
	return dist[t];
}

int main() {
	while (1) {
		cin >> n >> m; if (n==0 && m==0) break;
		
		G.assign(n, vector<Edge>());
		
		for (int i = 0; i < n; i++) {
			int a, b, c, t; cin >> a >> b >> c >> t;
			a--; b--;
			G[a].push_back(Edge(b, c, t));
			G[b].push_back(Edge(a, c, t));
		}
		
		cin >> k;
		
		for (int i = 0; i < k; i++) {
			int p, q, r; cin >> p >> q >> r;
			p--; q--;
			cout << dks(p, q, r) << endl;
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <iterator>
#include <queue>
#include <stack>
#include <deque>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <complex>
#include <functional>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdlib>
#include <cctype>

#define REP(i,m,n) for(int i=m;i<(int)n;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rp(i,c) rep(i,(int)c.size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(u) (u).rbegin(),(u).rend()
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl
#define v_delete(a,b) (a).erase(remove((a).begin(), (a).end(), b), (a).end())
#define v_unique(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define VV(T) vector<vector< T > >
#define init(a,b) memset((a), (b), sizeof((a)))
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int inf=1<<28;
const double INF=1e10,EPS=1e-9;


template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}

template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }

int in(){
	int t;
	cin >> t;
	return t;
}

int main()
{
	int n,m;
	while(cin >> n >> m,(n&&m)){
        int d[2][100][100];
        for(int i=0;i<2;i++){
            for(int j=0;j<100;j++){
                for(int k=0;k<100;k++){
                	if(j==k){
                        d[i][j][k]=0;
                	}else{
                		d[i][j][k]=inf;
                	}
                }
            }
        }
        for(int i=0;i<n;i++){
            int a,b,cost,time;
            cin >> a >> b >> cost >> time;
            a--;
            b--;
            d[0][a][b]=cost;
            d[0][b][a]=cost;
            d[1][a][b]=time;
            d[1][b][a]=time;
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
                for(int k=0;k<m;k++){
                    for(int o=0;o<2;o++){
                        d[o][j][k] = min(d[o][j][k],d[o][i][j]+d[o][i][k]);
                    }
                }
            }
        }
        int k;
        cin >> k;
        for(int i=0;i<k;i++){
            int p,q,r;
            cin >> p >> q >> r;
            p--;q--;
            cout << d[r][p][q] << endl;
        }
	}
	return 0;
}

C++: #include <climits>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define min(a, b) (a > b ? b : a)

int main() {
    int m, n;
    while (std::cin >> m >> n && m != 0 && n != 0) {
        int tb[n][n][2];

        rep(i, n) rep(j, n) tb[i][j][0] = tb[i][j][1] = tb[j][i][0] =
            tb[j][i][1] = (i == j ? 0 : INT_MAX / 3);
        rep(i, m) {
            int f, t, c, tm;
            std::cin >> f >> t >> c >> tm;
            f--;
            t--;
            tb[f][t][0] = tb[t][f][0] = c;
            tb[f][t][1] = tb[t][f][1] = tm;
        }
        rep(x, n) rep(y, x) rep(z, n) rep(i, 2) tb[z][y][i] = tb[y][z][i] =
            min(tb[y][z][i], tb[y][x][i] + tb[x][z][i]);

        int k;
        std::cin >> k;
        rep(i, k) {
            int s, e, q;
            std::cin >> s >> e >> q;
            s--;
            e--;
            std::cout << tb[s][e][q] << std::endl;
        }
    }
}