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++: #include <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///


int main()
{
	int n, m;
	while (cin >> n >> m, n)
	{
		int g[101][101][2];
		REP(k, 2)REP(i, 101)REP(j, 101) g[i][j][k] = (i == j ? 0 : INF);
		while (n--)
		{
			int a, b, c, d;
			cin >> a >> b >> c >> d;
			a--; b--;
			g[a][b][0] = g[b][a][0] = c;
			g[a][b][1] = g[b][a][1] = d;
		}
		REP(l, m)REP(k, m)REP(i, m)REP(j, m) g[i][j][l] = min(g[i][j][l], g[i][k][l] + g[k][j][l]);
		int q;
		cin >> q;
		while (q--)
		{
			int a, b, c;
			cin >> a >> b >> c;
			a--; b--;
			cout << g[a][b][c] << endl;
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#define MAX_M 101
#define MAX_N 301
#define MAX_K 201
#define MAX_INT 1 << 28;
using namespace std;

//dx[wa][wb][XCb`(0:àz, 1:Ô)] = (waÆwbÔÌàz or Ô)
int dx[MAX_N][MAX_N][2];

//p©çqÜÅÌÅ¬àzÜ½ÍÅZÔðÔ·
int tell(int p, int q, int n, int m, int r);

int main(){
	//n = üHÌîñÌ, m = wÌ
	int n, m;
	
	//üHÌîñðüÍ
	while(cin >>n >>m){
		if(n == 0 && m == 0) break;
		
		//dx[][][]Ìú»(SÄ-1É·é)
		for(int i = 0; i < MAX_N; i++){
			for(int j = 0; j < MAX_N; j++){
				dx[i][j][0] = -1;
				dx[i][j][1] = -1;
			}
		}
		
		for(int i = 0; i < n; i++){
			
			//a = wa, b = wb, cost = wa`wbÌàz, time = wa`wbÌÔ
			int a, b, cost, time;
			
			cin >>a >>b >>cost >>time;
			dx[a][b][0] = cost;
			dx[a][b][1] = time;
		}
		
		//k = â¢í¹Ì
		int k;
		
		cin >>k;
		for(int i = 0; i < k; i++){
			
			//p = o­w, q = w, r = oÍ·élÌíÞ
			int p, q, r;
			cin >>p >>q >>r;
			
			cout <<tell(p, q, n, m, r) <<endl;
		}
	}
	return 0;
}

int tell(int p, int q, int n, int m, int r){
	
	//checkendnum[wÌÔ] = Å¬àzÜ½ÍÅZÔ(¢mèÌêÍMAX_INT)
	int checkendnum[MAX_N];
	
	//checkendi[wÌÔ] = Å¬àzÜ½ÍÅZÔªmèµ½©Ç¤©(false:¢mè, true:mè)
	bool checkendi[MAX_N] = {false};
	
	//checkendnum[1 ` MAX_N]ðMAX_INTÅú»
	for(int i = 0; i < MAX_N; i++){
		checkendnum[i] = MAX_INT;
	}
	
	//p = ¡²×Ä¢ém[h
	int node = p;
	
	//o­wÍà¤0ÅmèµÄ¢é
	checkendnum[node] = 0;
	checkendi[node] = true;
	
	//EoðÍmè³ê½m[h = q
	while(1){
		
		//SÄÌüHÌîñð²×é
		for(int i = 0; i < MAX_N; i++){
			
			//p©çiÉs­àzÜ½ÍÔÌîñª èA»êªmèµÄ¢È¯êÎ
			if(dx[node][i][r] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½óâÌîñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[node][i][r] + checkendnum[node]));
				}
			
			//àµ­ÍAp©çiÉs­àzÜ½ÍÔÌîñª èA»êªmèµÄ¢È¯êÎ
			} else if(dx[i][node][r] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½îñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[i][node][r] + checkendnum[node]));
				}
			}
		}
		
		//minnum = mèµÄ¢È¢wÌÅAÅ¬ÌàzÜ½ÍÅZÌÔ, mini = minnumðÂwÌÔ
		int minnum = MAX_INT;
		int mini = -1;
		
		//SÄÌwð²×é
		for(int i = 1; i <= m; i++){
			
			//àzÜ½ÍÔªmèµÄ¨ç¸Aminnumæè¬³¢àzÜ½ÍÔðÂÈç
			if(checkendi[i] == false && checkendnum[i] < minnum){
				minnum = checkendnum[i];
				mini = i;
			}
		}
		
		//miniÌwÌàzÜ½ÍÔðmè
		checkendi[mini] = true;
		
		//Ì²×ém[hðminiÉ·é
		node = mini;
		
		//qÌÅ¬àzÜ½ÍÅZÔªmèµ½ÈçI¹(I¹ð)
		if(checkendi[q] == true){
			break;
		}
	}
	return checkendnum[q];
}