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++: //2013-5

#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <time.h>
#define mp make_pair
#define INF 1e8
#define fs first
#define sc second
#define rep(x,n) for(int x = 0;x < n;x ++)
#define Rep(x,n) for(int x = 1;x <= n;x ++)
using namespace std;

int d[2][105][105]; //行き先、cost...0 time...1

int main(){
    int n,m;
    while(scanf("%d %d",&n,&m)&&(n!=0||m!=0){

    rep(i,m){
        rep(j,m){
            d[0][i][j]=INF;
            d[1][i][j]=INF;
        }
    }
    rep(i,m){
        d[0][i][i]=0;
        d[1][i][i]=0;
    }

    rep(i,n){
        int a,b,cost,time;
        scanf("%d %d %d %d",&a,&b,&cost,&time);
        a--;b--;
        d[0][a][b]=cost;
        d[0][b][a]=cost;
        d[1][a][b]=time;
        d[1][b][a]=time;
    }
    
    
    rep(i,2){
        rep(j,m){
            rep(k,m){
                rep(l,m){
                    d[i][k][l]=min(d[i][k][l],d[i][k][j]+d[i][j][l]);
                    if(d[i][k][l]>INF)
                        d[i][k][l]=INF;
                }
            }
        }
    }
    
    int k;
    scanf("%d",&k);
    rep(i,k){
        int p,q,r;
        scanf("%d %d %d",&p,&q,&r);
        p--;q--;
        printf("%d\n",d[r][p][q]);
    }
    }
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <stack>

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,b,n) for(int i=b;i<n;i++)
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1<<27);

using namespace std;

typedef long long ll;
typedef long double ld;

int n, m, p, q, r;
int C[101][101], V[101][101];

void floyd() {
  rep(k,m) {
    rep(i,m) {
      rep(j,m) {
        C[i][j] = min(C[i][j], C[i][k] + C[k][j]);
        V[i][j] = min(V[i][j], V[i][k] + V[k][j]);
      }
    }
  }
}

int main() {
  while(cin >> n >> m, n) {
    rep(i, m) rep(j, m) {
      C[i][j] = ((i==j)?0:INFTY);
      V[i][j] = ((i==j)?0:INFTY);
    }
    
    rep(i, n) {
      int tmp_i, tmp_j, ti, co;
      cin >> tmp_i >> tmp_j >> co >> ti;
      C[tmp_i-1][tmp_j-1] = co;
      V[tmp_i-1][tmp_j-1] = ti;
    }

    floyd();

    int k;
    cin >> k;

    rep(i,k) {
      cin >> p >> q >> r;
      if(r == 0)
        cout << C[p-1][q-1] << endl;
      else
        cout << V[p-1][q-1] << endl;
    }
  }
  
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<functional>
#define pb push_back
#define MAX_V 2000//場合によって変更
#define INF 1000000000
using namespace std;
typedef pair<int,int> P;//firstは最短距離,secondは頂点番号
struct edge{
	int to,cost;
};
int V;//頂点の数
vector<vector<edge> > G;
//-----------
vector<vector<edge> > G2;
//-----------
int dijkstra(int s,int g){
	priority_queue<P,vector<P> , greater<P> > que;
	vector<int> d(V,INF);
	d[s]=0;
	que.push(P(0,0));
	while(!que.empty()){
		P p=que.top();
		que.pop();
		int v=p.second;
		if(d[v]<p.first)
			continue;
		for(int i=0;i<G[v].size();i++){
			edge e=G[v][i];
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));
			}
		}
	}
	
	return d[g];
}
//-----------
int dijkstra2(int s,int g){
	priority_queue<P,vector<P> , greater<P> > que;
	vector<int> d(V,INF);
	d[s]=0;
	que.push(P(0,0));
	while(!que.empty()){
		P p=que.top();
		que.pop();
		int v=p.second;
		if(d[v]<p.first)
			continue;
		for(int i=0;i<G2[v].size();i++){
			edge e=G2[v][i];
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));
			}
		}
	}
	
	return d[g];
}
//--------------
int main(){
	int E;
	while(cin>>E>>V,E){
		G.clear();
		G.resize(V);
		//------------
		G2.clear();
		G2.resize(V);
		//---------------
		for(int i=0;i<E;i++){
			int x,y,c,t;
			cin>>x>>y>>c>>t;x--;y--;
			edge e;
			e.cost=c;
			e.to=y;
			G[x].pb(e);
			e.to=x;
			G[y].pb(e);
			//--------------
			e.cost=t;
			e.to=y;
			G2[x].pb(e);
			e.to=x;
			G2[y].pb(e);
			//---------------
		}
		int n;
		cin>>n;
		for(int i=0;i<n;i++){
			int s,t,q;
			cin>>s>>t>>q;
			if(q==0)
			cout<<dijkstra(s-1,t-1)<<endl;
			else
			cout<<dijkstra2(s-1,t-1)<<endl;
		}
	}
	
	return 0;
}