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++: #include <cstdio>
#include <algorithm>

using namespace std;

#define chmin(a,b) a=min(a,b)
#define rep(i,x) for(int i=0;i<(x);++i)

const int inf = 1e9;

int N, M;
int mc[100][100], mt[100][100];

void wf()
{
    rep(k, M) rep(i, M) rep(j, M) {
        chmin(mc[i][j], mc[i][k] + mc[j][k]);
        chmin(mt[i][j], mt[i][k] + mt[j][k]);
    }
}

int main()
{
    while (scanf("%d %d", &N, &M), N || M) {
        fill(mc[0], mc[M], inf);
        fill(mt[0], mt[M], inf);

        rep(i, M) mc[i][i] = mt[i][i] = 0;

        rep(i, N) {
            int a, b, cost, time; scanf("%d %d %d %d", &a, &b, &cost, &time); a--; b--;
            mc[a][b] = mc[b][a] = cost;
            mt[a][b] = mt[b][a] = time;
        }

        wf();

        int K; scanf("%d", &K);
        rep(i, K) {
            int p, q, r; scanf("%d %d %d", &p, &q, &r); p--; q--;
            printf("%d\n", r == 0 ? mc[p][q] : mt[p][q]);
        }
    }
}

C++: #include "bits/stdc++.h"
using namespace std;

#define INF 1e9+7

int main(){
	int n,m;
	while(1){
		cin>>n>>m;
		if(n==0&&m==0)break;
		vector<vector<int> > cost(m,vector<int>(m,INF));
		vector<vector<int> > timea(m,vector<int>(m,INF));
		
		for(int i=0;i<n;i++){
			int a,b;
			cin>>a>>b;
			a--;
			b--;
			cin>>cost[a][b]>>timea[a][b];
		}
		
		for(int i=0;i<m;i++){
			cost[i][i]=0;
			timea[i][i]=0;
		}
		
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
					timea[i][j]=min(timea[i][j],timea[i][k]+timea[k][j]);
				}
			}
		}
		
		int k;
		cin>>k;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin>>p>>q>>r;
			p--;
			q--;
			if(r==0)cout<<cost[p][q]<<endl;
			if(r==1)cout<<timea[p][q]<<endl;
		}
	}
	return 0;
}

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

const int N = 110;
int timu[N][N];
int cost[N][N];

int main()
{
  int n,m;
  while(cin >> n >> m ,n|m)
  {
    for(int i=0;i<N;++i)
    for(int j=0;j<N;++j)
      timu[i][j] = cost[i][j] = 1<<8;

    for(int i=0;i<n;++i)
    {
      int a,b,c,t;
      cin >> a >> b >> c >> t;
      cost[a][b] = cost[b][a] = c;
      timu[a][b] = timu[b][a] = t;
    }

    for(int k=0;k<N;++k)
    for(int i=0;i<N;++i)
    for(int j=0;j<N;++j)
    {
      cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      timu[i][j] = min(timu[i][j], timu[i][k] + timu[k][j]);
    }

    int k;
    cin >> k;
    while(k--)
    {
      int p,q,r;
      cin >> p >> q >> r;
      if(r)
        cout << timu[p][q] << endl;
      else
        cout << cost[p][q] << endl;
    }
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<climits>
using namespace std;

int main(){
	int N, M, K;
	int cost[100][100], time[100][100];
	while(cin >> N >> M, N || M){
		fill_n( (int*)cost, 100*100, 1e6 );
		fill_n( (int*)time, 100*100, 1e6 );
		int A, B;
		for(int i = 0; i < N; i++){
			cin >> A >> B;
			A--; B--;
			cin >> cost[A][B] >> time[A][B];
			cost[B][A] = cost[A][B]; time[B][A] = time[A][B];
		}
		
		for(int k = 0; k < M; k++){
			for(int i = 0; i < M; i++){
				for(int j = 0; j < M; j++){
					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 >> K;
		int P, Q, R;
		for(int i = 0; i < K; i++){
			cin >> P >> Q >> R; P--; Q--;
			if(R == 0) printf("%d\n", cost[P][Q] );
			else printf("%d\n", time[P][Q] );
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <cstring>
#include <functional>

using namespace std;

#define REP(i,a,b) for((i)=(a);(i)<(int)(b);(i)++)
#define rep(i,n) REP(i,0,n)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

typedef pair<int,int> P;
const int INF = 1<<29;

vector<int> dijkstra(int m, int s, vector<vector<P> > G){
  int i;
  vector<int> d(m,INF);
  priority_queue<P,vector<P>,greater<P> > q;

  d[s] = 0;
  for(q.push(P(0,s)); !q.empty(); q.pop()){
    P p = q.top();
    if(p.first > d[p.second]) continue;

    rep(i,G[p.second].size()){
      P pt = G[p.second][i];
      if(d[pt.first] > d[p.second] + pt.second){
        d[pt.first] = d[p.second] + pt.second;
        q.push(P(d[pt.first], pt.first));
      }
    }
  }
  return d;
}

int main(){
  int i,j,k,e,m,n;
  vector<vector<P> > times,costs;

  while(scanf("%d %d",&e,&m)){
    if(e == 0 && m == 0) break;
    times.resize(m);
    costs.resize(m);

    while(e--){
      int from, to, cost, time;
      scanf("%d %d %d %d",&from,&to,&cost,&time);
      from--; to--;
      times[from].push_back(P(to,time));
      costs[from].push_back(P(to,cost));
    }

    scanf("%d",&n);
    while(n--){
      int s,g,bit;

      scanf("%d %d %d",&s,&g,&bit);
      s--; g--;

      if(bit == 0){
        vector<int> d = dijkstra(m,s,costs);
        printf("%d\n",d[g]);
      }
      if(bit == 1){
        vector<int> d = dijkstra(m,s,times);
        printf("%d\n",d[g]);
      }
    }
  }

  return 0;
}