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]
					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 <queue>
#include <algorithm>
#include <functional>

using namespace std;
typedef pair<int, int> P;
static const int INF = 99999999;
struct SEdge
{
	int To;
	int Cost;
	int Time;
};
int V, E;
vector<int> d;
vector< vector<SEdge> > G;

void Dijkstra_Cost(int s)
{
	fill(d.begin(), d.end(), INF);
	d[s] = 0;
	priority_queue<P, vector<P>, greater<P> > PQue;
	PQue.push(P(0, s));
	while(!PQue.empty())
	{
		P p = PQue.top();
		PQue.pop();
		int v = p.second;
		if(d[v] < p.first)
		{
			continue;
		}
		for(int i = 0; i < G[v].size(); ++i)
		{
			SEdge e = G[v][i];
			if(d[e.To] > d[v] + e.Cost)
			{
				d[e.To] = d[v] + e.Cost;
				PQue.push(P(d[e.To], e.To));
			}
		}
	}
}

void Dijkstra_Time(int s)
{
	fill(d.begin(), d.end(), INF);
	d[s] = 0;
	priority_queue<P, vector<P>, greater<P> > PQue;
	PQue.push(P(0, s));
	while(!PQue.empty())
	{
		P p = PQue.top();
		PQue.pop();
		int v = p.second;
		if(d[v] < p.first)
		{
			continue;
		}
		for(int i = 0; i < G[v].size(); ++i)
		{
			SEdge e = G[v][i];
			if(d[e.To] > d[v] + e.Time)
			{
				d[e.To] = d[v] + e.Time;
				PQue.push(P(d[e.To], e.To));
			}
		}
	}
}

void solve()
{
	while(cin >> E >> V, E || V)
	{
		d.clear();
		d.resize(V);
		for(int i = 0; i < G.size(); ++i)
		{
			G[i].clear();
		}
		G.clear();
		G.resize(V);
		for(int i = 0; i < E; ++i)
		{
			int From, To, Cost, Time;
			cin >> From >> To >> Cost >> Time;
			--From;
			--To;
			SEdge e1, e2;
			e1.To = To;
			e2.To = From;
			e1.Cost = e2.Cost = Cost;
			e1.Time = e2.Time = Time;
			G[From].push_back(e1);
			G[To].push_back(e2);
		}
		int n;
		cin >> n;
		for(int i = 0; i < n; ++i)
		{
			int p, q, r;
			cin >> p >> q >> r;
			--p;
			--q;
			if(r == 0)
			{
				Dijkstra_Cost(p);
			}
			else
			{
				Dijkstra_Time(p);
			}
			cout << d[q] << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

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

#define INF 10000000

int t[101][101], s[101][101];

int main(void){
	int n,m;

	while(cin>>n>>m, n){
		for(int i=0;i<=m;i++) for(int j=0;j<=m;j++) t[i][j] = s[i][j] = INF;
		for(int i=0;i<n;i++){
			int a,b,cost,time;
			cin>>a>>b>>cost>>time;
			t[a][b] = t[b][a] = cost;
			s[a][b] = s[b][a] = time;
		}

		for(int k=1;k<=m;k++){
			for(int i=1;i<=m;i++){
				for(int j=1;j<=m;j++){
					t[i][j] = min(t[i][j],t[i][k]+t[k][j]);
					s[i][j] = min(s[i][j],s[i][k]+s[k][j]);
				}
			}
		}

		cin>>n;
		while(n--){
			int a,b,flg;
			cin>>a>>b>>flg;
			cout<<(flg?s[a][b]:t[a][b])<<endl;
		}
	}

	return 0;
}

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にする
      
      time_graph[a].push_back(edge(b, time));
      time_graph[b].push_back(edge(a, time));
      cost_graph[a].push_back(edge(b, cost));
      cost_graph[b].push_back(edge(a, cost));
    }

    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;
    }
  }
}