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
			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
					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<stack>
#include<queue>
#include<map>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) for(int i=0;i<n;i++)

const int maxn=311,maxm=110,INF = 110000;
const int COST = 0, TIME = 1;

int n,m;

struct edge
{
    unsigned int to,cost;
    edge(int to,int cost):to(to),cost(cost){};
};

typedef pair<int,int> p;

int main()
{
    while(cin >> n >> m && (n||m))
    {
        vector<edge> cost[2][maxn];
        int a,b,tc,tt;
        REP(n)
        {
            cin >> a >> b >> tc >> tt;
            cost[TIME][a].push_back(edge(b,tt));
            cost[TIME][b].push_back(edge(a,tt));
            cost[COST][a].push_back(edge(b,tc));
            cost[COST][b].push_back(edge(a,tc));
        }

        int k; cin >> k;

        int start,goll,r;
        while(k--)
        {
            cin >> start >> goll >> r;
            int d[maxm] = {};
            memset(d,INF,sizeof(d));
            d[1] = 0;

            priority_queue<p,vector<p>, greater<p> > que;
            que.push(p(0,start) );

            while(!que.empty())
            {
                p tp = que.top(); que.pop();
                int v = tp.second; //確定ノード
                if(d[v] < tp.first) continue;
                REP(cost[r][v].size())
                {
                    edge e = cost[r][v][i];
                    if(d[e.to] > d[v] + e.cost)
                    {
                        d[e.to] = d[v] + e.cost;
                        que.push(p(d[e.to], e.to));
                    }
                }
            }
            cout << d[goll] << endl;
        }
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#define INF (1<<21)
using namespace std;
int cost[101][101];
int t_cost[101][101];
int main(){
  int n,m;
  while(cin >> n >> m , n+m ){
    m++;
    for(int i = 0  ; i < m ; i++ ){
      for(int j = 0  ; j < m ; j++ ){
	if(i == j){
	  cost[i][j] = 0;
	  t_cost[i][j] = 0;
	}else{
	  cost[i][j] = INF; 
	  t_cost[i][j] = INF; 
	}
      }
    }
      for(int i = 0 ; i < n ; i++ ){
	int a,b,c,d;
	cin >> a >> b >> c >> d;
	cost[a][b] = cost[b][a] = c;
	t_cost[a][b] = t_cost[b][a] = d;  
      }

      for(int i = 0 ; i < m ; i++ ){
	for(int j = 0 ; j < m ; j++ ){
	  for(int k = 0 ; k < m ; k++ ){
	    cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
	    t_cost[i][j] = min(t_cost[i][j],t_cost[i][k] + t_cost[k][j]);
	  }
	}
      }

      int k;
      cin >> k;
      for(int i = 0 ; i < k ; i++ ){
	int from,to,r;
	cin >> from >> to >> r; 
	if(r == 0){
	  cout << cost[from][to] << endl;
	} else {
	  cout << t_cost[from][to] << endl;
	}
      }
    }
    return 0;
  }

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#define INF 100000000
using namespace std;

int main(){
	int cost[100][100],tim[100][100];
	int n,m,l;
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0) break;
	for(int i = 0;i < m;i++){
		for(int j = 0;j < m;j++) {
			cost[i][j] = INF;
			tim[i][j] = INF;
		}
		cost[i][i] = 0;
		tim[i][i] = 0;
	}
	for(int i = 0;i < n;i++){
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		cost[a - 1][b - 1] = c;
		cost[b - 1][a - 1] = c;
		tim[a - 1][b - 1] = d;
		tim[b - 1][a - 1] = d;
	}
	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]);
				tim[i][j] = min(tim[i][j],tim[i][k] + tim[k][j]);
			}
		}
	}
	cin >> l;
	for(int i = 0;i < l;i++){
		int a,b,c;
		cin >> a >> b >> c;
		if(!c) cout << cost[a - 1][b - 1] << endl;
		else cout << tim[a - 1][b - 1] << endl;
	}
	}
	return 0;
}

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

using namespace std;

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;

int dijkstra(const Graph& g,int start,int goal)
{
	const int INFTY=1<<29;
	int size=g.size();
	vector<int> dist(size,INFTY);
	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));
		}
	}
	return dist[goal];
}

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));
		}
		int k; cin>>k;
		for(int i=0;i<k;i++){
			int p,q,r; cin>>p>>q>>r;
			int res=dijkstra(r==0?g1:g2,p-1,q-1);
			cout<<res<<endl;
		}
	}
	
	return 0;
}