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>
using namespace std;

const int INF = 1000000000;

int main()
{
	while(true){
		int n, m;
		cin >> n >> m;

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

		int C[100][100];
		int T[100][100];
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++){
				if(i==j){
					C[i][j] = T[i][j] = 0;
				}
				else{
					C[i][j] = T[i][j] = INF;
				}
			}
		}

		for(int i=0; i<n; i++){
			int a, b, c, t;
			cin >> a >> b >> c >> t;

			C[a-1][b-1] = C[b-1][a-1] = c;
			T[a-1][b-1] = T[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++){
					if(C[i][j]>C[i][k]+C[k][j]){
						C[i][j]=C[i][k]+C[k][j];
					}
				}
			}
		}

		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(T[i][j]>T[i][k]+T[k][j]){
						T[i][j]=T[i][k]+T[k][j];
					}
				}
			}
		}
		
		int k;
		cin>>k;
		int p,q,r;

		for(int i=0;i<k;i++){
			cin>>p>>q>>r;
			if(r==0){
				cout<<C[p-1][q-1]<<endl;
			}
			if(r==1){
				cout<<T[p-1][q-1]<<endl;
			}
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;

int map[2][101][101];
// 0...cost 1...time
int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) && n && m){
    rep(i, 101) rep(j, 101) rep(k, 2) map[k][j][i] = 999999999;
    int a, b, c, t;
    rep(i, n){
      scanf("%d%d%d%d", &a, &b, &c, &t);
      a--;b--;
      map[0][a][b] = map[0][b][a] = c;
      map[1][a][b] = map[1][b][a] = t;
    }
    rep(k, m) rep(i, m) rep(j, m) rep(l, 2) map[l][i][j] = min(map[l][i][j], map[l][i][k] + map[l][k][j]);

    int k, p, q, r;
    scanf("%d", &k);
    while(k--){
      scanf("%d%d%d", &p, &q, &r);
      p--;q--;
      printf("%d\n", map[r][p][q]);
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#include <utility>

typedef long long ll;
typedef std::pair<int, int> P;
using namespace std;

struct edge{int to,cost;};

int n,m;
vector<edge> c[1005];
vector<edge> t[1005];
int d[1005];

void dt(int s){
	fill(d,d+1005,INT_MAX/2);
	priority_queue<P, vector<P>, greater<P> > que;
	d[s] = 0;	
	que.push(P(0,s));

	while(!que.empty()){
		P p = que.top(); que.pop();
		int v = p.second;
		if(d[v] < p.first) continue;
		for (int i = 0;i < t[v].size();i++){
			edge e = t[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));
			}
		}
	}
}

void dc(int s){
	fill(d,d+1005,INT_MAX/2);
	priority_queue<P, vector<P>, greater<P> > que;
	d[s] = 0;	
	que.push(P(0,s));

	while(!que.empty()){
		P p = que.top(); que.pop();
		int v = p.second;
		if(d[v] < p.first) continue;
		for (int i = 0;i < c[v].size();i++){
			edge e = c[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));
			}
		}
	}
}


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0) break;
		for (int i = 0;i < 1005;i++){
			c[i] = vector<edge>(0);
			t[i] = vector<edge>(0);
		}
		int t1,t2,t3,t4;
		for (int i = 0;i < n;i++){
			cin >> t1 >> t2 >> t3 >> t4;
			c[t1].push_back((edge){t2,t3});
			t[t1].push_back((edge){t2,t4});
			c[t2].push_back((edge){t1,t3});
			t[t2].push_back((edge){t1,t4});
		}

		int k;
		cin >> k;

		for (int i = 0;i < k;i++){
			int p,q,r; cin >> p >> q >> r;
			if(r == 1) dt(p);
			else dc(p);
			cout << d[q] << endl;
		}
	}

	return 0;

}

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[start] = 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; //確定ノード
                //cout << v << endl;
                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;
}