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>
using namespace std;
int main(){
	int n,m;
	while(cin >> n >> m,n){
		int a,b,c,d;
		int co[n][n];
		int ti[n][n];
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++){
				co[i][j] = 100000000;
				ti[i][j] = 100000000;
			}
		for(int i = 0; i < n; i++){
			cin >> a >> b >> c >> d;
			a--; b--;
			co[a][b] = c;
			co[b][a] = c;
			ti[a][b] = d;
			ti[b][a] = d;
		}
		vector <int> used(n,100000000);
		vector <int> dist(n,100000000);
		int k,p,q,r;
		cin >> k;
		for(int i = 0; i < k; i++){
			cin >> p >> q >> r;
			p--; q--;
			used = vector <int> (n,100000000);
			dist = vector <int> (n,100000000);
			dist[p] = 0;
			while(1){
				int va = -1;
				for(int j = 0; j < n; j++)
					if(used[j] == 100000000 && (va == -1 || dist[va] > dist[j]))
						va = j;
				if(va == -1)
					break;
				used[va] = 1;
				for(int j = 0; j < n; j++)
					dist[j] = min(dist[j] , (r == 0 ? co[j][va] : ti[j][va]) + dist[va]);
			}
			cout << dist[q] << endl;
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cmath>
#include <queue>

#define INF (1 << 26)

using namespace std;

int cost[200][200], dist[200][200];

int main(){
	
	int n, m;
	
	while(true){
		
		cin >> n >> m;
		
		if(n == 0 && m == 0){
			break;
		}
		
		for(int i = 0; i <= m; i++){
			for(int j = 0; j <= m; j++){
				cost[i][j] = INF;
				cost[j][i] = INF;
				dist[i][j] = INF;
				dist[j][i] = INF;
			}
		}
		for(int i = 0; i <= m; i++){
			cost[i][i] = 0;
			dist[i][i] = 0;
		}
		
		for(int i = 0; i < n; i++){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a][b] = c;
			cost[b][a] = c;
			dist[a][b] = t;
			dist[b][a] = t;
		}
		
		for(int i = 0; i <= m; i++){
			for(int j = 0; j <= m; j++){
				for(int k = 0; k <= m; k++){
					cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
				}
			}
		}
		
		int k;
		
		cin >> k;
		
		for(int loop = 0; loop < k; loop++){
			int p, q, r;
			cin >> p >> q >> r;
			if(r == 0){
				cout << cost[p][q] << endl;
			}else{
				cout << dist[p][q] << endl;
			}
		}
		
	}
	
	return 0;
}

C++: // 2011/01/12 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int n, m;
		cin >> n >> m;
		if(n==0&&m==0)
			break;

		int cst[101][101];
		int tim[101][101];
		for(int i=1; i<=m; i++)
			for(int j=1; j<=m; j++)
				if(i==j)
					cst[i][j] = tim[i][j] = 0;
				else
					cst[i][j] = tim[i][j] = 200000;

		for(int i=0; i<n; i++){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cst[a][b] = cst[b][a] = c;
			tim[a][b] = tim[b][a] = t;
		}

		for(int h=1; h<=m; h++)
			for(int i=1; i<=m; i++)
				for(int j=1; j<=m; j++){
					if(cst[i][j]>cst[i][h]+cst[h][j])
						cst[i][j] = cst[i][h]+cst[h][j];
					if(tim[i][j]>tim[i][h]+tim[h][j])
						tim[i][j] = tim[i][h]+tim[h][j];
				}

		int k;
		cin >> k;

		for(int i=0; i<k; i++){
			int p, q, r;
			cin >> p >> q >> r;
			if(r==0)
				cout << cst[p][q] << endl;
			else
				cout << tim[p][q] << endl;
		}
	}

	return 0;
}

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

static const int MAX_S = 100;
static const int INFTY = ( 1 << 20 );

pair< int, int > Adj[MAX_S][MAX_S]; // first : cost, second : time

int main()
{

	int n, m, a, b, cost, time, qn, r;

	while( cin >> n >> m, n || m ) {

		for( int i = 0; i < m; i++ ) {
			for( int j = 0; j < m; j++ ) Adj[i][j].first = Adj[i][j].second = INFTY;
		}

		for( int i = 0; i < n; i++ ) {

			cin >> a >> b >> cost >> time;
			Adj[--a][--b].first  = Adj[b][a].first  = cost;
			Adj[a][b].second	 = Adj[b][a].second = time;

		}

		for( int k = 0; k < m; k++ ) {
			for( int i = 0; i < m; i++ ) {
				if( Adj[i][k].first == INFTY ) continue;
				for( int j = 0; j < m; j++ ) {

					Adj[i][j].first  = min( Adj[i][j].first , Adj[i][k].first  + Adj[k][j].first );
					Adj[i][j].second = min( Adj[i][j].second, Adj[i][k].second + Adj[k][j].second );
				
				}
			}
		}

		cin >> qn;
		for( int i = 0; i < qn; i++ ) {

			bool query;

			cin >> a >> b >> query;
			a--; b--;

			cout << ( !query ? Adj[a][b].first : Adj[a][b].second ) << endl;

		}

	}

	return 0;

}