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>
#include<algorithm>
#include<climits>
using namespace std;
const int INF = INT_MAX/3;
const int MAXN = 1000;
const int TIME = 1,COST = 0;
int graph[2][MAXN][MAXN];
int n,m,a,b,c,t;
void solve();
void init(){
  for(int i = 0;i < m;i++)
    for(int j = 0;j < m;j++)
      graph[0][i][j] =graph[0][j][i] = graph[1][j][i] = graph[1][i][j] = INF;
}
int main(){
  cin >> n >> m;
  init();
  for(int i = 0;i < n;i++){
    cin >> a >> b >> c >> t;
    graph[TIME][a-1][b-1] = graph[TIME][b-1][a-1] = t;
    graph[COST][a-1][b-1] = graph[COST][b-1][a-1] = c;
  }
  /*
  for(int i = 0;i < m;i++){
    for(int j = 0;j < m;j++){
      cout << graph[0][j][i] << ' ';
    }
    cout << endl;
  }
  */
  solve();
  /*
  for(int i = 0;i < m;i++){
    for(int j = 0;j < m;j++){
      cout << graph[0][j][i] << ' ';
    }
    cout << endl;
  }
  cout << endl;
  for(int i = 0;i < m;i++){
    for(int j = 0;j < m;j++){
      cout << graph[1][j][i] << ' ';
    }
    cout << endl;
  }
  */
  

  int X;
  cin >> X;
  for(int i = 0;i < X;i++){
    cin >> a >> b >> c;
    cout << graph[c][a-1][b-1] << endl;
  }
  return 0;
}

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

C++: #include<stdio.h>
#define INF 10000000
typedef struct{
	short to,cost,time;
}edge;
int n,m;
int i,j,k;
int p,q,r;
edge e[100][3000];
int ce[100];
bool used[100];
int d[100];

int min(int a,int b){return a<b?a:b;}

int solve(int p,int q,int r){
	int l;
	for(l=0;l<m;l++){
		used[l]=false;
		d[l]=INF;
	}
	d[p]=0;
	int next;
	while(1){
		next=-1;
		for(l=0;l<m;l++){
			if(!used[l]&&(next==-1||d[next]>d[l]))next=l;
		}
		if(next==-1)break;
		used[next]=true;
		for(l=0;l<ce[next];l++){
			if(r==0)d[e[next][l].to]=min(d[e[next][l].to],d[next]+e[next][l].cost);
			if(r==1)d[e[next][l].to]=min(d[e[next][l].to],d[next]+e[next][l].time);
		}
	}
	return d[q];
}

int main(){
	while(1){
		scanf("%d %d",&n,&m);
		if(n==0&&m==0)return 0;
		for(i=0;i<m;i++)ce[i]=0;
		for(i=0;i<n;i++){
			int a,b,cost,time;
			scanf("%d %d %d %d",&a,&b,&cost,&time);
			e[a-1][ce[a-1]].to=b-1;
			e[a-1][ce[a-1]].cost=cost;
			e[a-1][ce[a-1]].time=time;
			e[b-1][ce[b-1]].to=a-1;
			e[b-1][ce[b-1]].cost=cost;
			e[b-1][ce[b-1]].time=time;
			ce[a-1]++;
			ce[b-1]++;
		}
		scanf("%d",&k);
		int ans_c=INF,ans_t=INF;
		for(i=0;i<k;i++){
			scanf("%d %d %d",&p,&q,&r);
			if(r==0)ans_c=min(ans_c,solve(p-1,q-1,r));
			if(r==1)ans_t=min(ans_t,solve(p-1,q-1,r));
		}
		printf("%d\n%d\n",ans_c,ans_t);
	}
}

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

#define REP(i,k,n) for(int i=k;i<n;++i)
#define rep(i,n) REP(i,0,n)

int N,M,K;
const int MAXN=128;
const int INF=1e+9;
int G1[MAXN][MAXN];
int G2[MAXN][MAXN];

int main()
{
	while(cin>>M>>N&&(M||N))
	{
		rep(i,N){ rep(j,N){ G1[i][j]=G2[i][j]=(i==j?0:INF); } }

		rep(i,M)
		{ 
			int a,b,c,d;
			cin>>a>>b>>c>>d; --a; --b;
			G1[a][b]=G1[b][a]=c;
			G2[a][b]=G2[b][a]=d;
		}

		rep(point,N) rep(from,N) rep(to,N)
		{
			G1[from][to] = min(G1[from][to],G1[from][point]+G1[point][to]);
			G2[from][to] = min(G2[from][to],G2[from][point]+G2[point][to]);
		}

		cin>>K;
		rep(i,K)
		{
			int a,b,c; cin>>a>>b>>c; --a; --b;
			cout<<(c?G2[a][b]:G1[a][b])<<endl;
		}
	}

	return 0;
}

C++: #include<stdio.h>
#define INF 100001
int e1[100][100],e2[100][100];
int d[100];
bool used[100];
int m,n;
int p,q,r;
int k;
int i,j;
int a,b,t,c;
int v;
int mc,mt;

int min(int x,int y){
	if(x<=y)return x;
	else return y;
}

void solve(int s,int mo){
	for(j=0;j<m;j++){
		used[j]=false;
		d[j]=INF;
	}
	d[s]=0;
	while(true){
		v=-1;
		for(int u=0;u<m;u++)if(!used[u]&&(v==-1||d[u]<d[v]))v=u;
		if(v==-1)break;
		used[v]=true;
		for(int u=0;u<m;u++){
			if(mo==0)d[u]=min(d[u],d[v]+e1[v][u]);
			else d[u]=min(d[u],d[v]+e2[v][u]);
		}
	}
}


int main(){
	while(1){
		scanf("%d %d",&n,&m);
		if(n==0&&m==0)break;
		mt=INF;
		mc=INF;
		for(i=0;i<m;i++)for(j=0;j<m;j++){
			e1[i][j]=INF;
			e2[i][j]=INF;
		}
		for(i=0;i<n;i++){
			scanf("%d %d %d %d",&a,&b,&c,&t);
			e1[a-1][b-1]=c;
			e1[b-1][a-1]=c;
			e2[a-1][b-1]=t;
			e2[b-1][a-1]=t;
		}
		scanf("%d",&k);
		for(i=0;i<k;i++){
			scanf("%d %d %d",&p,&q,&r);
			solve(p-1,r);
			if(r==0)mc=min(mc,d[q-1]);
			else mt=min(mt,d[q-1]);
		}
		printf("%d\n%d\n",mc,mt);
	}
}