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>
using namespace std;

inline int max(int a, int b) { if (a>b) return a; else return b; }
inline int min(int a, int b) { if (a<b) return a; else return b; }

int min_route(int matrix[101][101], int n, int a, int b)
{
  int cost[101];
  bool done[101];
  int min_s = a;
  const int MAX = 999999999;

  for (int i=1; i<=n; i++)
  {
    cost[i] = MAX;
    done[i] = false;
  }
  cost[a] = 0;
  while (!done[b])
  {
    done[min_s] = true;
    for (int i=1; i<=n; i++)
    {
      if ((matrix[min_s][i] > 0) && (cost[i] > cost[min_s]+matrix[min_s][i]))
      {
        cost[i] = cost[min_s]+matrix[min_s][i];
      }
    }
    int minin = MAX;
    for (int i=1; i<=n; i++)
    {
      if ((!done[i]) && (minin > cost[i]))
      {
        min_s = i;
        minin = cost[i];
      }
    }
  }
  return cost[b];
}


int main()
{
  int time[101][101];
  int cost[101][101];
  int n, m;                     // n: ?????±?????°, m: ?§??????°
  int a, b, c, t;

  cin >> n >> m;

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

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

  int k;                        // ?????¨????????°
  int p, q, r;                  // r=0:????°??????? 1:????????????
  cin >> k;
  for (int i=1; i<=k; i++)
  {
    cin >> p >> q >> r;
    if (r==0)
    {
      cout << min_route(cost, m, p, q) << endl;
    }
    else
    {
      cout << min_route(time, m, p, q) << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int INF = 1000000;
int cost[301][301],time[301][301];
int main(){
	int n,m;
	while(cin >> n >> m,n || m){
		int a,b,icost,itime,c,r;
		for(int i = 0;i <= m;++i){
			for(int j = 0;j <= m;++j){
				if(i == j) {
					cost[i][i] = 0;
					time[i][i] = 0;
				}
				else {
					cost[i][j] = INF;
					time[i][j] = INF;
				}
			}
		}
		for(int i = 0;i < n;++i){
			cin >> a >> b >> icost >> itime;
			cost[a][b] = cost[b][a] = icost;
			time[a][b] = time[b][a] = itime;
		}
		cin >> c;
		for(int d = 0;d < c;++d){
			cin >> a >> b >> r;
			for(int k = 0;k <= m;++k){
				for(int i = 0;i <= m;++i){
					for(int j = 0;j <= m;++j){
						if(r == 1) time[i][j] = min(time[i][j],time[i][k] + time[k][j]);
						else cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
					}
				}
			}
			if(r == 1) cout << time[a][b] << endl;
			else cout << cost[a][b] << endl;
		}
	}
	return 0;
}

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

const int LEN = 100;
const int INF = 10000;
int time[LEN][LEN];
int cost[LEN][LEN];


int main(void){
	while(1){
		//init
		int rosen,eki,a,b,incost,intime;
		for(int i=0; i<LEN; i++){ 
			for(int j=0; j<LEN; j++)
				time[i][j] = cost[i][j] = INF;
		}
		for(int i=0; i<LEN; i++)
			time[i][i] = cost[i][i] = 0;
	
		//in
		cin >> rosen >> eki; 
		if(rosen==0&&eki==0)break;
		for(int i=0; i<rosen; i++){
			cin >> a >> b >> incost >> intime;
			a--; b--;
			cost[a][b] = cost[b][a] = incost;
			time[a][b] = time[b][a] = intime;
		}

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

		//out
		int toi;
		cin >> toi;
		for(int i=0; i<toi; i++){
			int a,b,r;
			cin >> a >> b >> r;
			a--; b--;
			if(r==0)
				cout << cost[a][b] << endl;
			else
				cout << time[a][b] << endl;
		}
	}

	return 0;
}

C++: #include<stdio.h>
int main(void){
	int i,j,k,n,m,a,b,cost[100][100],time[100][100],p,q,r;
	scanf("%d%d",&n,&m);
	while(n!=0 || m!=0){
		for(i=0; i<m; i++){
			for(j=0; j<m; j++){
				cost[i][j]=(i==j)?0:100001;
				time[i][j]=(i==j)?0:100001;
			}
		}
		for(i=0; i<n; i++){
			scanf("%d%d",&a,&b);
			scanf("%d%d",&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(i=0; i<m; i++){
			for(j=i; j<m; j++){
				for(k=i; k<m; k++){
					cost[i][j]=(cost[i][j]>cost[i][k]+cost[k][j])?(cost[i][k]+cost[k][j]):cost[i][j];
					cost[j][i]=cost[i][j];
					time[i][j]=(time[i][j]>time[i][k]+time[k][j])?(time[i][k]+time[k][j]):time[i][j];
					time[j][i]=time[i][j];
				}
			}
		}
		scanf("%d",&k);
		for(i=0; i<k; i++){
			scanf("%d%d%d",&p,&q,&r);
			printf("%d\n",(r==0)?cost[p-1][q-1]:time[p-1][q-1]);
		}
		scanf("%d%d",&n,&m);
	}
	return 0;
}