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>
#include <utility>
#include <queue>
#define INF 1000000
using namespace std;
typedef pair<int, int> P;
struct line {
  int to,time,cost;
};

vector<line> L[100];
int costv[100];
int timev[100];

void mincost(int from,int to) {
  priority_queue<P, vector<P>, greater<P> > que;
  for(int i=0;i<100;i++) {
    costv[i] = INF;
  }
  costv[from] = 0;
  que.push(P(0,from));
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int v = p.second;
    if(costv[v] < p.first) continue;
    for(int i=0;i < L[v].size();i++) {
      line l = L[v][i];
      if(costv[l.to] > costv[v] + l.cost) {
	costv[l.to] = costv[v] + l.cost;
	que.push(P(costv[l.to], l.to));
      }
    }
  }
  cout << costv[to] << endl;
}

void mintime(int from,int to) {
  priority_queue<P, vector<P>, greater<P> > que;
  for(int i=0;i<100;i++) {
    timev[i] = INF;
  }
  timev[from] = 0;
  que.push(P(0,from));
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int v = p.second;
    if(timev[v] < p.first) continue;
    for(int i=0;i < L[v].size();i++) {
      line l = L[v][i];
      if(timev[l.to] > timev[v] + l.time) {
	timev[l.to] = timev[v] + l.time;
	que.push(P(timev[l.to], l.to));
      }
    }
  }
  cout << timev[to] << endl;
}

int main() {
  int n,m,k;
  while(cin >> n >> m) {
    if(n==0&&m==0)break;
    for(int i=0;i<n;i++) { //input line
      line temp;
      int f,t,ti,co;
      cin >> f >> t >> co >> ti;
      temp.to = t;
      temp.time = ti;
      temp.cost = co;
      L[f].push_back(temp);
      temp.to = f;
      L[t].push_back(temp);
    }

    cin >> k;
    for(int i=0;i<k;i++) {
      int p,q,r;
      cin >> p >> q >> r;
      if(r == 0)
	mincost(p,q);
      else
	mintime(p,q);
    }
  }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

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

int main(void){  
  int cost[100][100]={0};
  int time[100][100]={0};
  int n,m,a,b,c,t,h,p,q,r;
  cin >> n >> m;
  REP(i,n){
    cin >> a >> b >> c >> t;
    cost[a-1][b-1] = cost[b-1][a-1] = c;
    time[a-1][b-1] = time[b-1][a-1] = t;
  }
  REP(i,m)
    REP(j,m){
      if(!cost[i][j]&&i!=j) cost[i][j] = 1000000000;
      if(!time[i][j]&&i!=j) time[i][j] = 1000000000;
    }
  REP(k,m)
    REP(i,m)
      REP(j,m){
        cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
        time[i][j] = min(time[i][j],time[i][k] + time[k][j]);
      }
  cin >> h;
  REP(i,h){
    cin >> p >> q >> r;
    if(r==0) cout << cost[p-1][q-1] << endl;
    else cout << time[p-1][q-1] << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<utility>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pi acos(-1)
#define all(v) v.begin(),v.end()
using namespace std;

const double eps = 1e-8;
const double INF = 1e5;

typedef pair<int,int> P;
typedef double Real;
typedef complex<Real> Point;
typedef vector<Point> G;

double cross(const Point& a,const Point& b){//calculate ad-bc(vector product)
	return imag(conj(a)*b);
}

struct L:public vector<Point>{//line data
	L(const Point &a,const Point &b){
		push_back(a);
		push_back(b);
	}
};

int main(){
	int n,m;
	while(1){
		cin>>n>>m;
		if(!n && !m)break;
		int cost[m+1][m+1],time[m+1][m+1];
		rep(i,m)rep(j,n)cost[i][j]=time[i][j]=INF;
		int a,b,c,t;
		rep(i,n){
			cin>>a>>b;
			cin>>cost[a][b]>>time[a][b];
		}
		rep(k,m+1){
			rep(i,m+1){
				rep(j,m+1){
					cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
					time[i][j]=min(time[i][j],time[i][k]+time[k][j]);
				}
			}
		}
		int k,p,q,r;
		cin>>k;
		rep(i,k){
			cin>>p>>q>>r;
			if(r==0){
				cout<<cost[p][q]<<endl;
			}else{
				cout<<time[p][q]<<endl;
			}
		}
	}
	return 0;
}