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++: //2013/11/15(Fri)

#include <iostream>
using namespace std;
int main()
{
	int n,m;
	int a,b,cost[100][100],time[100][100];
	int k;
	int p,q,r;
	int INF = 1000001;

	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++){
				if(i==j){
					cost[i][j] = 0;
					time[i][j] = 0;
				}		
				else{
					cost[i][j] = INF;
					time[i][j] = INF;
				}
			}

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

		for(int h=0; h<m; h++)
			for(int i=0; i<m; i++)
				for(int j=0; j<m; j++){
					if(cost[i][j] > cost[i][h] + cost[h][j])
						cost[i][j] = cost[i][h] + cost[h][j];
					if(time[i][j] > time[i][h] + time[h][j])
						time[i][j] = time[i][h] + time[h][j];
				}

		cin >> k;
		for(int i=0; i<k; i++){
			cin >> p >> q >> r;

			if(r == 0)
				cout << cost[p-1][q-1] << endl;
			else if(r == 1)
				cout << time[p-1][q-1] << endl;
		}
	}
	return 0;
}

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

const int INF = 1000000000;

int main()
{
  int count=0;
	while(count==50){
		int n, m;
		cin >> n >> m;

		if(n==0 && m==0)
			break;

		 int C[100][100];
		 int T[100][100];
	   for(int i=0; i<m; i++){
		   for(int j=0; j<m; j++){
			   if(i==j){
	         C[i][j] = T[i][j] = 0;
		     }else{
			     C[i][j] = T[i][j] = INF;
		     }
		   }
     }

	   for(int i=0; i<n; i++){
		   int a, b, c, t;
		   cin >> a >> b >> c >> t;

	     C[a-1][b-1] = C[b-1][a-1] = c;
		   T[a-1][b-1] = T[b-1][a-1] = t;
    }

    int k;
    cin>>k;
    for(int i=0;i<k;i++){
      int p, q, r;
      int a=0;
      cin>>p>>q>>r;
      int d[m][m];
      if(r==0){
        for(int i=0;i<m;i++){
          for(int j=0;j<m;j++){
            d[i][j]=C[i][j];
          }
        }
        for(int k=0;k<m;k++){
          for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
              if(d[i][j]>d[i][k]+d[k][j]){
                d[i][j]=d[i][k]+d[k][j];
              }
            }
          }
        }
        a=d[p-1][q-1];
        cout<<a<<endl;
      }
      if(r==1){
        for(int i=0;i<m;i++){
          for(int j=0;j<m;j++){
            d[i][j]=T[i][j];
          }
        }
        for(int k=0;k<m;k++){
          for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
              if(d[i][j]>d[i][k]+d[k][j]){
                d[i][j]=d[i][k]+d[k][j];
              }
            }
          }
        }
        a=d[p-1][q-1];
        cout<<a<<endl;
      }
    }
    count++;
	}

	return 0;
}

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()
{
	cin >> N >> M;
	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);
	}
	
}