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]
					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 <cstdio>
#include <vector>
#include <utility>
#include <queue>
#include <functional>
#define N 101
#define INF 2000000000
using namespace std;

int dist[N];
typedef pair<int,int> pii;

std::vector<pii> e1[N];
std::vector<pii> e2[N];

int dijk1(int a,int b){
	dist[a]=0;
	std::priority_queue<pii> pq;
	for(int i=0;i<e2[a].size();i++){
		pq.push(e2[a][i]);
	}
	while(!pq.empty()){
		pii temp=pq.top();
		int now=temp.second;
		int c=temp.first;
		pq.pop();
		if(dist[now]!=INF)continue;
		dist[now]=c;
		if(now==b)break;
		for(int i=0;i<e2[now].size();i++){
			pq.push(make_pair(c+e2[now][i].first,e2[now][i].second));
		}
	}
	return -dist[b];
}

int dijk(int a,int b){
	dist[a]=0;
	std::priority_queue<pii> pq;
	for(int i=0;i<e1[a].size();i++){
		pq.push(e1[a][i]);
	}
	while(!pq.empty()){
		pii temp=pq.top();
		int now=temp.second;
		int c=temp.first;
		pq.pop();
		if(dist[now]!=INF)continue;
		dist[now]=c;
		if(now==b)break;
		for(int i=0;i<e1[now].size();i++){
			pq.push(make_pair(c+e1[now][i].first,e1[now][i].second));
		}
	}
	return -dist[b];
}

int main(void){
	int a,b,c,d;
	int n,m;
	while(cin>>n>>m,n||m){
		for(int i=0;i<n;i++){
			cin>>a>>b>>c>>d;
			e1[a].push_back(make_pair(-c,b));
			e2[a].push_back(make_pair(-d,b));
		}
		int k;
		cin>>k;
		while(k--){
			for(int i=1;i<=m;i++)dist[i]=INF;
			cin>>a>>b>>c;
			if(c) cout<<dijk1(a,b)<<endl;
			else cout<<dijk(a,b)<<endl;
			// for(int i=1;i<=m;i++)printf("dist[%d]:%d\n",i,dist[i]);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>

using namespace std;

int i, j, k, p, q, r, n, m, a, b, c, cost[101], t, dc[101][101], dt[101][101];

int dic(const int d[101][101]) {
  cost[p] = 0;
  int min, f[101] = {};
  for(i = 0; i < m; i++) {
    f[p] = 1;
    for(j = 0; j <= m; j++) {
      if(f[j]) continue;
      if(cost[j] > cost[p] + d[p][j]) cost[j]  = cost[p] + d[p][j];
    }
    min = 200000000;
    for(j = 0; j <= m; j++ ) {
      if(f[j]) continue;
      if(min > cost[j]) min = cost[p = j];
    }
  }
  return cost[q];
}


int main() {
  while(cin >> n >> m, n != 0 || m != 0) {
    for(i = 0; i <= 100; i++)
      for(j = 0; j <= 100; j++) {
	dc[i][j] = 100000000;
	dt[i][j] = 100000000;
      }
    while(n--) {
      cin >> a >> b >> c >> t;
      dc[a][b] = dc[b][a] = c;
      dt[a][b] = dt[b][a] = t;
    }
    cin >> k;
    while(k--) {
      cin >> p >> q >> r;
      for(i = 0; i <= 100; i++) cost[i] = 100000000;
      if(r) cout << dic(dt) << endl;
      else cout << dic(dc) << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
const int INF = 10000000;
const int N = 101;
int cost[N][N];
int tim[N][N];
using namespace std;
int main()
{
	int n, m, a, b, co, ti,r;
	while (1){
		cin >> n >> m;
		if (n == 0 && m == 0)break;
		for (int i = 0; i < n; i++){
			for (int i2 = 0; i2 < n; i2++){
				cost[i][i2] = INF;
				tim[i][i2] = INF;
			}
		}
		for (int i = 0; i < n; i++){
			cin >> a >> b >> co >> ti;
			cost[a][b] = co;
			cost[b][a] = co;
			tim[a][b] = ti;
			tim[b][a] = ti;
		}
		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]);
					tim[i][j] = min(tim[i][j], tim[i][k] + tim[k][j]);
				}
		cin >> m;
		for (int i = 0; i < m; i++){
			cin >> a >> b >> r;
			if (r == 0){
				cout << cost[a][b] << endl;
			}
			else
				cout << tim[a][b] << endl;
		}
	}
}

C++: #include<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)
#define MIN(a, b) ((a)<(b)?(a):(b))
#define INF 10e6
using namespace std;
 /*temp*/

int main(){
    int n, m, l;
    while(cin>>m>>n,n){
        ll cost[n][n],time[n][n];
        // set table
        REP(i,n){
            REP(j,n){
                cost[i][j]=time[i][j]=INF;
            }
            cost[i][i]=time[i][i]=0;
        }
        // read
        REP(i,n){
            int a,b,c,t;
            a--;
            b--;
            cost[a][b]=cost[b][a]=MIN(c,cost[a][b]);
            time[a][b]=time[b][a]=MIN(t,time[a][b]);
        }
        // Warshall???Floyd Algorithm
        REP(a,n){
            REP(b,a+1){
                REP(k,n){
                    cost[a][b]=cost[b][a]=MIN(cost[a][b],cost[a][k]+cost[k][b]);
                    time[a][b]=time[b][a]=MIN(time[a][b],time[a][k]+time[k][b]);
                }
            }
        }
        // 
        int q;
        cin>>q;
        REP(i,q){
            int s,d,type;
            cin>>s>>d>>type;
            s--;
            d--;
            cout<<type==0?cost[s][d]:time[s][d]<<'\n';
        }
    }
    return 0;
}

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;
		}
	}
}