Go: // Warshall–Floyd Algorithm

package main

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

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
				} else {
					c_dist[i][j] = c_matrix[i][j]
				}
				if c_matrix[i][j] == 0{
					c_prev[i][j] = -1
				} else {
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0{
					t_dist[i][j] = INF
				} else {
					t_dist[i][j] = t_matrix[i][j]
				}
				if t_matrix[i][j] == 0{
					t_prev[i][j] = -1
				} else {
					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 <queue>
#include <vector>
#include <string.h>
#include <time.h>

using namespace std;

static const int TYPE_COST =0;
static const int TYPE_TIME =1;
static const int INF =100000000;

class station{
 public:
	int id;
	int cost;
	bool visited;
	vector<int> edge_to;
	vector<int> edge_cost;
	vector<int> edge_time;
	
	 public:
		station(int i) :  id(i), cost(INF), visited(false){}
		void add_to(int to, int cost, int time)
		{
			this->edge_to.push_back(to);
			this->edge_cost.push_back(cost);
			this->edge_time.push_back(time);
		}
};

int dijkstra(vector<station>, int size, int from, int to, int type);
void reset(vector<station> sta, int size);

int main(int argc, char** argv)
{
	while( 1 ){
		int n, m, from, to, cost, time, type;
		cin >> n;
		cin >> m;
		if(n==0 && m==0) break;
		
		vector<station> sta;
		for(int i=0; i<m; i++){
			sta.push_back(station(i));
		}
		
		for(int i=0; i<n; i++){
			cin >> from;
			cin >> to;
			cin >> cost;
			cin >> time;
			sta[from-1].add_to(to-1, cost, time);
			sta[to-1].add_to(from-1, cost, time);
		}
		cin >> n;
		for(int i=0; i<n; i++){
			cin >> from;
			cin >> to;
			cin >> type;
			clock_t t1 = clock();
			cout << dijkstra(sta, m, from-1, to-1, type) << endl;
			clock_t t2 = clock();
			cout << "Time : "  << t2 - t1<< " (msec)" << endl;
			reset(sta, m);
		}
	}
	
	return 0;
}

struct station_compare
{
	bool operator () (station s1, station s2)
	{
		return s1.cost - s2.cost;
	}
};

int dijkstra(vector<station> sta, int size, int start, int goal, int type)
{
	priority_queue<station, vector<station>, station_compare> que;
	sta[start].cost = 0;
	que.push(sta[start]);
	
	while(!que.empty())
	{
		station s = que.top(); que.pop();
		if(sta[s.id].cost < s.cost) continue;
		for(int i=0; i<s.edge_to.size(); i++)
		{
			int to = s.edge_to[i];
			int cost = (type==TYPE_COST) ? s.cost + s.edge_cost[i] : s.cost + s.edge_time[i];
			if(sta[to].cost > cost)
			{
				sta[to].cost = cost;
				que.push(sta[to]);
			}
		}
	}
			
	return sta[goal].cost;		
}

void reset(vector<station> sta, int size)
{
	for(int i=0; i<size; i++){
		sta[i].visited = false;
		sta[i].cost = -1;
	}
}

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

using namespace std;

#define fee first
#define time second
#define ALL(T) T.begin(),T.end()

const int INF = 1<<24;

int main(){
	int n, m;
	while (cin >> n >> m, n, m) {
		vector< vector<pair<int, int> > > lines(m, vector<pair<int, int> >(m));
		for (int i = 0; i < m; i++)
			for (int j = 0; j < m; j++)
				lines[i][j].fee = lines[i][j].time = INF;
		for (int i = 0; i < n; i++) {
			int a, b; cin >> a >> b;
			cin >> lines[a-1][b-1].fee >> lines[a-1][b-1].time;
			lines[b-1][a-1].fee = lines[a-1][b-1].fee;
			lines[b-1][a-1].time = lines[a-1][b-1].time;
		}

		for (int k = 0; k < m; k++)
			for (int i = 0; i < m; i++)
				for (int j = 0; j < m; j++) {
					lines[i][j].fee = min(lines[i][j].fee, lines[i][k].fee+lines[k][j].fee);
					lines[i][j].time = min(lines[i][j].time, lines[i][k].time+lines[k][j].time);
				}

		int k; cin >> k;
		for (int i = 0; i < k; i++) {
			int p, q, r; cin >> p >> q >> r;
			if (r == 0) { cout << lines[p-1][q-1].fee << endl; }
			else { cout << lines[p-1][q-1].time << endl; }

			// if (r == 0) { // minimum fee
			// 	int cur = q;
			// 	vector<int> cost(m);
			// 	for (int i = 0; i < m; i++) { cost[i] = -1; }
			// 	vector<int> visited(m);
			// 	vector<vector<int> > graph(m);

			// 	while (count(ALL(visited), 0)) {
			// 		vector<int> list;
			// 		for (int i = 0; i < m; i++) {
			// 			if (lines[cur][i].fee != -1) {
			// 				list.push_back(i);
			// 			}
			// 		}
			// 		int I;
			// 		for (int i = 0; i < list.size(); i++) {
			// 			if (cost[list[i]] > cost[cur] + lines[cur][i].fee) {
			// 				cost[list[i]] = cost[cur] + lines[cur][i].fee;
			// 				I = list[i];
			// 			}
			// 		}
			// 		visited[cur] = 1;
			// 		cur = I;
			// 	}
			// 	cout << cost[p] << endl;
			// } else if (r == 1) { // minimum time
			// 	int cur = q;
			// 	vector<int> cost(m);
			// 	for (int i = 0; i < m; i++) { cost[i] = -1; }
			// 	vector<int> visited(m);
			// 	vector<vector<int> > graph(m);

			// 	while (count(ALL(visited), 0)) {
			// 		vector<int> list;
			// 		for (int i = 0; i < m; i++) {
			// 			if (lines[cur][i].time != -1) {
			// 				list.push_back(i);
			// 			}
			// 		}
			// 		int I;
			// 		for (int i = 0; i < list.size(); i++) {
			// 			if (cost[list[i]] > cost[cur] + lines[cur][i].time) {
			// 				cost[list[i]] = cost[cur] + lines[cur][i].time;
			// 				I = list[i];
			// 			}
			// 		}
			// 		visited[cur] = 1;
			// 		cur = I;
			// 	}
			// 	cout << cost[p] << endl;
			// }

		}
	}

	return 0;
}