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>
using namespace std;

#define rep(i, n) for(int i = 0;i < n;i++)
#define rep2(i, n) for(int i = 1;i <= (int)n;i++)

int c[101][101];
int t[101][101];

int main() {
    while(true) {
        int n, m;
        cin >> n >> m;
        if(!n && !m) break;

        rep(i, 101) {
            rep(j, 101) {
                c[i][j] = t[i][j] = (i != j ? 1e9 : 0);
            }
        }

        rep(i, n) {
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;
            a--; b--;
            c[a][b] = c[b][a] = cost;
            t[a][b] = t[b][a] = time;
        }

        rep(i, m) {
            rep(j, m) {
                rep(k, m) {
                    //c[j][k] = min(c[j][k], c[j][i] + c[i][k]);
                    //t[j][k] = min(t[j][k], t[j][i] + t[i][k]);
                    c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
                    t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
                }
            }
        }

        int k;
        cin >> k;
        rep(i, k) {
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            if(r) cout << t[p][q];
            else cout << c[p][q];
            cout << endl;
            //cout << (r == 0 ? c[p][q] : t[p][q]) << endl;
        }
    }

    return 0;
}

C++: #include<iostream>
using namespace std;
int c[110][110];
int t[110][110];
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		for(int j=1;j<=m;j++){
			c[i][j]=t[i][j]=10000000;
		}
	}
	for(int i=0;i<n;i++){
		int a,b,cost,time;
		cin>>a>>b>>cost>>time;
		c[a][b]=c[b][a]=min(cost,c[a][b]);
		t[a][b]=t[b][a]=min(time,t[a][b]);
	}
	for(int k=1;k<=m;k++){
		for(int i=1;i<=m;i++){
			for(int j=1;j<=m;j++){
				c[i][j]=min(c[i][j],c[i][k]+c[k][j]);
				t[i][j]=min(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;
		cout<<(r?t[p][q]:c[p][q])<<endl;
	}
	cin>>n>>m;
	return 0;
}

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

using namespace std;
int zikan[101][101];
int cost[101][101];


int main(){
	
	int i,j,k,a,b,t,c;
	int rail, station;
	int q,start,goal,dotti;
	int temp;
	
	


	while (1){
		
		cin >> rail >> station;
		
		if (rail == 0 && station == 0){
			break;
		}


		for (i = 0; i < station+1; i++){
			for (j = 0; j < station+1; j++){
				zikan[i][j] = 1000000;
				cost[i][j] = 1000000;
			}
		}

		for (i = 0; i < rail; i++){
			cin >> a >> b >> c >> t;
			zikan[a][b] = t;
			zikan[b][a] = t;
			cost[a][b] = c;
			cost[b][a] = c;
		}


		cin >> q;

		for (;q>0;q--){
			cin >> start >> goal >> dotti;

			for (k = 1; k <= station; k++){
				for (i = 1; i <= station; i++){
					for (j = 1; j <= station; j++){
						cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					}
				}
			}

			for (k = 1; k <= station; k++){
				for (i = 1; i <= station; i++){
					for (j = 1; j <= station; j++){
						zikan[i][j] = min(zikan[i][j], zikan[i][k] + zikan[k][j]);
					}
				}
			}


			if (dotti == 0){
				cout << cost[start][goal] << endl;
			}

			if (dotti == 1){
				cout << zikan[start][goal] << endl;
			}


		}
	


	}



}