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++: #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];
  const int MAX = 999999999;

  for (int i=1; i<=n; i++)
  {
    cost[i] = MAX;
    done[i] = false;
  }

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


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<set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define INF 1e8
using namespace std;
int n,m,k,a,b,c,alt,p,q,r;
long long cost[101][101],tm[101][101],d[101];
bool used[101];

void dfunc(int s,int r){
	d[s]=0;
	rep(i,101){
		used[i]=true;
		d[i]=INF;
	}
	while(1){
		int mem=INF,now=-1;
		rep(i,101){
			if(used[i] && mem>d[i]){
				now=i;
				mem=d[i];
			}
		}
		if(now==-1)break;
		used[now]=false;
		rep(i,101){
			if(r==0){
				if(cost[now][i]!=INF)d[i]=d[now]+cost[now][i];
			}else{
				if(tm[now][i]!=INF)d[i]=d[now]+tm[now][i];
			}
		}
	}
}

int main(){
	while(cin>>n>>m,n||m){
		rep(i,101)rep(j,101){
			cost[i][j]=INF;
			tm[i][j]=INF;
		}
		rep(i,n){
			cin>>a>>b>>c>>alt;
			a--;
			b--;
			cost[b][a]=cost[a][b]=c;
			tm[b][a]=tm[a][b]=alt;
		}
		cin>>k;
		rep(i,k){
			cin>>p>>q>>r;
			dfunc(p,r);
			cout<<d[q]<<endl;
		}
	}
	return 0;
}

C++: #include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<sstream>
#include<functional>
#include<map>
#include<set>
#include<cmath>

#define REP(i,s,e) for(int i=int(s);i<=int(e);i++)
#define rep(i,n) for(int i=0;i<int(n);i++)

using namespace std;

#define INF 10000

int m,x,y;
int c[105][105],ct[105][105];
int d[105],dt[105];
bool b[105],bt[105];

void mc(int s){
rep(i,m){
d[i]=INF; b[i]=false;
}
d[s]=0;

while(1){
int M=INF;
rep(i,m){
if(!b[i] && d[i]<M){
	M=d[i]; x=i;
}
}

if(M==INF) break;

b[x]=true;
rep(i,m) d[i]=min(d[i],d[x]+c[x][i]);

}
}

void mt(int s){
rep(i,m){
dt[i]=INF; bt[i]=false;
}
dt[s]=0;

while(1){
int M=INF;
rep(i,m){
if(!bt[i] && dt[i]<M){
	M=dt[i]; y=i;
}
}

if(M==INF) break;

bt[y]=true;
rep(i,m) dt[i]=min(dt[i],dt[y]+ct[y][i]);

}
}

int main(){

rep(i,105){
rep(j,105){
	c[i][j]=INF;
	ct[i][j]=INF;
}
}

int n,a,b,cost,time,k,p,q,r;

while(1){

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

rep(i,n){
cin >> a >> b >> cost >> time;

c[a-1][b-1]=cost; c[b-1][a-1]=cost;
ct[a-1][b-1]=time; ct[b-1][a-1]=time;
}

cin >> k;

rep(i,k){
cin >> p >> q >> r;

if(r==0){
	mc(p-1); cout << d[q-1] << endl;


}

else{
	mt(p-1); cout << dt[q-1] << endl;


}
}


}


	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int INF = 1e+9;

int main(){
	int n,m;
	while( cin >> n >> m , n||m ){
		int d[2][101][101];
		for(int i=0 ; i < 101 ; i++ ){
			for(int j=0 ; j < 101 ; j++ ){
				d[0][i][j] = (i==j)? 0 : INF;
				d[1][i][j] = (i==j)? 0 : INF;
			}
		}

		for(int i=0 ; i < n ; i++ ){
			int a,b,cost,time;
			cin >> a >> b >> cost >> time;
			d[0][a][b] = cost;
			d[0][b][a] = cost;
			d[1][a][b] = time;
			d[1][b][a] = time;
		}
		for(int k=1 ; k <= m ; k++ ){
			for(int i=1 ; i <= m ; i++ ){
				for(int j=1 ; j <= m ; j++ ){
					d[0][i][j] = min( d[0][i][j] , d[0][i][k]+d[0][k][j] );
					d[1][i][j] = min( d[1][i][j] , d[1][i][k]+d[1][k][j] );
				}
			}
		}
		int k;
		cin >> k;
		for(int i=0 ; i < k ; i++ ){
			int p,q,r;
			cin >> p >> q >> r;
			cout << d[r][p][q] << endl;
		}
	}
}