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>

using namespace std;

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

const int INF = 2<<10;

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>
#include <algorithm>
using namespace std;
const int INF = 1 << 25;

int cmin[100][100];
int tmin[100][100];

int main() {
	int n, m, query;
	while (cin >> n >> m , n) {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				cmin[i][j] = (i == j) ? 0 : INF;
				tmin[i][j] = (i == j) ? 0 : INF;
			}
		}

		while (n--) {
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cmin[a - 1][b - 1] = cmin[b - 1][a - 1] = c;
			tmin[a - 1][b - 1] = tmin[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++) {
					cmin[i][j] = min(cmin[i][j], cmin[i][k] + cmin[k][j]);
					tmin[i][j] = min(tmin[i][j], tmin[i][k] + tmin[k][j]);
				}
			}
		}

		cin >> query;
		while (query--) {
			int p, q, r;
			cin >> p >> q >> r;
			if (r == 0) {
				cout << cmin[p - 1][q - 1] << endl;
			}
			else {
				cout << tmin[p - 1][q - 1] << endl;
			}
		}
	}
}

C++: #include<iostream>
using namespace std;
int MAX=100000;
int main(){
  while(true){
    int n,m;
    cin>>n>>m;
    int a,b,cost,timer;
    int costs[m][m];
    int timers[m][m];
    for(int i=0;i<m;i++){
      for(int j=0;j<m;j++){
        costs[i][j]=MAX;
        timers[i][j]=MAX;
      }
      costs[i][i]=0;
      timers[i][i]=0;
    }

    for(int i=0;i<n;i++){
      cin>>a>>b>>cost>>timer;
      costs[a-1][b-1]=cost;
      timers[a-1][b-1]=timer;
      costs[b-1][a-1]=cost;
      timers[b-1][a-1]=timer;
    }
    for(int i=0;i<m;i++){
      for(int j=0;j<m;j++){
        for(int k=0;k<m;k++){
          if(costs[j][k]>(costs[j][i]+costs[i][k])){
            costs[j][k]=costs[j][i]+costs[i][k];
            costs[k][j]=costs[j][i]+costs[i][k];
          }
          if(timers[j][k]>(timers[j][i]+timers[i][k])){
            timers[j][k]=timers[j][i]+timers[i][k];
            timers[k][j]=timers[j][i]+timers[i][k];
          }
        }
      }
    }
    int k;
    int p;
    int q;
    bool r;
    cin>>k;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      if(r){
        cout<<timers[p-1][q-1]<<endl;
      }else if(!r){
        cout<<costs[p-1][q-1]<<endl;
      }
    }
  }
    return 0;
}