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<algorithm>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << endl

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

template<typename T>
bool chmax(T &a, T b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmin(T &a, T b){
    if(b < a){
        a = b;
        return true;
    }
    return false;
}

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

    int n, m;
    while(cin >> n >> m, n){
        vector< vector<lint> > c(m, vector<lint>(m, LLINF)), t(m, vector<lint>(m, LLINF));
        rep(i, m){
            c[i][i] = 0;
            t[i][i] = 0;
        }

        rep(i, n){
            int a, b;
            lint co, ti;
            cin >> a >> b >> co >> ti;
            chmin(c[--a][--b], co);
            chmin(c[b][a], co);
            chmin(t[a][b], ti);
            chmin(t[b][a], ti);
        }

        rep(k, m){
            rep(i, m){
                rep(j, m){
                    chmin(c[i][j], c[i][k] + c[k][j]);
                    chmin(t[i][j], t[i][k] + t[k][j]);
                }
            }
        }

        int query;
        cin >> query;
        while(query--){
            int p, q, r;
            cin >> p >> q >> r;
            --p;
            --q;
            if(r == 0){
                cout << c[p][q] << endl;
            }else{
                cout << t[p][q] << endl;
            }
        }
    }

    return 0;
}

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

const int INF = 1<<20;

int GM[100][100];
int GT[100][100];

int main() {
  
  int n, m, a, b, c, t;
  int K, p, q, r;
  
  while(cin >> n >> m, n+m) {
    fill(GM[0], GM[100], INF);
    fill(GT[0], GT[100], INF);
    for(int i=0; i<m; i++) {
      GM[i][i] = 0;
      GT[i][i] = 0;
    }
    for(int i=0; i<n; i++) {
      cin >> a >> b >> c >> t; a--, b--;
      GM[a][b] = c, GM[b][a] = c;
      GT[a][b] = t, GT[b][a] = t;
    }
    
    for(int k=0; k<m; k++)
      for(int i=0; i<m; i++)
	for(int j=0; j<m; j++) {
	  GM[i][j] = min(GM[i][j], GM[i][k] + GM[k][j]);
	  GT[i][j] = min(GT[i][j], GT[i][k] + GT[k][j]);
	}
    
    cin >> K;
    for(int i=0; i<K; i++) {
      cin >> p >> q >> r; p--, q--;
      if(r) {
	cout << GT[p][q] << endl;
      } else {
	cout << GM[p][q] << endl;
      }
    }
  }
  
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
int cost[100][100],time[100][100];
int n,m;
void Min(int cost[100][100]) {
	for(int k=0;k<m;k++) {
		for(int i=0;i<m;i++) {
			for(int j=0;j<m;j++) {
				cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
			}
		}
	}
}
int main() {
	while(scanf("%d %d",&n,&m),n) {
		for(int i=0;i<m;i++) {
			for(int j=0;j<m;j++)  {
				if(i!=j)cost[i][j]=time[i][j]=1<<27;
				else cost[i][j]=time[i][j]=0;
			}
		}
		int a,b;
		for(int i=0;i<n;i++) {
			scanf("%d %d",&a,&b);
			scanf("%d %d",&cost[a-1][b-1],&time[a-1][b-1]);
			cost[b-1][a-1]=cost[a-1][b-1];
			time[b-1][a-1]=time[b-1][a-1];
		}
		Min(cost);Min(time);
		int k,p,q,r;
		scanf("%d",&k);
		for(int i=0;i<k;i++) {
			scanf("%d %d %d",&p,&q,&r);
			if(r)printf("%d\n",time[p-1][q-1]);
			else printf("%d\n",cost[p-1][q-1]);
		}
	}
}

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