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
			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++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
using namespace std;
main(){
  int m,n;
  while(cin>>m>>n,n){
    int a1[n][n],a2[n][n],q,w,e,r;
    r(i,n)r(j,n){
      if(i==j)a2[i][j]=a1[i][j]=0;
      else a2[i][j]=a1[i][j]=INF;
    }
    while(m--){
      cin>>q>>w>>e>>r;q--;w--;
      a1[q][w]=a1[w][q]=e;
      a2[q][w]=a2[w][q]=r;
    }
    r(k,n)r(i,n)r(j,n){
      a1[i][j]=min(a1[i][j],a1[i][k]+a1[k][j]);
      a2[i][j]=min(a2[i][j],a2[i][k]+a2[k][j]);
    }
    cin>>e;
    while(e--){
      cin>>q>>w>>r;q--;w--;
      if(r)cout<<a2[q][w]<<endl;
      else cout<<a1[q][w]<<endl;
    }
  }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int road1[3333][3333];
  int road2[3333][3333];
  int n,m;
  int a,b,cost,time;
  int p,q,r;
  int x;
  while(1){
    for(int i=0;i<101;i++){
      for(int j=0;j<101;j++){
	road1[i][j]=road2[i][j]=1111111;
      }
    }
    cin >> n >> m;
    if(n==0 && m==0)break;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      road1[a][b]=road1[b][a]=cost;
      road2[a][b]=road2[b][a]=time;
    }

    for(int k=1;k<=n;k++){
      for(int i=1;i<=n;i++){
	for(int j=1;j<=n;j++){
	  if(road1[i][j]>road1[i][k]+road1[k][j]){
	    road1[i][j]=road1[i][k]+road1[k][j];
	  }
	  if(road2[i][j]>road2[i][k]+road2[k][j]){
	    road2[i][j]=road2[i][k]+road2[k][j];
	  }
	}
      }
    }
    
    cin >> x;
    for(int i=1;i<=x;i++){
      cin >> p >> q >> r;
      cout << (r==0 ? road1[p][q] : road2[p][q]) << endl;
    }
  }
}

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

const int INF = 1 << 25;
const int M_LIMIT = 100;

int main()
{
	int n, m, k;
	int cost[ M_LIMIT + 1 ][ M_LIMIT + 1 ];
	int time[ M_LIMIT + 1 ][ M_LIMIT + 1 ];
	while( cin >> n >> m && ( n || m ) ){
		for( int i = 0; i < M_LIMIT + 1; ++i ){
			for( int j = 0; j < M_LIMIT + 1; ++j ){
				cost[ i ][ j ] = time[ i ][ j ] = INF;
			}
		}
		for( int i = 0; i < M_LIMIT + 1; ++i ){
			cost[ i ][ i ] = time[ i ][ i ] = 0;
		}
		for( int i = 0; i < n; ++i ){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[ a ][ b ] = cost[ b ][ a ] = c;
			time[ a ][ b ] = time[ b ][ a ] = t;
		}
		for( int p = 1; p <= m; ++p ){
			for( int i = 1; i <= m; ++i ){
				for( int j = 1; j <= m; ++j ){
					cost[ i ][ j ] = min( cost[ i ][ j ], cost[ i ][ p ] + cost[ p ][ j ] );
					time[ i ][ j ] = min( time[ i ][ j ], time[ i ][ p ] + time[ p ][ j ] );
				}
			}
		}
		cin >> k;
		for( int i = 0; i < k; ++i ){
			int p, q, r;
			cin >> p >> q >> r;
			cout << ( r == 0 ? cost[ p ][ q ] : time[ p ][ q ] ) << endl;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T1, class T2> inline void minup(T1& m, T2 x){ if(m>x) m=static_cast<T2>(x); }
template<class T1, class T2> inline void maxup(T1& m, T2 x){ if(m<x) m=static_cast<T2>(x); }

#define INF 1000000000LL
#define MOD 1000000009
#define EPS 1E-9

const int MAX_V = 100;
ll dp[2][MAX_V][MAX_V];
int E, V;
int a, b;
ll c, t;
int k;
int p, q, r;

int main()
{
  cin >> E >> V;
  rep(i, V) rep(j, V) dp[0][i][j] = dp[1][i][j] = INF;

  rep(i, E){
    cin >> a >> b >> c >> t;
    a-=1;  b-=1;
    dp[0][a][b] = dp[0][b][a] = c;
    dp[1][a][b] = dp[0][b][a] = t;
  }

  rep(i, V) dp[0][i][i] = dp[1][i][i] = 0;
  rep(b, 2) rep(k, V) rep(i, V) rep(j, V) minup(dp[b][i][j], dp[b][i][k] + dp[b][k][j]);

  cin >> k;
  while(k--){
    cin >> p >> q >> r;
    cout << dp[r][p-1][q-1] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n,m;
  int a,b,c,d;
  int x;
  int p,q,r;
  int cost[111][111];
  int time[111][111];
  while(1){
    cin >> n >> m;
    if(n==0 && m==0)break;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cost[i][j]=999999;
	time[i][j]=999999;
      }
    }
    for(int i=0;i<n;i++){
      cin >> a >> b >> c >> d;
      cost[a-1][b-1]=c;
      cost[b-1][a-1]=c;
      time[a-1][b-1]=d;
      time[b-1][a-1]=d;
    }
    for(int k=0;k<n;k++){
      for(int i=0;i<n;i++){
	for(int j=0;j<n;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]);
	}
      }
    }
    cin >> x;
    for(int i=0;i<x;i++){
      cin >> p >> q >> r;
      if(r==0){
	cout << cost[p-1][q-1] << endl;
      }else{
	cout << time[p-1][q-1] << endl;
      }
    }
  }
}