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++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

signed main() {
	int n, m;
	while (cin >> n >> m, n || m) {
		vector<vector<pair<int, int>>>d(m, vector<pair<int, int>>(m, pair<int, int>(INF, INF)));
		rep(i, 0, m)d[i][i] = make_pair(0, 0);
		int a, b, c, t;
		rep(i, 0, n) {
			cin >> a >> b >> c >> t; a--; b--;
			d[a][b] = d[b][a] = make_pair(c, t);
		}
		rep(k, 0, m) {
			rep(i, 0, m) {
				rep(j, 0, m) {
					d[i][j].first = min(d[i][j].first, d[i][k].first + d[k][j].first);
					d[i][j].second = min(d[i][j].second, d[i][k].second + d[k][j].second);
				}
			}
		}
		int k; cin >> k;
		int p, q, r;
		rep(i, 0, k) {
			cin >> p >> q >> r; p--; q--;
			if (r)cout << d[p][q].second << endl;
			else cout << d[p][q].first << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;

int main()
{
	for(int n,m;cin>>n>>m,n|m;){
		vvi c(m,vi(m,INFTY));
		rep(i,m)
			c[i][i]=0;
		vvi t=c;
		
		rep(i,n){
			int src,dst,cost,time;
			cin>>src>>dst>>cost>>time;
			src--;dst--;
			c[src][dst]=c[dst][src]=cost;
			t[src][dst]=t[dst][src]=time;
		}
		
		rep(k,m) rep(i,m) rep(j,m){
			c[i][j]=min(c[i][j],c[i][k]+c[k][j]);
			t[i][j]=min(t[i][j],t[i][k]+t[k][j]);
		}
		
		int query;
		cin>>query;
		rep(i,query){
			int src,dst,which;
			cin>>src>>dst>>which;
			src--;dst--;
			if(which)
				cout<<t[src][dst]<<endl;
			else
				cout<<c[src][dst]<<endl;
		}
	}
	
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
#define INF 1<<28
using namespace std;

struct edge{
  int to,cost;
};

typedef pair <int, int> P;

int n,m;
vector <edge> G[500][2];
int d[500][2];

void dijkstra(int s,int k){

  priority_queue <P, vector<P>, greater<P> > que;
  for(int i=0;i<n;i++) d[i][0] = d[i][1] = INF;

  d[s][k] = 0;
  que.push( P(0, s) );

  while( !que.empty() ){
    P p = que.top(); que.pop();
    int v = p.second;

    if(d[v][k] < p.first) continue;

    for(int i=0;i<G[v][k].size();i++){
      edge e = G[v][k][i];

      if(d[e.to][k] > e.cost + d[v][k]){
	d[e.to][k] = e.cost + d[v][k];
	que.push( P(d[e.to][k],e.to) );
      }
    }
  }
}

int main(){

  int a,b,cost,time,l;

  while(true){
    cin >> n >> m;
    if(!n && !m) break;
    
    edge z;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      
      z.to = b, z.cost = cost;
      G[a][0].push_back(z);
      z.cost = time;
      G[a][1].push_back(z);
      
      z.to = a, z.cost = cost;
      G[b][0].push_back(z);
      z.cost = time;
      G[b][1].push_back(z);
      
    }
    
    cin >> l;
    for(;l>0;l--){
      int c;
      cin >> a >> b >> c;
      dijkstra(a,c);
      cout << d[b][c] << endl;
    }
  
  }  
}

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

using namespace std;

#define inf 1000000000

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

class S{
	public:
	int iti,now;
	S(){}
	S(int a,int b){iti=a;now=b;}
	bool operator < (const S &a)const{
		return now>a.now;
	}
};
int dike(int st,int en,int va){
	priority_queue<S> state;
	int sima[101];
	int visit[101]={0};
	for(int i=0;i<101;i++){
		sima[i]=inf;
		visit[i]=0;
	}
	state.push(S(st,0));
	sima[st]=0;
	while(1){
		S u;
		u=state.top();
		state.pop();
		visit[u.iti]=1;
		if(u.iti==en){
			return u.now;
		}
		
		for(int i=1;i<=m;i++){
			if(C[u.iti][i]==inf||visit[i]==1){
				continue;
			}
				int cost=C[u.iti][i];
				if(va==1){
					cost=T[u.iti][i];
				}
			if(sima[u.iti]+cost < sima[i]){
				sima[i]=sima[u.iti]+cost;
				state.push(S(i,sima[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]=inf;
				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));
		}
	}
}