Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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
					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[100],q[100],r[100];
	int INF = 1001;

	int dcost[100][100],dtime[100][100];
	while(true){
		cin >> n >> m;
		if(n==0 && m==0)
			break;

		for(int i=0; i<n; i++)
			for(int j=0; j<n; 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][b] >> time[a][b];
			cost[b][a] = cost[a][b];
			time[b][a] = time[a][b];
		}  
		cin >> k;
		for(int i=0; i<k; i++)
			cin >> p[i] >> q[i] >> r[i];
			
		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++){
				dcost[i][j] = cost[i][j];
				dtime[i][j] = time[i][j];
			}
		for(int h=0; h<n; h++)
			for(int i=0; i<n; i++)
				for(int j=0; j<n; j++){
					if(dcost[i][j] > dcost[i][h] + dcost[h][j])
						dcost[i][j] = dcost[i][h] + dcost[h][j];
					if(dtime[i][j] > dtime[i][h] + dtime[h][j])
						dtime[i][j] = dtime[i][h] + dtime[h][j];
				}

		
		for(int i=0; i<k; i++)
			if(r[i] == 0)
				cout << dcost[p[i]][q[i]] << endl;
			else if(r[i] == 1)
				cout << dtime[p[i]][q[i]] << endl;
	}
	return 0;
}

C++: #include <iostream>

#define INF (1000000000)
#define min(x, y) ((x) < (y) ? (x) : (y))

int main(void) {
  using namespace std;

  int n, m;

  while (cin >> n >> m, n != 0 && m != 0) {
    int cost[100][100];
    int time[100][100];

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = time[i][j] = i == j ? 0 : INF;
      }
    }

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

      a--; b--;

      cost[a][b] = cost[b][a] = min(c, cost[a][b]);
      time[a][b] = time[b][a] = min(t, time[a][b]);
    }

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

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

      p--; q--;

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

  return 0;
}

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

const int INF = (int)1e9 + 1;

int d1[100][100], d2[100][100];

int main() {
	while (1) {
		int n, m;
		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) {
					d1[i][j] = 0;
					d2[i][j] = 0;
				}
				else {
					d1[i][j] = INF;
					d2[i][j] = INF;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			a--; b--;
			d1[a][b] = d1[b][a] = c;
			d2[a][b] = d2[b][a] = t;
		}
		for (int k = 0; k < m; k++) {
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < m; j++) {
					d1[i][j] = min(d1[i][j], d1[i][k] + d1[k][j]);
					d2[i][j] = min(d2[i][j], d2[i][k] + d2[k][j]);
				}
			}
		}
		int k;
		cin >> k;
		for (int i = 0; i < k; i++) {
			int p, q, r;
			cin >> p >> q >> r;
			p--; q--;
			if (r == 0) {
				cout << d1[p][q] << endl;
			}
			else {
				cout << d2[p][q] << endl;
			}
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
const int INF = INT_MAX/3;
const int MAXN = 200;
const int TIME = 1,COST = 0;
int graph[2][MAXN][MAXN];
int n,m,a,b,c,t,k;//m:wÌAn:GbWÌ
void solve();
void init(){
  for(int i = 0;i < m;i++)
    for(int j = 0;j < m;j++)
      graph[0][i][j] =graph[0][j][i] = graph[1][j][i] = graph[1][i][j] = INF;
}
int main(){
  cin >> n >> m;
  init();
  for(int i = 0;i < n;i++){
    cin >> a >> b >> c >>t;
    graph[TIME][a-1][b-1] = graph[TIME][b-1][a-1] = t;
    graph[COST][a-1][b-1] = graph[TIME][b-1][a-1] = c;
  }

  solve();

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

    cout << graph[c][a-1][b-1] << endl;
  }
  return 0;
}

void solve(){
  for(int k = 0;k < m;k++){
    for(int i = 0;i < m;i++){
      for(int j = 0;j < m;j++){
	graph[0][i][j] = min(graph[0][i][j],graph[0][i][k] + graph[0][k][j]);
	graph[1][i][j] = min(graph[1][i][j],graph[1][i][k] + graph[1][k][j]);
      }
    }
  }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
static const long long INFTY = (1LL << 32);
int main()
{
	int n, m, p, q, r,i,j,k,a,b,cost,time;
	long long cost_d[101][101], time_d[101][101];
	while (1) {
		cin >> n >> m;
		if (n == 0 && m == 0)break;
		for (i = 1; i <= m; i++) {
			for (j = 1; j <= m; j++) {
				cost_d[i][j] = ((i == j) ? 0 : INFTY);
				time_d[i][j] = ((i == j) ? 0 : INFTY);
			}
		}
		for (i = 0; i < n; i++) {
			cin >> a >> b >> cost >> time;
			cost_d[a][b] = cost;
			cost_d[b][a] = cost;
			time_d[a][b] = time;
			time_d[b][a] = time;
		}
		for (k = 1; k <= m; k++) {
			for (i = 1; i <= m; i++) {
				if (cost_d[i][k] == INFTY)continue;
				for (j = 1; j <= m; j++) {
					if (cost_d[k][j] == INFTY)continue;
					cost_d[i][j] = min(cost_d[i][j], cost_d[i][k] + cost_d[k][j]);
					time_d[i][j] = min(time_d[i][j], time_d[i][k] + time_d[k][j]);
				}
			}
		}
		cin >> k;
		for (i = 0; i < k; i++) {
			cin >> p >> q >> r;
			if (r == 0) {
				cout << cost_d[p][q]<<endl;
			}
			else {
				cout << time_d[p][q] << endl;
			}
		}
	}
	return 0;
}