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++: // 0200
#include <iostream>
#include <vector>
#include <queue>
#define  N   10000000
using namespace std;

struct rail{
	int st;
	int num;
};

int com_min(int p, int q, vector<vector<rail> > table){
	vector<int> store;
	for(int i=0;i<table.size();i++)
		store.push_back(N);

	store[p] = 0;
	queue<int> sta;
	sta.push(p);

	while(!sta.empty()){
		int x = sta.front();
		for(int i=0;i<table[x].size();i++){
			if(store[table[x][i].st] > store[x] + table[x][i].num){
				store[table[x][i].st] = store[x] + table[x][i].num;
				sta.push(table[x][i].st);
			}
		}
		sta.pop();
	}

	return store[q];
}

int main(){
	while(1){
		int m, n, a, b, cost, time;
		rail ra;
		vector<vector<rail> > c_table, t_table;

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

		c_table.resize(m+1);
		t_table.resize(m+1);

		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			ra.st = b;
			ra.num = cost;
			c_table[a].push_back(ra);
			ra.num = time;
			t_table[a].push_back(ra);
			ra.st = a;
			t_table[b].push_back(ra);
			ra.num = cost;
			c_table[b].push_back(ra);
		}

		int k, p, q, r;

		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;
			
			if(r == 0)
				cout<<com_min(p, q, c_table)<<endl;
			else if(r == 1)
				cout<<com_min(p, q, t_table)<<endl;
		}

	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

#define INF (1e9)

#define MAX_N 3000
#define MAX_M 100

using namespace std;

int cost[MAX_M + 1][MAX_M + 1][2];

int N,M;
int K;


void init(){
	for(int i = 0;i <= MAX_M;i++){
		for(int j = 0;j <= MAX_M;j++){
			for(int k = 0;k < 2;k++){
				if(i != j) cost[i][j][k] = INF;
			}
		}
	}

	return ;
}

void solve(){
	for(int k = 1;k <= M;k++){
		for(int i = 1;i <= M;i++){
			for(int j = 1;j <= M;j++){
				for(int h = 0;h < 2;h++){
					cost[i][j][h] = min(cost[i][j][h],cost[i][k][h] + cost[k][j][h]);
				}
			}
		}
	}


	return ;
}

int query(int u,int v,int r){
	return cost[u][v][r];
}

int main(){

	while(true){
		init();
		scanf("%d%d",&N,&M);
		if(N == 0 && M == 0) break;
		int _u,_v;
		int _c,_t;
		for(int i = 0;i < N;i++){
			scanf("%d%d%d%d",&_u,&_v,&_c,&_t);
			cost[_u][_v][0] = _c;
			cost[_u][_v][1] = _t;
		}
	
		solve();
	
		scanf("%d",&K);
		int _r;
		for(int i = 0;i < K;i++){
			scanf("%d%d%d",&_u,&_v,&_r);
			printf("%d\n",query(_u,_v,_r));
		}
	}

	return 0;

}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int INF = 1 << 25;

int cost[100][100], time[100][100];

int main() {
	int n, m, query;
	while (cin >> n >> m , n) {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				cost[i][j] = (i == j) ? 0 : INF;
				time[i][j] = (i == j) ? 0 : INF;
			}
		}

		while (n--) {
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a - 1][b - 1] = cost[b - 1][a - 1] = c;
			time[a - 1][b - 1] = time[b - 1][a - 1] = t;
		}

		for (int k = 0; k < m; k++) {
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < m; 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 >> query;
		while (query--) {
			int p, q, r;
			cin >> p >> q >> r;
			if (r == 0) {
				cout << cost[p - 1][q - 1] << endl;
			}
			else {
				cout << time[p - 1][q - 1] << endl;
			}
		}
	}
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<functional>
#include<stdio.h>
#define INF 1000000000//INF=∞
using namespace std;
typedef pair<int, int> P;//firstは最短距離,secondは頂点番号

const int MAX_M = 100;

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(MAX_M); //頂点間の距離情報(隣接リスト)
vector<vector<edge> > cost_graph(MAX_M);

int dijkstra(vector<vector<edge> >& G, 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 < G[v].size(); i++){
      //頂点vはG[v].size()個の行き先を持つ
      edge e = G[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 (scanf("%d%d", &n, &m), n || m) {
    V = m;

    for (int i = 0; i < MAX_M; i++) {
      time_graph[i].clear();
      cost_graph[i].clear();
    }

    for (int i = 0; i < n; i++) {
      int a, b, time, cost;
      scanf("%d%d%d%d", &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;
      scanf("%d%d%d", &p, &q, &r);
      p--; q--;
      if (p > q) swap(p, q);
      if (r == 0)
        printf("%d\n", dijkstra(time_graph, p, q));
      else
        printf("%d\n", dijkstra(cost_graph, p, q));
    }
  }
}