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

class Node{
public:
	vector< int > e_to;
	vector< int > e_cost;
	
	bool def;
	int min_cost;
	
	Node(){
		def = false;
		min_cost = -1;
	}
};
bool operator>(const Node& ot1, const Node& ot2){
	return ot1.min_cost > ot2.min_cost;
}


class Dijkstra{
	priority_queue< Node, vector< Node >, greater< Node > > que;
	vector< vector< Node > > nodes;
	int node_num;
	
public:
	void Init(int n){
		node_num = n;
		nodes.resize(node_num);
		for(int i = 0; i < node_num; ++i){
			nodes[i].resize(node_num);
			for(int j = 0; j < node_num; ++j){
				nodes[i][j] = Node();
			}
		}
	}
	
	void Put(int id, int to, int cost){
		for(int i = 0; i < node_num; ++i){
			nodes[i][id].e_to.push_back(to);
			nodes[i][id].e_cost.push_back(cost);
		}
	}
	
	int Run(int s, int t){
		
		if(nodes[s][t].min_cost != -1) return nodes[s][t].min_cost;
		
		nodes[s][s].min_cost = 0;
		que.push(nodes[s][s]);
		
		while(!que.empty()){
			Node def_node = que.top();
			que.pop();
		
			if(def_node.def)continue;
		
			def_node.def = true;
		
			for(int i = 0; i < def_node.e_to.size(); ++i){
				int to = def_node.e_to[i];
				int cost = def_node.min_cost + def_node.e_cost[i];
				
				if(nodes[s][to].min_cost == -1 || cost < nodes[s][to].min_cost){
					nodes[s][to].min_cost = cost;
					que.push(nodes[s][to]);
				}
			}
		}
		return nodes[s][t].min_cost;
	}
};

int main(){
	
	int n, m, a, b, c, t, k, p, q, r;
	Dijkstra dijk_cost, dijk_time;
	
	
	while(cin >> n >> m, n){
		dijk_cost.Init(m);
		dijk_time.Init(m);
		
		for(int i = 0; i < n; ++i){
			cin >> a >> b >> c >> t;
			
			dijk_cost.Put(a - 1, b - 1, c);
			dijk_cost.Put(b - 1, a - 1, c);
			
			dijk_time.Put(a - 1, b - 1, t);
			dijk_time.Put(b - 1, a - 1, t);
		}
		
		cin >> k;
		
		for(int i = 0; i < k; ++i){
			cin >> p >> q >> r;
			
			if(r == 0){
				cout << dijk_cost.Run(p - 1, q - 1) << endl;
			}else if(r == 1){
				cout << dijk_time.Run(p - 1, q - 1) << endl;
			}
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define INF 1e+9
using namespace std;

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

C++: #include<iostream>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define INF 1e6
using namespace std;

int main(){
	int n,m,a,b,cost,time,l,p,q,r;
	while(cin>>n>>m,n||m){
		int mt[m][m],mc[m][m];
		rep(i,m)rep(j,m)mt[i][j]=mc[i][j]=INF;
		rep(i,n){
			cin>>a>>b>>cost>>time;
			mt[a-1][b-1]=mt[b-1][a-1]=time;
			mc[a-1][b-1]=mt[b-1][a-1]=cost;
		}
		rep(k,n){
			rep(i,n){
				rep(j,n){
					mt[i][j]=min(mt[i][j],mt[i][k]+mt[k][j]);
					mc[i][j]=min(mc[i][j],mc[i][k]+mc[k][j]);
				}
			}
		}
		cin>>l;
		rep(i,l){
			cin>>p>>q>>r;
			if(r==0)cout<<mc[p-1][q-1]<<endl;
			else cout<<mt[p-1][q-1]<<endl;
		}
	}
	return 0;
}

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

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long


int cost[101][101];
int time_[101][101];

int main()
{
	int n, m;
	while (cin>>n>>m)
	{
		if (n == 0 && m == 0) break;
		REP(i, 101)REP(j, 101)
		{
				cost[i][j] = INF;
				time_[i][j] = INF;
		}

		REP(i, n)
		{
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			a--; b--;
			cost[a][b] = cost[b][a] = c;
			time_[a][b] = time_[b][a] = t;
		}

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