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<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;}
	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>
#include <algorithm>
#define INF (1<<25)
using namespace std;

int cost[2][128][128];

int n,m;

void warshall_floyd(){
  for(int i = 1 ; i < m ; i++ ){
    for(int j = 1 ; j < m ; j++ ){
      for(int k = 1 ; k < m ; k++ ){
	cost[0][j][k] = min(cost[0][j][k],cost[0][j][i] + cost[0][i][k]);
	cost[1][j][k] = min(cost[1][j][k],cost[1][j][i] + cost[1][i][k]);
      }
    }
  }
}

void input(){
  for(int i = 0 ; i < n ; i++ ){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    cost[0][a][b] = cost[0][b][a] = c;
    cost[1][a][b] = cost[1][b][a] = d;  
  }
}

void init(){
  for(int i = 0  ; i < 128 ; i++ ){
    for(int j = 0  ; j < 128 ; j++ ){
      if(i == j){
	cost[0][i][j] = 0;
	cost[1][i][j] = 0;
      }else{
	cost[0][i][j] = INF; 
	cost[1][i][j] = INF; 
      }
    }
  }
}

int main(){

  while(cin >> n >> m , n+m ){
    m++;
    init();
    input();
    warshall_floyd();
    int k;
    cin >> k;
    for(int i = 0 ; i < k ; i++ ){
      int from,to,r;
      cin >> from >> to >> r; 
      cout << cost[r][from][to] << endl;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define INF 1<<10

int main(){
  int cost[101][101];
  int times[101][101];
  int n,m;
  int a,b,c,t;
  while(cin>>n>>m,n|m){
    for(int i=0;i<101;i++){
      for(int j=0;j<101;j++){
	cost[i][j]=times[i][j]=INF;
      }
    }
    for(int i=0;i<n;i++){
      cin>>a>>b>>c>>t;
      cost[a][b]=cost[b][a]=c;
      times[a][b]=times[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]);
	  times[j][k]=min(times[j][k],times[j][i]+times[i][k]);
	}
      }
    }
    int k,p,q,r;
    cin>>k;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      cout<<(r==0 ? cost[p][q] : times[p][q] ) <<endl;
    }
  }
}

C++: #include <algorithm>
#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
	int n, m;
	while(cin >> n >> m, n && m) {
		int s[2][101][101];

		for(int i = 0; i < 101; ++i) {
			for(int j = 0; j < 101; ++j) {
				s[0][i][j] = (i == j) ? 0 : INF;
				s[1][i][j] = (i == j) ? 0 : INF;
			}
		}

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

		for(int i = 1; i <= m; ++i) {
			for(int j = 1; j <= m; ++j) {
				for(int k = 1; k <= m; ++k) {
					s[0][j][k] = min(s[0][j][k], s[0][j][i] + s[0][i][k]);
					s[1][j][k] = min(s[1][j][k], s[1][j][i] + s[1][i][k]);
				}
			}
		}

		int k;
		cin >> k;
		for(int i = 0; i < k; ++i) {
			int p, q, r;
			cin >> p >> q >> r;
			cout << s[r][p][q] << endl;
		}
	}
}

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]);
                }
            }
        }

        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;
}