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 <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#define INF 100000000
typedef long long int lli;
using namespace std;
int dc[101][101];
int dt[101][101];

int main(){
	int n,m;
	while(cin>>n>>m&&n&&m){
		for(int i=0; i<101; ++i){
			for(int j=0; j<101; ++j){
				if(i==j){
					dc[i][j]=0;
					dt[i][j]=0;
				} else {
					dc[i][j]=INF;
					dt[i][j]=INF;
				}
			}
		}
		for(int i=0; i<n; ++i){
			int a,b,cost,time;
			cin >> a >> b >> cost >> time;
			dc[a][b]=cost;
			dc[b][a]=cost;
			dt[a][b]=time;
			dt[b][a]=time;
		}
		for(int k=1; k<=m; ++k){
			for(int i=1; i<=m; ++i){
				for(int j=1; j<=m; ++j){
					dc[i][j]=min(dc[i][j],dc[i][k]+dc[k][j]);
					dt[i][j]=min(dt[i][j],dt[i][k]+dt[k][j]);
				}
			}
		}
		int K;
		cin >> K;
		for(int i=0; i<K; ++i){
			int p,q,r;
			cin >> p >> q >> r;
			if(r==0){
				cout << dc[p][q] << "\n";
			} else {
				cout << dt[p][q] << "\n";
			}
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define MAX 1000000
int main(){
	int n, m, a, b, cost, ti, k, p, q, r, point = 0;
	int cost_[101][101], time_[101][101];
	while ( cin >> n >> m && (n != 0 && m != 0)){
		for (int i = 0; i < m; i++)
		for (int j = 0; j < m; j++) { cost_[i][j] = time_[i][j] = MAX; }
		for (int i = 0; i < n; i++){
			cin >> a >> b >> cost >> ti;
			cost_[a-1][b-1] = cost;
			time_[a-1][b-1] = ti;
		}
		for (int i = 0; i < m; i++){
			for (int j = 0; j < m; j++){
				for (int o = 0; o < m; o++){
					if ((cost_[j][i] != MAX && cost_[i][o] != MAX) &&
						(time_[j][i] != MAX && time_[i][o] != MAX)){
						cost_[j][o] = min(cost_[j][o], cost_[j][i] + cost_[i][o]);
						time_[j][o] = min(time_[j][o], time_[j][i] + time_[i][o]);
					}
				}
			}
		}cin >> k;
		for (int i = 0; i < k; i++){
			cin >> p >> q >> r;
			if (r != 1) cout << cost_[p-1][q-1] << endl;
			else cout << time_[p-1][q-1] << endl;
		}
	}
	return 0;
}

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;
}