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 <vector>
#define INF (int)1e9

void warshall_floyd(int m, int p, int q, std::vector<std::vector<int> > dp) { 
    for (int i = 0; i < m; i++) {
        dp[i][i] = 0;
    }
    for (int k = 0; k < m; k++) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                dp[i][j] = std::min(dp[i][j], dp[i][k] + dp[k][j]);
            }
        }

    }
    std::cout << dp[p][q] << std::endl;
    return;
}

int main(void) {
    int n;
    int m;
    int a;
    int b;
    int cost;
    int time;
    int k;
    int p;
    int q;
    int r;

    while (true) {
        std::cin >> n >> m;
        if (n == 0 && m == 0) {
            break; 
        }
        std::vector<std::vector<int> > vcost(m, std::vector<int>(m, INF));
        std::vector<std::vector<int> > vtime(m, std::vector<int>(m, INF));

        for (int i = 0; i < n; i++) {
            std::cin >> a >> b >> cost >> time;
            --a;
            --b;
            vcost[a][b] = cost; 
            vcost[b][a] = cost; 
            vtime[a][b] = time; 
            vtime[b][a] = time; 
        }

        std::cin >> k;
        for (int i = 0; i < k; i++) {
            std::cin >> p >> q >> r;
            --p;
            --q;
            if (r == 0) {
                warshall_floyd(m, p, q, vcost); 
            } else {
                warshall_floyd(m, p, q, vtime); 
            }
        }
    } return 0;
}

C++: #include <iostream>

#define N 101
#define INF 1e+9

using namespace std;
int main(void){
	int n,m;
	while(cin>>n>>m,n||m){
		int cost[N][N],time[N][N];
		for(int i=0;i<n;++i)for(int j=0;j<n;++j){cost[i][j]=time[i][j]=INF;}
		for(int i=0,a,b,c,t;i<n;++i){
			cin>>a>>b>>c>>t;
			cost[a][b]=cost[b][a]=c;
			time[a][b]=time[b][a]=t;
		}
		int k;cin>>k;
		for(int i=0,p,q,r;i<k;++i){
			cin>>p>>q>>r;
			if(r){//time
				int d[N];
				for(int j=0;j<n;++j) d[j]=INF;
				d[p]=0;
				bool f=false;
				for(;;){
					for(int j=0;j<n;++j){//from
						for(int k=0;k<n;++k){//to
							if(d[j]!=INF && d[j] + time[j][k] < d[k]){
								f==true;
								d[k] = d[j] + time[j][k];
							}
						}
					}
					if(f==false) break;
					f=false;
				}
				cout<<d[q]<<endl;
			}else{//cost
				int d[N];
				for(int j=0;j<n;++j) d[j]=INF;
				d[p]=0;
				bool f=false;
				for(;;){
					for(int j=0;j<n;++j){//from
						for(int k=0;k<n;++k){//to
							if(d[j]!=INF && d[j] + cost[j][k] < d[k]){
								f==true;
								d[k] = d[j] + cost[j][k];
							}
						}
					}
					if(f==false) break;
					f=false;
				}
				cout<<d[q]<<endl;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int n, m;
    while(cin >> n >> m, n!=0 && m!=0){
        int cost[101][101];
        int time[101][101];
        for(int i=0; i<101; ++i){
            for(int j=0; j<101; ++j) time[i][j] = cost[i][j] = (i==j)?0:100000000;
        }
        for(int i=0; i<n; ++i){
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            cost[a][b] = c;
            cost[b][a] = c;
            time[a][b] = t;
            time[b][a] = t;
        }
        for(int i=1; i<=m; ++i){
            for(int j=1; j<=m; ++j){
                for(int k=1; k<=m; ++k){
                    cost[j][k] = min(cost[j][k], cost[j][i]+cost[i][k]);
                    time[j][k] = min(time[j][k], time[j][i]+time[i][k]);
                }
            }
        }
        int k;
        cin >> k;
        for(int i=0; i<k; ++i){
            int p, q, r;
            cin >> p >> q >> r;
            cout << ((r==0)?cost[p][q]:time[p][q]) << endl;
        }
    }
    return 0;
}

C++: #include <algorithm>
#define MAX_M 100
#define INF (1<<21)
using namespace std;

int dp_cost[MAX_M][MAX_M];
int dp_time[MAX_M][MAX_M];
int m;

void warshall_froyd(){
  for(int k=0;k<m;k++){
    for(int i=0;i<m;i++){
      for(int j=0;j<m;j++){
        dp_cost[i][j] = min(dp_cost[i][j], dp_cost[i][k] + dp_cost[k][j]);
        dp_time[i][j] = min(dp_time[i][j], dp_time[i][k] + dp_time[k][j]);
      }
    }
  }
}

int main(){
  int n;
  cin >> n >> m;
  for(int i=0;i<m;i++){
    for(int j=0;j<m;j++){
      dp_time[i][j] = (i==j ? 0 : INF);
      dp_cost[i][j] = (i==j ? 0 : INF);
    }
  }

  for(int i=0;i<n;i++){
    int a,b,cost,time;
    cin >> a >> b >> cost >> time;
    a--;
    b--;
    dp_cost[a][b] = cost; dp_cost[b][a] = cost;
    dp_time[a][b] = time; dp_time[b][a] = time;
  }
  
  warshall_froyd();

  int k,p,q,r;
  cin >> k;
  for(int i=0;i<k;i++){
    cin >> p >> q >> r;
    p--; q--;
    cout << (r == 0 ? dp_cost[p][q] : dp_time[p][q]) << endl;
  }
  cin >> p >> q;
  return 0;
}