Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 2000000000

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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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>

using namespace std;

int i, j, k, p, q, r, n, m, a, b, c, cost[101], t, dc[101][101], dt[101][101];

int dic(int d[101][101]) {
  cost[p] = 0;
  int min, f[101] = {};
  for(i = 0; i < m; i++) {
    f[p] = 1;
    for(j = 1; j <= m; j++) {
      if(f[j]) continue;
      if(cost[j] > cost[p] + d[p][j]) cost[j]  = cost[p] + d[p][j];
    }
    min = 200000000;
    for(j = 1; j <= m; j++ ) {
      if(f[j]) continue;
      if(min > cost[j]) min = cost[p = j];
    }
  }
  return cost[q];
}


int main() {
  for(i = 0; i <= 100; i++)
    for(j = 0; j <= 100; j++) {
      dc[i][j] = 100000000;
      dt[i][j] = 100000000;
    }
  
  cin >> n >> m;
  
  while(n--) {
    cin >> a >> b >> c >> t;
    dc[a][b] = dc[b][a] = c;
    dt[a][b] = dt[b][a] = t;
  }
  
  cin >> k;
  
  while(k--) {
    cin >> p >> q >> r;
    for(i = 0; i <= 100; i++) cost[i] = 1000000000;
    if(r) cout << dic(dt) << endl;
    else cout << dic(dc) << endl;
  }
  
  return 0;
}

C++: #include<cstdio>
#include<climits>
#include<algorithm>
using namespace std;
int main()
{
	int time[101][101],cost[101][101],n,m,i,j,k,a,b,c,d;
	while(scanf("%d%d",&n,&m),m)
	{
		for(i=0;i<101;++i)
		{
			for(j=0;j<101;++j)
			{
				time[i][j]=cost[i][j]=INT_MAX/2;
			}
		}
		for(i=0;i<n;++i)
		{
			scanf("%d%d%d%d",&a,&b,&c,&d),cost[a][b]=cost[b][a]=c,time[a][b]=time[b][a]=d;
		}
		for(k=1;k<=m;++k)
		{
			for(i=1;i<=m;++i)
			{
				for(j=1;j<=m;++j)
				{
					time[i][j]=min(time[i][j],time[i][k]+time[k][j]),
					cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
				}
			}
		}
		scanf("%d",&k);
		for(i=0;i<k;++i)
		{
			scanf("%d%d%d",&a,&b,&c);
			printf("%d\n",c?time[a][b]:cost[a][b]);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define INF 1e8
using namespace std;
int n,m,k,a,b,c,alt,p,q,r;
long long cost[101][101],tm[101][101],d[101];
bool used[101];

void dfunc(int s,int r){
	rep(i,101){
		used[i]=true;
		d[i]=INF;
	}
	d[s]=0;
	while(1){
		int mem=INF,now=-1;
		rep(i,101){
			if(used[i] && mem>d[i]){
				now=i;
				mem=d[i];
			}
		}
		if(now==-1)break;
		used[now]=false;
		rep(i,101){
			if(r==0){
				if(cost[now][i]!=INF)d[i]=min(d[i],d[now]+cost[now][i]);
			}else{
				if(tm[now][i]!=INF)d[i]=min(d[i],d[now]+tm[now][i]);
			}
		}
	}
}

int main(){
	while(cin>>n>>m,n||m){
		rep(i,101)rep(j,101){
			cost[i][j]=INF;
			tm[i][j]=INF;
		}
		rep(i,n){
			cin>>a>>b>>c>>alt;
			a--;
			b--;
			cost[b][a]=cost[a][b]=c;
			tm[b][a]=tm[a][b]=alt;
		}
		cin>>k;
		rep(i,k){
			cin>>p>>q>>r;
			dfunc(p-1,r);
			cout<<d[q-1]<<endl;
		}
	}
	return 0;
}

C++: #include<cmath>
#include<cctype>
#include<cstdlib>
#include<cstdio>
#include<climits>
#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<utility>
#include<queue>

using namespace std;
int n,m;
const int MAXN = 1000;
int t[MAXN][MAXN];
int c[MAXN][MAXN];

void solve(){
  for(int k = 0;k < n; ++k){
    for(int i = 0;i < n; ++i){
      for(int j = 0;j < n; ++j){
	t[i][j] = min(t[i][j],t[i][k] + t[k][j]);
	c[i][j] = min(c[i][j],c[i][k] + c[k][j]);
      }
    }
  }
}
const int INF = INT_MAX/3;
void init(){
  for(int i = 0;i < MAXN; ++i){
    for(int j = 0;j < MAXN;j++){
      c[i][j] = t[i][j] = INF;
    }
  }
  for(int i = 0;i < MAXN;i++){
    c[i][i] = t[i][i] = 0;
  }
}


int main(){
  ios::sync_with_stdio(false);

  while(cin >> n >> m && n+m > 0){
    init();
    int a,b,icost,itime;
    for(int i = 0;i < n;i++){
      cin >> a >> b >> icost >> itime;
      t[a][b] = t[b][a] = itime;
      c[a][b] = c[b][a] = icost;
    }
    solve();
    int k;
    cin >> k;
    for(int i = 0;i < k; ++i){
      int p,q,r;
      cin >> p >> q >> r;
      if(r == 1){
	cout << t[p][q] << endl;
      }else{
	cout << c[p][q] << endl;
      }
    }
  }
  return 0;
}

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

# define INF 1e6
# define MAX_V 100

int e, v, K;

int d[2][MAX_V][MAX_V];

int main(){
	while (1){
		for (int i = 0; i < v; ++i){
			for (int j = 0; j < v; ++j){
				d[0][i][j] = (i == j) ? 0 : INF;
				d[1][i][j] = (i == j) ? 0 : INF;
			}
		}
		int a, b, cost, time;
		std::cin >> e >> v;
		if ((e + v) == 0)
			break;
		for (int i = 0; i < e; ++i){
			std::cin >> a >> b >> cost >> time;
			d[0][a][b] = cost;
			d[0][b][a] = cost;
			d[1][a][b] = time;
			d[1][b][a] = time;
		}
		for (int k = 0; k < v; ++k){
			for (int i = 0; i < v; ++i){
				for (int j = 0; j < v; ++j){
					d[0][i][j] = std::min(d[0][i][j], d[0][i][k] + d[0][k][j]);
					d[1][i][j] = std::min(d[1][i][j], d[1][i][k] + d[1][k][j]);
				}
			}
		}
		int p, q, r;
		std::cin >> K;
		for (int i = 0; i < K; ++i){
			std::cin >> p >> q >> r;
			std::cout << d[r][p - 1][q - 1] << std::endl;
		}
	}
	return 0;
}