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<algorithm>
#include<iostream>

const int INF = 1 << 29;

int n, m;
int d[2][3000][3000];

int main()
{
	while( std::cin >> n >> m, n | m )
	{
		for( int i = 0; i != n; ++i )
			for( int j = 0; j != n; ++j )
				for( int k = 0; k != 2; ++k )
					d[k][i][j] = i == j ? 0 : INF;

		for( int i = 0; i != n; ++i )
		{
			int a, b, c, t;
			std::cin >> a >> b >> c >> t;
			d[0][a-1][b-1] = d[0][b-1][a-1] = c;
			d[1][a-1][b-1] = d[1][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 )
					for( int l = 0; l != 2; ++l )
						d[l][i][j] = std::min( d[l][i][j], d[l][i][k] + d[l][k][j] );

		int k;
		std::cin >> k;
		while( k-- )
		{
			int p, q, r;
			std::cin >> p >> q >> r;

			std::cout << d[r][p-1][q-1] << std::endl;
		}
	}

	return 0;	
}

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

typedef uint32_t ui;

static const ui INF = 1 << 30;

ui cost[101][101][2];

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    fill_n(cost[0][0], sizeof(cost) / sizeof(ui), INF);
    for (int i = 1; i <= m; i++) cost[i][i][0] = cost[i][i][1] = 0;
    while (n--) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      cost[a][b][0] = c;
      cost[b][a][0] = c;
      cost[a][b][1] = t;
      cost[b][a][1] = t;
    }

    for (int i = 1; i <= m; i++) {
      for (int k = i + 1; k <= m; k++) {
        for (int j = i + 1; j <= m; j++) {
          for (auto&& r : {0, 1}) {
            cost[i][j][r] = cost[j][i][r] = min(cost[i][k][r] + cost[k][j][r], cost[i][j][r]);
          }
        }
      }
    }

    int k;
    cin >> k;
    while (k--) {
      int p, q, r;
      cin >> p >> q >> r;
      cout << cost[p][q][r] << endl;
    }
  }
  return 0;
}

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

#define inf 1000000000

using namespace std;

int C[101][101],T[101][101];
	int n,m;

class sint{
	public:
	int n,m;
	sint(){}
	sint(int a,int b){n=a;m=b;}
	bool operator <(const sint &a) const{
		return m>a.m;
	}
};

int dike(int st,int en,int va){
	priority_queue<sint> state;
	
	int visit[101];
	int now[101];
	for(int i=0;i<101;i++){
		visit[i]=0;
		now[i]=inf;
	}
	
	state.push(sint(st,0));
	now[st]=0;
	
	while(1){
		sint u;
		u=state.top();
		state.pop();
		visit[u.n]=1;
		/*
		printf("come %d %d\n",u.n,u.m);
		for(int i=1;i<=m;i++){
			printf("%4d",now[i]);
		}
		puts("");
		for(int i=1;i<=m;i++){
			printf("%4d",visit[i]);
		}puts("");
		*/if(u.n==en){
			return u.m;
		}
		
		
		for(int i=1;i<=m;i++){
			int cost=C[u.n][i];
			if(va==1){
				cost=T[u.n][i];
			}
			if(cost==inf||visit[i]==1){
				continue;
			}
			if(cost+u.m < now[i]){
				now[i]=cost+u.m;
				state.push(sint(i,now[i]));
			}
		}
	}
}

int main(){
	
	while(1){
		cin>>n>>m;
		if(n==0){
			break;
		}
		for(int i=0;i<101;i++){
			for(int j=0;j<101;j++){
				C[i][j]=T[i][j]=inf;
			}
		}
		
		for(int i=0;i<n;i++){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			C[a][b]=C[b][a]=c;
			T[a][b]=T[b][a]=d;
		}
		
		int l;
		cin>>l;
		
		for(int i=0;i<l;i++){
			int a,b,c;
			cin>>a>>b>>c;
			printf("%d\n",dike(a,b,c));
		}
	}
}

C++: #include<iostream>
#define INF 2 << 23
using namespace std;
int main(){
  int n,m;
  while(cin >> n >> m){
    if(n==0&&m==0) break;
    int cost[m+1][m+1];
    int time[m+1][m+1];

    for(int i=0;i<=m;i++)
      for(int j=0;j<=m;j++){
        if(i==j)
          cost[i][j] = time[i][j] = 0;
        else
          cost[i][j] = time[i][j] = INF;
      }

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

    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;
    int p,q,r;
    for(int i=0;i<k;i++){
      cin >> p >> q >> r;
      switch(r){
      case 0: cout << cost[p][q] << endl; break;
      case 1: cout << time[p][q] << endl; break;
      default: break;
      }
    }
  }
}