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>


const unsigned int inf = 1 << 26;

const int MAX_OF_INFOMATIONS = 300; // 路線情報の最大数

const int MAX_OF_STATIONS = 100; // 駅の最大数

const int MAX_OF_ENQUIRIES = 200; // 問い合わせの最大数


// 大工ストアならぬダイクストラ

int dijk(int m, int n, int s, int g[2][MAX_OF_STATIONS][MAX_OF_STATIONS], int ary[2][MAX_OF_STATIONS][MAX_OF_STATIONS]) {

	bool *used = new bool[n];

	unsigned int *d = new unsigned int[n];


	// std::cout << "n = " << n << " " << "s = " << s << std::endl;


	for(int i = 0; i < n; i++)
		d[i] = inf;

	d[s] = 0;

	for(int i = 0; i < n; i++)
		used[i] = false;


	while(true) {

		int u = -1;

		for(int v = 0; v < n; v++)

		if(!used[v] && (u == -1 || d[v] < d[u]))
			u = v;


			// std::cout << "u = " << u << std::endl;


		if(u == -1) break;

		used[u] = true;


		for(int v = 0; v < n; v++)
			if(d[v] > d[u] + g[m][u][v])
				d[v] = d[u] + g[m][u][v];

	}


	for(int i = 0; i < n; i++)
		ary[m][s][i] = d[i];


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

// std::cout << "d[" << i << "] = " << d[i] << std::endl;


	return 0;

}


int main() {

	int a[MAX_OF_INFOMATIONS] = {}, // 駅番号aの配列

	b[MAX_OF_INFOMATIONS] = {}, // 駅番号bの配列

	cost[MAX_OF_INFOMATIONS] = {}, // aからbまでの運賃

	time[MAX_OF_INFOMATIONS] = {}, // aからbまでの移動時間

	p[MAX_OF_ENQUIRIES] = {}, // 始点a

	q[MAX_OF_ENQUIRIES] = {}, // 終点b

	r[MAX_OF_ENQUIRIES] = {}, // 知りたい情報の種類

	d[2][MAX_OF_STATIONS][MAX_OF_STATIONS] = {},

	g[2][MAX_OF_STATIONS][MAX_OF_STATIONS] = {};

	int info, // 路線情報数

	station, // 駅の数

	enquiry; // 問い合わせ数


	while(true) {

		std::cin >> info;

		std::cin >> station;


		// 初期化処理

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

			for(int j = 0; j < station; j++) {

				for(int k = 0; k < station; k++) {

					g[i][j][k] = inf;

					d[i][j][k] = inf;

				}

			}

		}


		// inputが0 0だったら終了

		if(!info && !station) break;


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

			std::cin >> a[i];

			std::cin >> b[i];

			std::cin >> cost[i];

			std::cin >> time[i];

		}


			// a - 1, b - 1をする(駅番号が1からmまでである)

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

			a[i]--;

			b[i]--;

		}


			// 時間と運賃を配列に入れる

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

			g[0][a[i]][b[i]] = time[i];

			g[1][a[i]][b[i]] = cost[i];

			g[0][b[i]][a[i]] = time[i];

			g[1][b[i]][a[i]] = cost[i];


		}


		// 大工ストアじゃなくてダイクストラに入れる

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

//			dijk(0, station, i, g, d);

//			dijk(1, station, i, g, d);

		}


		std::cin >> enquiry;


		// 問い合わせ

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

			std::cin >> p[i];

			std::cin >> q[i];

			std::cin >> r[i];

		}


		// 数合わせ.

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

			p[i]--;

			q[i]--;

		}


// 表示

		for(int i = 0; i < enquiry; i++)

//			std::cout << d[!r[i]][p[i]][q[i]] << std::endl;

		std::cout << !0 << std::endl;

	}


	return 0;

}

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

#define INF 1010000000
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)
#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);

using namespace std;


int main(){
	while(1){
		int n,m;
		sintt(m,n);
		if(n==0)break;
		int fro[101][101][2];
		rep(i,101)rep(j,101)fro[i][j][0]=fro[i][j][1]=INF;
		
		rep(i,m){
			int a,b,c,d;
			sintt(a,b);
			sintt(c,d);
			
			fro[a][b][0]=fro[b][a][0]=c;
			fro[a][b][1]=fro[b][a][1]=d;
		}
		
		
		reps(k,n){
			reps(i,n){
				reps(j,n){
					fro[i][j][0]=min(fro[i][j][0],fro[i][k][0]+fro[k][j][0]);
					fro[i][j][1]=min(fro[i][j][1],fro[i][k][1]+fro[k][j][1]);
				}
			}
		}
		
		int t;
		sint(t);
		
		rep(i,t){
			int a,b,c;
			sinttt(a,b,c);
			
			printf("%d\n",fro[a][b][c]);
		}
	}
}

C++: #include <stdio.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>

using namespace std;

#define MAX_N 3000
#define MAX_M 100
#define MAX_K 200

int N, M, K;
//int P, Q, R;
int cost[MAX_M][MAX_M];
int times[MAX_M][MAX_M];
const int inf = 100100;

//R 0:最小金額、 1:最短時間

void init()
{
	for(int y=1; y<=M; ++y)
	{
		for(int x=1; x<=M; ++x)
		{
			cost[x][y] = inf;
			times[x][y] = inf;
		}
	}
}

void show(int R)
{
	auto weight = (R? times : cost);
	for(int x=1; x<=M; ++x)
	{
		for(int y=1; y<=M; ++y)
		{
			if(weight[x][y] == inf){cout << "inf ";}
			else{printf("%3d ",weight[x][y]);}
		}
		cout << endl;
	}
}

void floyd_warshall(int P, int Q, int R)
{
	auto weight = (R? times : cost);
	for(int k=1; k<=M; ++k)
	{
		for(int i=1; i<=M; ++i)
		{
			for(int j=1; j<=M; ++j)
			{
				if(weight[i][j] > weight[i][k]+weight[k][j])
				{
					weight[i][j] = weight[i][k]+weight[k][j];
				}
			}
		}
	}
	cout << weight[P][Q] << endl;
}

void dijkstra()
{
	//for(int i=0; )
}

void bellman_ford(int P, int Q, int R)
{
	auto weight = (R? times : cost);
	
	int D[M+1];
	for(int i=0; i<=M; ++i)
	{
		D[i] = inf;
	}
	D[P] = 0;
	for(int i=0; i<abs(M)-1; ++i)
	{
		for(int u=1; u<=M; ++u)
		{
			for(int v=1; v<=M; ++v)
			{
				D[v] = min(D[v], D[u]+weight[u][v]);
			}
		}
	}
	cout << D[Q] << endl;
}

int main()
{
	while(true)
	{
		cin >> N >> M;
		if(!N && !M){break;}
		init();
		int a, b, c, t;
		for(int i=0; i<N; ++i)
		{
			cin >> a >> b >> c >> t;
			cost[a][b] = c;
			cost[b][a] = c;
			times[a][b] = t;
			times[b][a] = t;
		}
		cin >> K;
	
		int P, Q, R;
		for(int i=0; i<K; ++i)
		{
			cin >> P >> Q >> R;
			//bellman_ford(P,Q,R);
			floyd_warshall(P, Q, R);
		}
	}
}