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<functional>
 
constexpr int MAX_N = 100, INF = (int)1e8;
 
using P = std::pair<int, int>;
 
struct edge {
    int to, cost;
    edge() = default;
    edge(int _t, int _c) : to(_t), cost(_c) {}
};
 
int n;
std::vector<edge> G[2][MAX_N];//0:??????,1:??????
 
int d[MAX_N];
 
int dijkstra(int s, int g, int index) {
    std::fill(d, d + n, INF);
    d[s] = 0;
    std::priority_queue<P, std::vector<P>, std::greater<P>> que;
    que.emplace(d[s], s);
    while (!que.empty()) {
        P p = que.top();
        que.pop();
        if (p.second == g)
            return d[g];
        if (d[p.second] > p.first)
            continue;
        for (int i = 0; i < G[index][p.second].size(); ++i) {
            edge e = G[index][p.second][i];
            if (d[e.to] > d[p.second] + e.cost) {
                d[e.to] = d[p.second] + e.cost;
                que.emplace(d[e.to], e.to);
            }
        }
    }
    return -1;
}
 
int main() {
    while (1) {
        int m;
        std::cin >> m >> n;
        if (m + n == 0)
            break;
        int a, b, c, t;
        for (int i = 0; i < m; ++i) {
            std::cin >> a >> b >> c >> t;
            --a;
            --b;
            G[0][a].emplace_back(b, c);
            G[0][b].emplace_back(a, c);
            G[1][a].emplace_back(b, t);
            G[1][b].emplace_back(a, t);
        }
        int q, r;
        std::cin >> q;
        int p[2];
        for (int i = 0; i < q; ++i) {
            std::cin >> p[0] >> p[1] >> r;
            --p[0];
            --p[1];
            std::cout << dijkstra(p[0], p[1], r) << std::endl;
        }
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < n; ++j)
                G[i][j].clear();
        }
    }
    return 0;
}

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 i = 0; i < m; i++)
			for (int j = 0; j < m; j++)
				for (int k = 0; k < m; k++) {
					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;
}

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];
		      }
		      if(T[i][j]>T[i][k]+T[k][j]){
			T[i][j] = T[i][k]+T[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 << C[p-1][q-1] << endl;
		  }
		  else{
		    cout << T[p-1][q-1] << endl;
		  }
		}
	}

	return 0;
}