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 <cstring>
#include <queue>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

struct edge{
    int to,money,time;
};
struct state{
    int v,d;
    bool operator<(const state& rhs)const{
        return d>rhs.d;
    }
};

priority_queue<state> q;
vector<vector<edge>> G;
int d[101];

int main(){
    int n,m;
    while(cin>>n>>m,n|m){
        vector<vector<edge>>(m+1).swap(G);
        rep(i,n){
            int f,t,m,ti; cin>>f>>t>>m>>ti;
            G[f].push_back({t,m,ti});
            G[t].push_back({f,m,ti});
        }
        int k; cin>>k;
        rep(i,k){
            int a,b,c; cin>>a>>b>>c;
            priority_queue<state>().swap(q);
            for(int& i:d) i=1<<30;
            d[a]=0;
            q.push({a,0});
            while(q.size()){
                state sta=q.top(); q.pop();
                if(d[sta.v]<sta.d) continue;
                for(edge& e:G[sta.v]){
                    int cost=(c==0 ? e.money : e.time);
                    if(d[e.to]>d[sta.v]+cost){
                        d[e.to]=d[sta.v]+cost;
                        q.push({e.to,d[e.to]});
                    }
                }
            }
            cout<<d[b]<<endl;
        }
    }
}

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

struct Node{
    vector<int> to; //どのノードとつながっているか
    vector<int> cost; //エッジの料金
    vector<int> time; //エッジの時間

    bool done; //確定ノードか
    int minCost;
    int from;
};

void addEdge(int v, int u, int c, int t, struct Node *node){
    //uはvとつながっている
    node[u].to.push_back(v);
    //uとvのエッジのコスト
    node[u].cost.push_back(c);
    node[u].time.push_back(t);

    //無向グラフの場合は以下の処理
    //ノードvはuとつながってる
    node[v].to.push_back(u);
    //uとvのエッジのコスト
    node[v].cost.push_back(c);
    node[v].time.push_back(t);
}

void dijkstra(int n, int start, int end, int k, struct Node *node){
    for(int i=0; i<n; ++i){
        node[i].done = false;
        node[i].minCost = -1;
    }

    node[start].minCost = 0; //スタートまでのコストは0
    while(1){
        int doneNode = -1; //最新の確定したノード
        for(int i=0; i<n; ++i){
            if(node[i].done == true) continue; //i番目が確定ノードならcontinue
            if(node[i].minCost < 0) continue; //iまでのコストが不明の時continue
            //確定ノードがないかiまでのコストのほうが小さい時最新の確定ノードを更新
            if(doneNode < 0 || node[i].minCost < node[doneNode].minCost) doneNode = i;
        }
        if(doneNode == -1) break; //全ノードが確定したらbreak
        node[doneNode].done = true; //確定ノードにする
        for(int i=0; i<node[doneNode].to.size(); ++i){
            int to = node[doneNode].to[i];
            int cost = node[doneNode].minCost + (k==0?node[doneNode].cost[i]:node[doneNode].time[i]);
            //toは未訪のノード
            //またはtoへより小さいコストの経路だったらtoの最小コストを更新
            if(node[to].minCost < 0 || cost < node[to].minCost){
                node[to].minCost = cost;
                node[to].from = doneNode;
            }
        }
    }
}

int main(){
    int n, m;
    while(cin >> n >> m, n != 0 && m != 0){
        struct Node node[m];
        for(int i=0; i<n; ++i){
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;
            addEdge(a-1, b-1, cost, time, node);
        }
        int k;
        cin >> k;
        for(int i=0; i<k; ++i){
            int p, q, r;
            cin >> p >> q >> r;
            dijkstra(n, p-1, q-1, r, node);
            cout << node[q-1].minCost << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int INFTY=1<<29;

struct Edge{
	int src,dst,weight;
	Edge(){}
	Edge(int s,int d,int w):src(s),dst(d),weight(w){}
	bool operator<(const Edge& e)const{return weight<e.weight;}
	bool operator>(const Edge& e)const{return weight>e.weight;}
};
typedef vector<vector<Edge> > Graph;

void dijkstra(const Graph& g,int start,vector<int>& dist)
{
	priority_queue<Edge,vector<Edge>,greater<Edge> > pq;
	pq.push(Edge(-1,start,0));
	while(pq.size()){
		Edge cur=pq.top(); pq.pop();
		if(dist[cur.dst]!=INFTY) continue;
		dist[cur.dst]=cur.weight;
		for(int i=0;i<g[cur.dst].size();i++){
			Edge e=g[cur.dst][i];
			pq.push(Edge(e.src,e.dst,cur.weight+e.weight));
		}
	}
}

int main()
{
	for(int n,m;cin>>n>>m,n|m;){
		Graph g1(m),g2(m);
		for(int i=0;i<n;i++){
			int a,b,cost,time; cin>>a>>b>>cost>>time;
			a--; b--;
			g1[a].push_back(Edge(a,b,cost));
			g1[b].push_back(Edge(b,a,cost));
			g2[a].push_back(Edge(a,b,time));
			g2[b].push_back(Edge(b,a,time));
		}
		vector<vector<int> > w1(m,vector<int>(m,INFTY)),w2=w1;
		for(int i=0;i<m;i++){
			dijkstra(g1,i,w1[i]);
			dijkstra(g2,i,w2[i]);
		}
		int k; cin>>k;
		for(int i=0;i<k;i++){
			int p,q,r; cin>>p>>q>>r;
			int res=(r==0?w1:w2)[p-1][q-1];
			cout<<res<<endl;
		}
	}
	
	return 0;
}