Go: // Warshall–Floyd Algorithm

package main

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

var scanner = bufio.NewScanner(os.Stdin)

const INF = 2000000000

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++: /* {{{ Shinobu kawaii */
/*
  ______   __        _                  __                 
.' ____ \ [  |      (_)                [  |                
| (___ \_| | |--.   __   _ .--.   .--.  | |.--.   __   _   
 _.____`.  | .-. | [  | [ `.-. |/ .'`\ \| '/'`\ \[  | | |  
| \____) | | | | |  | |  | | | || \__. ||  \__/ | | \_/ |, 
 \______.'[___]|__][___][___||__]'.__.'[__;.__.'  '.__.'_/ 

*/
/* }}} */

#include <bits/stdc++.h>
using namespace std;

// #define int long long
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* cpp template {{{ */

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; std::cin >> x; return x;}
template <typename T> void print(T x) {std::cout << x << '\n';}
template <typename T>
void print(std::vector<T>& v, std::string s = " ") {
  REP(i, v.size()) {
    if (i != 0) std::cout << s;
    std::cout << v[i];
  }
  std::cout << '\n';
}

/* }}} */

int n, m;

void warshall_floyd(vvi& d)
{
  REP(k, m) REP(i, m) REP(j, m) {
    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  }
}

signed main()
{
  while (cin >> n >> m, n || m) {
    vvi dc, dt;
    dc = dt = vvi(m, vi(m, INF));
    REP(i, n) {
      int a = in() - 1, b = in() - 1;
      dc[a][b] = dc[b][a] = in();
      dt[a][b] = dt[b][a] = in();
    }
    warshall_floyd(dc);
    warshall_floyd(dt);
    int k = in();
    REP(i, k) {
      int a = in() - 1, b = in() - 1, r = in();
      print(r ? dt[a][b] : dc[a][b]);
    }
  }

  return 0;
}

C++: #include<iostream>
using namespace std;
int MAX=100000;
int main(){
  while(true){
    int n,m;
    cin>>n>>m;
    int a,b,cost,timer;
    int costs[m][m];
    int timers[m][m];
    for(int i=0;i<m;i++){
      for(int j=0;j<m;j++){
        costs[i][j]=MAX;
        timers[i][j]=MAX;
      }
      costs[i][i]=0;
      timers[i][i]=0;
    }

    for(int i=0;i<n;i++){
      cin>>a>>b>>cost>>timer;
      costs[a-1][b-1]=cost;
      timers[a-1][b-1]=timer;
      costs[b-1][a-1]=cost;
      timers[b-1][a-1]=timer;
    }
    for(int i=0;i<m;i++){
      for(int j=0;j<m;j++){
        for(int k=0;k<m;k++){
          if(costs[j][k]>(costs[j][i]+costs[i][k])){
            costs[j][k]=costs[j][i]+costs[i][k];
            costs[k][j]=costs[j][i]+costs[i][k];
          }
          if(timers[j][k]>(timers[j][i]+timers[i][k])){
            timers[j][k]=timers[j][i]+timers[i][k];
            timers[k][j]=timers[j][i]+timers[i][k];
          }
        }
      }
    }
    int k;
    int p;
    int q;
    bool r;
    cin>>k;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      if(r){
        cout<<timers[p-1][q-1]<<endl;
      }else if(!r){
        cout<<costs[p-1][q-1]<<endl;
      }
    }
  }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int MAX_INT = 10000;
int n, m;
int p, q, r;

int costMap[101][101];
int timeMap[101][101];
int visited[101];

class Node {
public:
	int cost, pos;
	Node(int p) {
		cost = 0;
		pos = p;
	}
	Node(int p, int c, const Node& pre) {
		cost = c + pre.cost;
		pos = p;
	}
	bool operator< (const Node& o) const {
		return cost > o.cost;
	}
};

int main () {
	while (cin >> n >> m, n || m) {
		for (int i = 0; i <= m; i++) {
			fill(costMap[i], costMap[i] + m + 1, MAX_INT);
			fill(timeMap[i], timeMap[i] + m + 1, MAX_INT);
		}
		for (int i = 0; i < n; i++) {
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			costMap[a][b] = costMap[b][a] = cost;
			timeMap[a][b] = timeMap[b][a] = time;
		}
		int k;
		for (cin >> k; k; k--) {
			cin >> p >> q >> r;
			fill(visited, visited + m + 1, MAX_INT);
			priority_queue<Node> que;
			que.push(Node(p));
			visited[p] = 0;
			while (!que.empty()) {
				Node current = que.top();
				que.pop();
				if (current.pos == q) {
					cout << current.pos << ", " << current.cost << endl;
					break;
				}
				for (int i = 1; i <= m; i++) {
					if (r) {
						if (timeMap[current.pos][i] != MAX_INT && timeMap[current.pos][i] + current.cost < visited[i]) {
							visited[i] = timeMap[current.pos][i] + current.cost;
							que.push(Node(i, timeMap[current.pos][i], current));
							cout << current.pos << " to " << i << endl;
						}
					}
					else {
						if (costMap[current.pos][i] != MAX_INT && costMap[current.pos][i] + current.cost < visited[i]) {
							visited[i] = costMap[current.pos][i] + current.cost;
							que.push(Node(i, costMap[current.pos][i], current));
							cout << current.pos << " to " << i << endl;
						}
					}
				}
			}
		}
	}
	return 0;
}