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<iostream>
using namespace std;
int line[101][101],c[101][101],t[101][101];
int stop[101]={0};
int ls[101]={0}; 
int route_cost(int p,int q){
  int top = -1;
  for(int i=0;i<ls[p];i++){
    //cout << line[p][i] << endl;
    if(stop[line[p][i]]!=0) continue;
    stop[p]++;
    if(q==line[p][i]){
	stop[p] = 0;
	return c[p][q];
   }
int h = route_cost(line[p][i],q)+c[p][line[p][i]];
    if(top<0||top>h){
      top = h+c[p][line[p][i]];
    }
  }
  stop[p] = 0;
  return top;
}
int route_time(int p,int q){
  int top = -1;
  for(int i=0;i<ls[p];i++){
    //cout << "P " << p << endl;
    //cout << line[p][i] << endl;
    if(stop[line[p][i]]!=0) continue;
    stop[p]++;
      if(q==line[p][i]){
	stop[p] = 0;
	 return t[p][q];
      }
int h = route_time(line[p][i],q)+t[p][line[p][i]];
     if(top<0||top>h){
	top = h;
	//cout << 'O' << endl;
      }
  }
  stop[p] = 0;
  return top;
}
int main(){
  int n,m;
  int a,b,cost,time;
  while(cin>>n>>m){
    if(n==0&&m==0) break;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      line[a][ls[a]] = b;
      ls[a]++;
      line[b][ls[b]] = a;
      ls[b]++;
      c[a][b] = cost;
      c[b][a] = cost;
      t[a][b] = time;
      t[b][a] = time;
      //cout << "line " << line[a][ls[a]-1] << ' ' << line[b][ls[b]-1] << endl; 
    }
    int k;
    cin >> k;
    int p,q,r;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      if(r==0) cout << route_cost(p,q) << endl;
      else if(r==1) cout << route_time(p,q) << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#define INF 10e6

using namespace std;

int main(void) {
	int n,m,k,p,q,r;
	int dp[101][101][2];
	while (cin >> n >> m && n && m) {
		for (int i = 0; i < m + 1; ++i) {
			for (int j = 0; j < m + 1; ++j) {
				for (int k = 0; k < 2; ++k) {
					if (i != j) {
						dp[i][j][k] = INF;
					}
					else {
						dp[i][j][k] = 0;
					}
				}
			}
		}

		int a,b;
		for (int i = 0; i < n; ++i) {
			cin >> a >> b >> dp[a][b][0] >> dp[a][b][1];
			dp[b][a][0] = dp[a][b][0];
			dp[b][a][1] = dp[a][b][1];
		}

		for (int mode = 0; mode < 2; ++mode) {
			for (int k = 1; k < m + 1; ++k) {
				for (int i = 1; i < m + 1; ++i) {
					for (int j = 1; j < m + 1; ++j) {
						dp[i][j][mode] = min(dp[i][j][mode], dp[i][k][mode]+dp[k][j][mode]);
					}
				}
			}
		}

		cin >> k;
		for (int i = 0; i < k; ++i) {
			cin >> p >> q >> r;
			cout << dp[p][q][r] << endl;
		}
	}
	return 0;
}

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

#define rep(i,n)	for(int i = 0; i < n; i++)
#define INF 1 << 24

class State
{
public:
	int to,ct[2];	//cost,time
	State(int to, int cost,int time) :
	to(to){ct[0] = cost, ct[1] = time;}

};

// routes, stations
int n,m;
int d[101];

int dijkstra(vector<State> *vec, int p, int q, int r)
{
	queue<State> que;
	que.push(State(p,0,0));	//first position
	fill(d, d+101, INF);
	d[p] = 0;
	

	while(que.size()){
		State sta = que.front();
		que.pop();

		rep(i, vec[sta.to].size()){
			if(d[vec[sta.to][i].to] > vec[sta.to][i].ct[r] + d[sta.to]){
				d[vec[sta.to][i].to] = vec[sta.to][i].ct[r] + d[sta.to];
				que.push(vec[sta.to][i]);
			}
		}
	}
	return d[q];
}


int main(void)
{
	int a,b,cost,time;
	int k;
	int p,q,r;
	while(cin >> n >> m, n){
		vector<State> *vec = new vector<State>[m+1];

		rep(i,n){
			cin >> a >> b >> cost >> time;
			vec[a].push_back(State(b,cost,time));
			vec[b].push_back(State(a,cost,time));
		}
		cin >> k;
		rep(i,k){
			cin >> p >> q >> r;
			cout << dijkstra(vec, p, q, r) << endl;
		}
	}
}

C++: #include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int dp[105][105][2], n, m;

int main(){
    int k;
    int a, b, c, d;
    int st, en, de;
    rep(i, 105)
        rep(j, 105)
        rep(k, 2)
        dp[i][j][k] = 2 << 24;

    while(cin >> n >> m, n||m){
        rep(i, n){
            cin >> a >> b >> c >> d;
            dp[a][b][0] = dp[b][a][0] = c;
            dp[a][b][1] = dp[b][a][1] = d;
        }
        cin >> k;
        rep(i, n){
            rep(j, n){
                rep(k, n){
                    dp[i][j][0] = min(dp[i][k][0] + dp[k][j][0], dp[i][j][0]);
                    dp[i][j][1] = min(dp[i][k][1] + dp[k][j][1], dp[i][j][1]);
                }
            }
        }

        rep(i, k){
            cin >> st >> en >> de;
            cout << dp[st][en][de] << endl;
        }
    }
}