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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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<algorithm>
#define MAX 1000000
using namespace std;
int main(){
	int n, m, a, b, cost, ti, k, p, q, r, point = 0;
	int cost_[101][101], time_[101][101], k_[601];
	for (int i = 0; i < 101; i++){
		for (int l = 0; l < 101; l++){
			cost_[i][l] = MAX;
			time_[i][l] = MAX;
		}
	}
	cin >> n >> m;
	/*while (n != 0 && m != 0){*/
		for (int i = 0; i < n; i++){
			cin >> a >> b >> cost >> ti;
			cost_[a][b] = cost;
			time_[a][b] = ti;
		}
		cin >> k;
		for (int i = 0; i < k; i++){
			cin >> p >> q >> r;
			k_[point] = p;
			k_[point + 1] = q;
			k_[point + 2] = r;
			point += 3;
		}point = 0;
		for (int i = 1; i <= m; i++){
			for (int j = 1; j <= m; j++){
				for (int o = 1; 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]);
					}
				}
			}
		}
		for (int i = 0; i < k; i++){
			if (k_[point + 2] == 0){
				cout << cost_[k_[point]][k_[point + 1]] << endl;
			}
			else cout << time_[k_[point]][k_[point + 1]] << endl;
			point += 3;
		}point = 0;
		/*cin >> n >> m;
	}*/
	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 i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < n; k++) {
					if (C[j][k] > C[j][i] + C[i][k]) {
						C[j][k] = C[j][i] + C[i][k];
					}
					if (T[j][k] > T[j][i] + T[i][k]) {
						T[j][k] = T[j][i] + T[i][k];
					}
				}
			}
		}

		int k = 0;
		cin >> k;

		while(k--){
			int p, q, r;
			cin >> p >> q >> r;
			if (r == 0) {
				cout << C[p][q] << endl;
			}
			if (r == 1) {
				cout << T[p][q] << endl;
			}
		}

	}

	return 0;
}

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

constexpr int INF = 1000000000;

using Pair = std::pair<int, int>;

int n, m;
std::vector<Pair> cost[101];
std::vector<Pair> time_[101];

int d[101];
int dijkstra(int st, int gl, std::vector<Pair>* elm) {
    std::fill(d, d + 101, INF);

    d[st] = 0;

    std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> queue;

    queue.push({0, st});

    while(!queue.empty()) {
        auto p = queue.top();queue.pop();

        if(p.first > d[p.second]) {
            continue;
        }

        for(int i = 0; i < elm[p.second].size(); ++i) {
            auto e = elm[p.second][i];

            //To, cost

            if (d[e.first] > d[p.second] + e.second) {
                d[e.first] = d[p.second] + e.second;

                queue.push({d[e.first], e.first});
            }
        }
    }

    return d[gl];
}

int main() {
    while(std::cin >> n >> m, n + m) {
        int a, b, c, t;

        for(int i = 0; i < n; ++i) {
            cost[i].clear();
            time_[i].clear();
        }

        for(int i = 0; i < n; ++i) {
            std::cin >> a >> b >> c >> t;

            --a, --b;

            cost[a].push_back({b, c});
            cost[b].push_back({a, c});
            time_[a].push_back({b, t});
            time_[b].push_back({a, t});
        }

        int k;
        std::cin >> k;
        for(int i = 0; i < k; ++i) {
            int p, q, r;

            std::cin >> p >> q >> r;

            if(r == 0) {
                std::cout << dijkstra(p - 1, q - 1, cost) << std::endl;
            }else {
                std::cout << dijkstra(p - 1, q - 1, time_) << std::endl;
            }

        }
    }
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
const int MAX_V = 101;
int d[MAX_V][MAX_V][2];
int V;

void wrshall_floyd(){
    for (int k = 0; k < V; ++k) {
        for (int i = 0; i < V; ++i) {
            for (int j = 0; j < V; ++j) {
                d[i][j][0]=min(d[i][j][0],d[i][k][0]+d[k][j][0]);
                d[i][j][1]=min(d[i][j][1],d[i][k][1]+d[k][j][1]);
            }
        }
    }
}

int main(){
    for (int i = 0; i < MAX_V; ++i) {
        for (int j = 0; j < MAX_V; ++j) {
            if(i==j){
                d[i][j][0]=d[j][i][0]=d[i][j][1]=d[j][i][1]=0;

            } else {
                d[i][j][1]=d[j][i][0]=100000000;
            }
        }
    }
    int n,m;
    cin>>n>>m;
    V=n;
    for (int i = 0; i < n; ++i) {
        int a,b,c,t;
        cin>>a>>b>>c>>t;
        a--;b--;
        d[a][b][0]=d[b][a][0]=c;
        d[a][b][1]=d[b][a][1]=t;
    }
    wrshall_floyd();
    int k;cin>>k;
    for (int i = 0; i < k; ++i) {
        int t,f,r;cin>>t>>f>>r;
        t--;f--;
        cout<<d[t][f][r]<<endl;
    }
}