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>
#include<vector>
#include<map>
#include<queue>
#include<functional>
#define INF 1000000000//INF=∞
using namespace std;
typedef pair<int, int> P;//firstは最短距離,secondは頂点番号

struct edge {
  int to, cost;
  //edge() : to(0), cost(0) {}
  //edge(int to, int cost) : to(to), cost(cost) {}
};
int V;//頂点の数

vector<vector<edge> > time_graph; //頂点間の距離情報(隣接リスト)
vector<vector<edge> > cost_graph;

int dijkstra_time(int s, int g){
  priority_queue<P, vector<P>, greater<P> > que;
  vector<int> d(V, INF);//最短距離を記録する配列(∞で初期化しておく)
  d[s] = 0;//頂点sから頂点sへの最短距離は0で確定
  que.push(P(0, s));//距離0,頂点sからスタート

  while(!que.empty()){
    /*処理1*/
    //キューの中で最も頂点sからの距離が近い頂点vとその距離cを取得
    P p = que.top();
    que.pop();
    int v = p.second, c = p.first;

    /*処理2*/
    //頂点vの最短距離がすでに確定していたらcontenue
    if(d[v] < c)
      continue;

    /*処理3*/
    //頂点vの最短距離が既に確定していなければ頂点vへの最短経路はcで確定
      d[v]=c;

    /*処理4*/
    //頂点vから移動しうる頂点とその距離をキューに入れる
    for(int i = 0; i < time_graph[v].size(); i++){
      //頂点vはG[v].size()個の行き先を持つ
      edge e = time_graph[v][i];
      if(d[e.to] > d[v] + e.cost){
        que.push(P(d[v] + e.cost, e.to));
      }
    }
  }

  return d[g];//頂点gへの最短距離を返す
}


int dijkstra_cost(int s, int g){
  priority_queue<P, vector<P>, greater<P> > que;
  vector<int> d(V, INF);//最短距離を記録する配列(∞で初期化しておく)
  d[s] = 0;//頂点sから頂点sへの最短距離は0で確定
  que.push(P(0, s));//距離0,頂点sからスタート

  while(!que.empty()){
    P p = que.top();
    que.pop();
    int v = p.second, c = p.first;

    if(d[v] < c)
      continue;

      d[v]=c;

    for(int i = 0; i < cost_graph[v].size(); i++){
      edge e = cost_graph[v][i];
      if(d[e.to] > d[v] + e.cost){
        que.push(P(d[v] + e.cost, e.to));
      }
    }
  }

  return d[g];//頂点gへの最短距離を返す
}


int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    V = m;

    time_graph.clear();
    time_graph.resize(m);
    cost_graph.clear();
    cost_graph.resize(m);

    for (int i = 0; i < n; i++) {
      int a, b, time, cost;
      cin >> a >> b >> time >> cost;
      a--; b--; // 0-indexedにする
      edge e1,e2,e3,e4;
      e1.to = e3.to = b; e1.cost = e2.cost = time;
      e2.to = e4.to = a; e3.cost = e4.cost = cost;
      time_graph[a].push_back(e1);
      time_graph[b].push_back(e2);
      cost_graph[a].push_back(e3);
      cost_graph[b].push_back(e4);
    }

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--; q--;
      if (r == 0)
        cout << dijkstra_time(p, q) << endl;
      else
        cout << dijkstra_cost(p, q) << endl;
    }
  }
}

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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long INTEGER;
typedef double FLOAT;

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

const int MM = 100;
int cost[MM][MM];
int toki[MM][MM];

int main() {

  int n, m; cin >> m >> n;

  rep (i, n) {
    rep (j, n) {
      cost[i][j] = INF;
      toki[i][j] = INF;
    }
    cost[i][i] = 0;
    toki[i][i] = 0;
  }

  rep (i, m) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    --a; --b;
    cost[a][b] = cost[b][a] = c;
    toki[a][b] = toki[b][a] = t;
  }

  rep (k, n) {
    rep (i, n) {
      rep (j, n) {
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
        toki[i][j] = min(toki[i][j], toki[i][k] + toki[k][j]);
      }
    }
  }

  //rep (i,n) { rep(j,n) { cout << cost[i][j] << ' '; } cout << endl; }

  int k; cin >> k;
  rep (i, k) {
    int a, b, t;
    cin >> a >> b >> t;
    --a; --b;
    if (t) {
      cout << toki[a][b] << endl;
    } else {
      cout << cost[a][b] << endl;
    }
  }

  return 0;
}

C++: /*
 * src.cpp
 *
 *  Created on: 2016/07/29
 *      Author: joi
 */

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

const int MAX_M=100,INF=1e8;

int n,m,k;
int G[2][MAX_M][MAX_M];

void warshall_floyd(){
	REP(t,2){
		REP(k,m){
			REP(i,m){
				REP(j,m){
					G[t][i][j]=min(G[t][i][j],G[t][i][k]+G[t][k][j]);
				}
			}
		}
	}
}

int main(){
	do{
		scanf("%d %d",&n,&m);
		if (n || m){
			REP(t,2){
				REP(i,m){
					REP(j,m){
						G[t][i][j]=INF;
					}
				}
			}
			REP(t,2){
				REP(i,m){
					G[t][i][i]=0;
				}
			}
			REP(i,n){
				int a,b,c,t;
				scanf("%d %d %d %d",&a,&b,&c,&t);
				a--;
				b--;
				G[0][a][b]=G[0][b][a]=c;
				G[1][a][b]=G[1][b][a]=t;
			}
			warshall_floyd();
			scanf("%d",&k);
			REP(i,k){
				int p,q,r;
				scanf("%d %d %d",&p,&q,&r);
				p--;
				q--;
				printf("%d\n",G[r][p][q]);
			}
		}
	}while(n || m);
	return 0;
}