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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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>
#include <map>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef pair<int,int> P;

const int MAX_V = 101;
const int INF = 1000000009;
struct edge { int to, cost, time; };
vector<edge> G[MAX_V];
int dist[MAX_V];

int dijkstra(int s, int t, int type) {
	fill(dist, dist+MAX_V, INF);
	dist[s] = 0;
	priority_queue<P, vector<P>, greater<P> > que;
	que.push(P(0, s));
	while(!que.empty()) {
		P n = que.top(); que.pop();
		int v = n.second;
		if(v == t) return dist[v];

		for(int i=0; i<G[v].size(); i++) {
			edge& e = G[v][i];
			if(type == 0) {
				if(dist[e.to] > dist[v] + e.cost) {
					dist[e.to] = dist[v] + e.cost;
					que.push(P(dist[e.to], e.to));
				}
			} else {
				if(dist[e.to] > dist[v] + e.time) {
					dist[e.to] = dist[v] + e.time;
					que.push(P(dist[e.to], e.to));
				}
			}
		}
	}
	return INF;
}

void add_edge(int from, int to, int cost, int time) {
	edge e1 = { to, cost, time };
	edge e2 = { from, cost, time };
	G[from].push_back(e1);
	G[to].push_back(e2);
}

int M, N, K;
int a, b, c, t;
int p, q, r;

int main() {
	while(scanf("%d%d", &N, &M), N|M) {
		for(int i=0; i<MAX_V; i++) G[i].clear();

		for(int i=0; i<N; i++) {
			scanf("%d%d%d%d", &a, &b, &c, &t);
			add_edge(a, b, c, t);
		}

		scanf("%d", &K);
		for(int i=0; i<K; i++) {
			scanf("%d%d%d", &p, &q, &r);
			int d = dijkstra(p, q, r);
			printf("%d\n", d);
		}
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int rp;
	rp = 0;
	int ans[111][333];
	int f[1111], aa, bb, cc;
	int n,a,b,c,d, time[111][111],m, cost[111][111];
	while (1) {
		cin >> n >> m;
		if (n == 0 && m == 0) {
			goto a;
		}
		rp = rp + 1;
		for (int i = 1; i <= m; i++) {
			for (int z = 1; z <= m; z++) {
				if (z == i) {
					time[z][i] = 0;
					time[i][z] = 0;
					cost[i][z] = 0;
					cost[z][i] = 0;
				}
				else {
					time[z][i] = 9999;
					time[i][z] =9999;
					cost[i][z] = 9999;
					cost[z][i] = 9999;
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			cin >> a >> b >> c >> d;
			cost[a][b] = c;
			cost[b][a] = c;
			time[a][b] = d;
			time[b][a] = d;
		}
		for (int i = 1; i <= m; i++) {
			for (int z = 1; z <= m; z++) {
				for (int s = 1; s <= m; s++) {
					cost[z][s] = min(cost[z][s], cost[z][i] + cost[i][s]);
					time[z][s] = min(time[z][s], time[z][i] + time[i][s]);
				}
			}
		}
		cin >> f[rp];
		for (int i = 1; i <= f[rp]; i++) {
			cin >> aa >> bb >> cc;
			if (cc == 1) {
				ans[rp][i] = time[aa][bb];
			}
			else {
				ans[rp][i] = cost[aa][bb];
			}
		}
	}
a:;
	for (int z = 1; z <= rp; z++) {
		for (int i = 1; i <= f[z]; i++) {
			cout << ans[z][i] << endl;
		}
	}
}

C++: # include <iostream>
# include <algorithm>

# define INF 1e6
# define MAX_V 100

int e, v, K;

int d[2][MAX_V][MAX_V];

int main(){
	while (1){
		for (int i = 0; i < v; ++i){
			for (int j = 0; j < v; ++j){
				d[0][i][j] = (i == j) ? 0 : INF;
				d[1][i][j] = (i == j) ? 0 : INF;
			}
		}
		int a, b, cost, time;
		std::cin >> e >> v;
		if ((e + v) == 0)
			break;
		for (int i = 0; i < e; ++i){
			std::cin >> a >> b >> cost >> time;
			d[0][a][b] = cost;
			d[0][b][a] = cost;
			d[1][a][b] = time;
			d[1][b][a] = time;
		}
		for (int k = 0; k < v; ++k){
			for (int i = 0; i < v; ++i){
				for (int j = 0; j < v; ++j){
					d[0][i][j] = std::min(d[0][i][j], d[0][i][k] + d[0][k][j]);
					d[1][i][j] = std::min(d[1][i][j], d[1][i][k] + d[1][k][j]);
				}
			}
		}
		int p, q, r;
		std::cin >> K;
		for (int i = 0; i < K; ++i){
			std::cin >> p >> q >> r;
			std::cout << d[r][p - 1][q - 1] << std::endl;
		}
	}
	return 0;
}

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

const int MAX=1001;
const int INF=(1<<21);

int main(){

  int n,m;
  int a,b,cost,time;
  int p,q,r;
  int rail_cost[MAX][MAX];
  int rail_time[MAX][MAX];
  int k;
  while(1){
    cin>> n>> m;
    if(n==0 && m==0) break;
  
  for(int i=0;i<m;i++){
    for(int j=0;j<m;j++){
      rail_cost[i][j]=INF;
      rail_time[i][j]=INF;
    }
  }

  for(int i=0;i<n;i++){
    cin>> a>> b>> cost>> time; 
    rail_cost[a-1][b-1]=cost;
    rail_time[a-1][b-1]=time;
  }

  for(int i=0;i<m;i++){
    for(int j=0;j<m;j++){
      for(int k=0;k<m;k++){
	rail_cost[j][k]=min(rail_cost[j][k],rail_cost[j][i]+rail_cost[i][k]);
	rail_time[j][k]=min(rail_time[j][k],rail_time[j][i]+rail_time[i][k]);
      }
    }
  }

  cin>> k;
  for(int i=0;i<k;i++){
    cin>> p>> q>> r;
    if(r==0){
      cout<< rail_cost[p-1][q-1]<< endl;
    }else{
      cout<< rail_time[p-1][q-1]<< endl;
    }
  }

  }
}