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 <algorithm>
using namespace std;

const int INF = 1e+9;

int main()
{
  int n,m;
  while(cin >> n >> m, n||m)
  {
    int d[2][101][101];
    for(int i=0; i < 101; i++)
    {
      for(int j=0; j < 101; j++ )
      {
        d[0][i][j] = (i==j)? 0 : INF;
        d[1][i][j] = (i==j)? 0 : INF;
      }
    }

    for(int i=0; i < n; i++)
    {
      int a, b, cost,time;
      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=1; k <= m; k++)
    {
      for(int i=1; i <= m; i++)
      {
        for(int j=1; j <= m; j++)
        {
          d[0][i][j] = min(d[0][i][j], d[0][i][k]+d[0][k][j]);
          d[1][i][j] = min(d[1][i][j], d[1][i][k]+d[1][k][j]);
        }
      }
    }
    int k;
    cin >> k;
    for(int i=0; i < k; i++)
    {
      int p, q, r;
      cin >> p >> q >> r;
      cout << d[r][p][q] << endl;
    }
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

struct edge{
	int money;
	int time;
};

int main(){
	int n, m;
	int count;
	edge G[100][100];

	while(cin >> n >> m, n || m){
		for(int i=0; i < 100; i++){
			for(int j=0; j < 100; j++){
				G[i][j].money = 100000;
				G[i][j].time = 100000;
			}
		}

		for(int i=0; i < n; i++){
			int a, b, mo, t;
			cin >> a >> b >> mo >> t;

			G[a][b].time=t;
			G[a][b].money=mo;
			G[b][a].time=t;
			G[b][a].money=mo;
		}

		for(int k=0; k < n; k++){
			for(int i=0; i < n; i++){
				for(int j=0; j < n; j++){
					G[i][j].time = min(G[i][j].time, G[i][k].time+G[k][j].time);
					G[i][j].money = min(G[i][j].money, G[i][k].money+G[k][j].money);
				}
			}
		}

		cin >> count;
		for(int i=0; i < count; i++){
			int p;
			cin >> n >> m >> p;

			if(p==1) cout << G[n][m].time << endl;
			else cout << G[n][m].money << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
int main(void){
  int maze[2][100][100];
  int n,m;
  int a,b,c,t;
  while(cin>>m>>n,n+m){
    rep(i,n)rep(j,n)rep(k,2)maze[k][i][j]=100000;
    rep(i,m){
      cin>>a>>b>>c>>t;
      maze[0][--a][--b]=c;
      maze[1][a][b]=t;
      maze[0][b][a]=c;
      maze[1][b][a]=t;
    }
    rep(k,n)rep(i,n)rep(j,n)rep(l,2)
      maze[l][i][j]=min(maze[l][i][j],maze[l][i][k]+maze[l][k][j]);
    cin>>m;
    //cout<<endl;rep(i,n)rep(j,n)cout<<maze[1][i][j]<<(j==n-1?'\n':' ');
    
    rep(i,m){
      cin>>a>>b>>c;
      cout<<maze[c][--a][--b]<<endl;
    }
  }
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<map>
using namespace std;
#define fr first
#define sc second
typedef pair<int,int> Pi;
typedef pair<Pi,int> Pii;
struct edge{
  int to,cost,time;
};

int m;

typedef vector< vector < edge > > Graph;

int dijkstr_cost(Graph& info,int s,int g){
  //int min_cost[info.size()+1];
  int min_cost[m+1];
  priority_queue<Pi,vector<Pi>,greater<Pi> > que;

  //min_cost.resize(3,-1);

  for(int i=0;i<m+1;i++) min_cost[i]=-1;
  que.push(Pi(0/*現在コストor時間*/,s/*現在地*/));
  while(!que.empty()){

    Pi p = que.top();
    que.pop();

    //ゴールだったら。
    if(p.sc == g) return p.fr;

    //if(p.fr > min_cost[p.sc]) continue;
    
    for(int i=0;i<info[p.sc].size();i++){

      edge& e = info[p.sc][i];
      //移動先が-1(まだ行っていなかったら)
      if(min_cost[e.to]==-1 || min_cost[e.to] > p.fr+e.cost){
	min_cost[e.to] = p.fr+e.cost;
	que.push(Pi(p.fr + e.cost , e.to));
      }
    }

  }
  return -1;
}


int dijkstr_time(Graph& info,int s,int g){
  //int min_cost[info.size() + 1]; // n+1個
  int min_cost[m+1];
  priority_queue<Pi,vector<Pi>,greater<Pi> > que;

  //min_cost.resize(3,-1);
  for(int i=0;i<m+1;i++) min_cost[i]=-1;
  que.push(Pi(0/*現在コストor時間*/,s/*現在地*/));
  while(!que.empty()){
    Pi p = que.top();
    que.pop();
    //ゴールだったら。
    if(p.sc == g) return p.fr;

    //if(p.fr > min_cost[p.sc]) continue;
    
    for(int i=0;i<info[p.sc].size();i++){
      edge& e = info[p.sc][i];
      //移動先が-1(まだ行っていなかったら)
      if(min_cost[e.to]==-1 || min_cost[e.to] > p.fr+e.time){
	que.push(Pi(p.fr + e.time,e.to));
      }
    }
  }

  return -1;
}



int main(){
  int n;
  Graph rail;
  while(1){
    cin >> n >> m;
    if(n==0 && m==0) break;
    rail.resize(n+1);
    rail.clear();
    for(int i=0;i<n;i++){
      int a,b,cost,time;
      cin >> a >> b >> cost >> time;

      rail[a].push_back((edge){b,cost,time});
      rail[b].push_back((edge){a,cost,time});
    }
    /*
    cout << endl;
    for(int i=0;i<n;i++){
      for(int j=0;j<rail[i].size();j++){
	cout << j << " " << rail[i][j].to << " " << rail[i][j].cost << " " << rail[i][j].time << endl;
      }
      }*/

    int N;
    cin >> N;
    for(int i=0;i<N;i++){
      int p,q,r;
      cin >> p >> q >> r;
      if(r==0){
	cout << dijkstr_cost(rail,p,q) << endl;
      }
      else if(r==1){
	cout << dijkstr_time(rail,p,q) << endl;
      }
    }

  }
}