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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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>
#include<vector>

using namespace std;

int i, j, k, p, q, r, n, m, a, b, c, cost[3001], t, dc[200][200], dt[200][200];

int dic(int d[200][200]) {
  cost[p] = 0;
  int min, f[200] = {};
  for(i = 0; i < m; i++) {
    f[p] = 1;
    for(j = 1; j <= m; j++) {
      if(f[j]) continue;
      if(cost[j] > cost[p] + d[p][j]) cost[j]  = cost[p] + d[p][j];
    }
    min = 200000000;
    for(j = 1; j <= m; j++ ) {
      if(f[j]) continue;
      if(min > cost[j]) min = cost[p = j];
    }
  }
  return cost[q];
}


int main() {
  for(i = 0; i < 200; i++)
    for(j = 0; j < 200; j++) {
      dc[i][j] = 100000000;
      dt[i][j] = 100000000;
    }
  
  cin >> n >> m;
  
  while(n--) {
    cin >> a >> b >> c >> t;
    dc[a][b] = dc[b][a] = c;
    dt[a][b] = dt[b][a] = t;
  }
  
  cin >> k;
  
  while(cin >> p  >> q, p != 0 || q != 0 ) {
    cin >> r;
    for(i = 0; i <= 3000; i++) cost[i] = 1000000000;
    if(r) cout << dic(dt) << endl;
    else cout << dic(dc) << endl;
  }
  
  return 0;
}

C++: #include<iostream>
using namespace std;
const int INF=1000000;
int n,m;
int G1[2000][2000],G2[2000][2000];
int d1[2000][2000],d2[2000][2000];

int main(){


	
	while(cin>>n>>m){
		int to,from,cost,time,num,d;
		if(n==0&&m==0)break;
	
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				G1[i][j]=INF;
				G2[i][j]=INF;
				d1[i][j]=INF;
				d2[i][j]=INF;
			}
		}
	
		for(int i=0;i<n;i++){
			cin>>to>>from>>cost>>time;
			to--;from--;
			G1[to][from]=cost;
			G1[from][to]=cost;
			G2[to][from]=time;
			G2[from][to]=time;
		
			d1[to][from]=cost;
			d1[from][to]=cost;
			d2[to][from]=time;
			d2[from][to]=time;	
		}
	
	
	
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				for(int k=0;k<m;k++){
					if(d1[i][j]>d1[i][k]+d1[k][j])
						d1[i][j]=d1[i][k]+d1[k][j];
					
					if(d2[i][j]>d2[i][k]+d2[k][j])
						d2[i][j]=d2[i][k]+d2[k][j];
				}
			}
		}
	

		cin>>num;
		for(int i=0;i<num;i++){
			cin>>to>>from>>d;
			to--;
			from--;
			if(d)cout<<d2[to][from]<<endl;
			else cout<<d1[from][to]<<endl;
		}
	}
	
	
	
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <limits.h>
using namespace std;


				
int main()
{
	int n,m,a,b,co,ti,p,r;
	int money[300][300];
	int time[300][300];

	while(cin >> n >> m && n && m)
	{
		/*[m-1][m-1]ツづ慊づづ個配ツ療アツづーツ渉可甘コツ可サ*/
		for(int i=0;i<m;i++)
			for(int j=0;j<m;j++)
				time[i][j]=money[i][j]= (i-j) ? 0xFFFFFFF : 0;

		for(int i=0;i<n;i++)
		{
			cin >> a >> b >> co >> ti;
			money[a-1][b-1]=money[b-1][a-1]=co;
			time[a-1][b-1]=time[b-1][a-1]=ti;
		}
		/*ツδ渉ーツシツδδδ仰フツδ債イツドツ法 for time*/
		for(int k=0;k<m;k++)
			for(int i=0;i+k<m;i++)
				for(int j=0;j<m;j++)
					time[i][j]=time[j][i]=min(time[i][j],time[i][k]+time[k][j]);
					
		/*ツδ渉ーツシツδδδ仰フツδ債イツドツ法 for cost*/	
		for(int k=0;k<m;k++)
			for(int i=0;i+k<m;i++)
				for(int j=0;j<m;j++)
					money[i][j]=money[j][i]=min(money[i][j],money[i][k]+money[k][j]);
		cin >> p;
		
		/*
		for(int i=0;i<m;i++)
		{
			
			for(int j=0;j<m;j++)printf("%10d",money[i][j]);
			cout << endl;
		}
		*/
		
		for(int i=0;i<p;i++)
		{
			cin >> a >> b >> r;
			cout <<(r?time[a-1][b-1]:money[a-1][b-1])<< endl;
		}
	}
}

C++: #include <iostream>
#include <cstring>

using namespace std;

const int INF=100000;
int cost[101][101];
int time[101][101];

int main(void){
	int n,m,a,b,c;
	while(1){
		for(a=0;a<=100;a++){
			for(b=0;b<=100;b++){
				cost[a][b]=INF;
				time[a][b]=INF;
				if(a==b)cost[a][b]=0,time[a][b]=0;
			}
		}
		cin >> n >> m;
		if(n==0 && m==0)break;
		for(a=0;a<n;a++){
			int f,t,co,ti;
			scanf("%d%d%d%d",&f,&t,&co,&ti);
			if(cost[f][t]>co)cost[f][t]=co,cost[t][f]=co;
			if(time[f][t]>ti)time[f][t]=ti,time[t][f]=ti;
		}
		for(a=1;a<=m;a++){
			for(b=1;b<=m;b++){
				for(c=1;c<=m;c++){
					cost[a][b]=min(cost[a][b],cost[a][c]+cost[c][b]);
					time[a][b]=min(time[a][b],time[a][c]+time[c][b]);
				}
			}
		}
		cin >> n;
		for(a=0;a<n;a++){
			int d,f,t;
			scanf("%d%d%d",&f,&t,&d);
			if(d==0)printf("%d\n",cost[f][t]);
			if(d==1)printf("%d\n",time[f][t]);
		}
	}
    return 0;
}

C++: #include<iostream>

#define INF 1000000;
using namespace std;


int main(){
  int n,m;
  while(1){
    cin>>n>>m;
    if(n==0&&m==0){break;}
    int s_tim[m][m];
    int c_money[m][m];
    int a,b,cost,tim;

    for(int i=0;i<m;i++){
      for(int j=0;j<n;j++){
        s_tim[i][j]=INF;
        c_money[i][j]=INF;
      }
    }
    for(int i=0;i<m;i++){
      s_tim[i][i]=0;
      c_money[i][i]=0;
    }
    for(int i=0;i<n;i++){
      cin>>a>>b>>cost>>tim;
      s_tim[a-1][b-1]=tim;s_tim[b-1][a-1]=tim;
      c_money[a-1][b-1]=cost;c_money[b-1][a-1]=cost;
    }

    for(int k=0;k<m;k++){
      for(int i=0;i<m;i++){
        for(int j=0;j<m;j++){
          if(s_tim[i][j]>s_tim[i][k]+s_tim[k][j]){
            s_tim[i][j]=s_tim[i][k]+s_tim[k][j];
          }
          if(c_money[i][j]>c_money[i][k]+c_money[k][j]){
            c_money[i][j]=c_money[i][k]+c_money[k][j];
          }
        }
      }
    }

    int contacts,p,q,r;
    cin>>contacts;

    for(int i=0;i<contacts;i++){
      cin>>p>>q>>r;
      if(r){cout<<s_tim[p-1][q-1];}
      else{ cout<<c_money[p-1][q-1];}
      cout<<endl;
    }
  }
}