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>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define rep2(i, n) for(int i = 1; i <= (int)n; ++i)
#define repf(i, f, l) for(int i = f; i < l; ++i)
#define repit(it, t) for(__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++)

#define pb push_back
#define lb lower_bound
#define mp make_pair
#define ite iterator
#define ul unsigned long
#define ull unsigned long long
#define INF 1000000007
#define MOD 1000000007
#define EPS 1.0e-6
#define fs first
#define sd second

template<typename T>
bool elem(set<T> st, T val){
	return (st.find(val) != st.end());
}

template<typename T>
void printv(vector<T> vt){
    if(vt.size() == 0) return;
    rep(i, vt.size() - 1)
        cout << vt[i] << " ";
    cout << vt[vt.size() - 1] << endl;
}

typedef vector<int> vint;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef pair<int, int> pii;

int edge[2][301][301];
bool vst[301];
int main(void){
    ios::sync_with_stdio(false);
    while(true){
        int n, m;
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        rep(ii, 2) rep(i, m) rep(j, m) edge[ii][i][j] = -1;
        rep(i, n){
            int a, b, ed[2];
            cin >> a >> b >> ed[0] >> ed[1];
            a--; b--;
            rep(j, 2){
                edge[j][a][b] = ed[j];
                edge[j][b][a] = ed[j];
            }
        }
        int k;
        cin >> k;
        while(k-- > 0){
            rep(i, m) vst[i] = false;
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            set<pii> st;
            st.insert(mp(0, p));
            vst[p]++;

            int mn = 1000000009;
            while(!st.empty()){
                int  cs = (*st.begin()).fs;
                int cur = (*st.begin()).sd;
                st.erase(st.begin());
                if(cur == q){
                    mn = cs;
                    break;
                }
                vst[cur]++;
                rep(i, m)
                    if(edge[r][cur][i] > 0 && !vst[i])
                        st.insert(mp(cs + edge[r][cur][i], i));
            }
            cout << mn << "\n";
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 24;
int c[200][200];
int t[200][200];
int cost[200][200];
int tim[200][200];

int main(){
  int n, m, K;
  int p, q, r;
  while(cin >> n >> m && (n || m)){
    fill(&cost[0][0], &cost[199][199] + 1, INF);
    fill(&tim[0][0], &tim[199][199] + 1, INF);
    fill(&c[0][0], &c[199][199] + 1, INF);
    fill(&t[0][0], &t[199][199] + 1, INF);
    for(int i = 1; i <= m; i++){
      cost[i][i] = tim[i][i] = 0;
      c[i][i] = t[i][i] = 0;
    }
    for(int i = 0; i < n; i++){
      int a, b;
      int d, e;
      cin >> a >> b;
      cin >> d >> e;
      c[b][a] = min(c[b][a], d);
      c[a][b] = min(c[a][b], d);
      t[b][a] = min(t[b][a], e);
      t[a][b] = min(t[a][b], e);
    }
    for(int k = 1; k <= m; k++){
      for(int i = 1; i <= m; i++){
	for(int j = 1; j <= m; j++){
	  if(cost[i][j] > cost[i][k] + c[k][j]){
	    cost[i][j] = cost[i][k] + c[k][j];
	  }
	  if(tim[i][j] > tim[i][k] + t[k][j]){
	    tim[i][j] = tim[i][k] + t[k][j];
	  }
	}
      }
    }
    cin >> K;    
    for(int i = 0; i < K; i++){
      cin >> p >> q >> r;
      if(!r){
	cout << cost[p][q] << endl;
      }else{
	cout << tim[p][q] << endl;
      }
    }
  }
  return 0;
}

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

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