Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

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

class Node{
public:
	vector< int > e_to;
	vector< int > e_cost;
	
	int this_node;
	bool def;
	int min_cost;
	
	Node(){
		this_node = -1;
		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);
			nodes[i][id].this_node = id;
		}
	}
	
	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]);
				}
			}
			
			if(def_node.this_node == t) return nodes[s][t].min_cost;
		}
		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<cstring>
#include<algorithm>
using namespace std;
int x[1000][1000][2], n, m, a, b, c, d, q;

int path(int n, int s, int g) {
	int p[1000];
	memset(p, 127, sizeof(p));
	p[s] - 0;
	for (int i = 0; i < n; i++){
		for (int j = 1; j <= n; j++) {
			for (int k = 1; k <= n; k++) {
				if (x[j][k][1] < 10000000) {
					p[k] = min(p[k], p[j] + x[j][k][1]);
				}
			}
		}
	}
	return p[g];
}

int score(int n, int s, int g) {
	int p[1000];
	memset(p, 127, sizeof(p));
	p[s] - 0;
	for (int i = 0; i < n; i++) {
		for (int j = 1; j <= n; j++) {
			for (int k = 1; k <= n; k++) {
				if (x[j][k][0] < 10000000) {
					p[k] = min(p[k], p[j] + x[j][k][0]);
				}
			}
		}
	}
	return p[g];
}

int main() {
	while (true) {
		memset(x, 0, sizeof(x));
		cin >> m >> n;
		if (m == 0 && n == 0) { break; }
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> c >> d;
			x[a][b][0] = c;
			x[b][a][0] = c;
			x[a][b][1] = d;
			x[b][a][1] = d;
		}
		cin >> q;
		for (int i = 0; i < q; i++) {
			cin >> a >> b >> c;
			if (c == 0) { cout << score(n, a, b); }
			else { cout << path(n, a, b); }
		}
	}
}