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 <stdio.h>
#include <limits.h>

const int INF = INT_MAX;

int main(void)
{
	int n, m, a, b, k, t;
	int tcost[105][105], mcost[105][105], node[105], used[105];
	int i, j, min, cost, time;
	
	while (1){
		scanf("%d %d", &n, &m);
		
		if (n == 0 && m == 0){
			break;
		}
		
		for (i = 0; i < m; i++){
			for (j = 0; j < m; j++){
				mcost[i][j] = INF;
				tcost[i][j] = INF;
			}
		}
		
		for (i = 0; i < n; i++){
			scanf("%d %d", &a, &b);
			a--; b--;
			scanf("%d %d", &cost, &time);
			
			if (cost < mcost[a][b]){
				mcost[a][b] = cost;
				mcost[b][a] = cost;
			}
			if (time < tcost[a][b]){
				tcost[a][b] = time;
				tcost[b][a] = time;
			}
			
		}
		
		scanf("%d", &k);
		
		for (i = 0; i < k; i++){
			scanf("%d %d %d", &a, &b, &t);
			a--; b--;
			for (j = 0; j < m; j++){
				node[j] = INF;
				used[j] = 0;
			}
			
			node[a] = 0;
			while (1){
				used[a] = 1;
				min = INF;
				
				for (j = 0; j < m; j++){
					if (t == 1){
						if (used[j] == 0 && tcost[a][j] != INF && node[j] > node[a] + tcost[a][j]){
							node[j] = node[a] + tcost[a][j];
						}
						if (used[j] == 0 && min > node[j]){
							min = j;
						}
					}
					else{
						if (used[j] == 0 && mcost[a][j] != INF && node[j] > node[a] + mcost[a][j]){
							node[j] = node[a] + mcost[a][j];
						}
						if (used[j] == 0 && min > node[j]){
							min = j;
						}
					}
				}
				a = min;
				if (a == b){
					break;
				}
			}
			printf("%d\n", node[b]);
		}
	}
	return (0);
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <utility>
using namespace std;

typedef pair<int,int> P;

#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define mp make_pair
#define pb push_back

struct edge {
  int to, cost, time;
  edge(int a, int b, int c): to(a), cost(b), time(c) {}
};

const int MAX_M = 100, INF = 10000000;
int n, m;

vector<edge> E[MAX_M+1];

void solve() {
  rep(i,MAX_M+1) E[i].clear();
  
  rep(i,n) {
    int a, b, cost, time; cin>>a>>b>>cost>>time;
    E[a].push_back(edge(b, cost, time));
    E[b].push_back(edge(a, cost, time));
  }
  int zz; cin>>zz;
  rep(z,zz) {
    int p, q, r; cin>>p>>q>>r;
    // dijkstra
    int d[MAX_M+1];
    fill(d, d+MAX_M, INF);
    priority_queue<P, vector<P>, greater<P> > que;
    que.push(P(0, p));
    d[p] = 0;
    while (!que.empty()) {
      P p = que.top(); que.pop();
      int td = p.first, ti = p.second;
      if (d[ti] < td) continue;
      rep(i,E[ti].size()) {
        edge e = E[ti][i];
        if (r == 0) {
          if (d[e.to] > d[ti] + e.cost) {
            d[e.to] = d[ti] + e.cost;
            que.push(P(d[e.to], e.to));
          } 
        } else {
          if (d[e.to] > d[ti] + e.time) {
            d[e.to] = d[ti] + e.time;
            que.push(P(d[e.to], e.to));
          }
        }
      }
    }
    cout<<d[q]<<endl;
  }
}

int main (int argc, char const* argv[]) {
  while (cin>>n>>m, n||m) {
    solve();
  }
  return 0;
}

C++: // 2014/06/16 Tazoe

#include <iostream>
using namespace std;

const int INF = 1000000000;

int main()
{
	while(true){
		int n, m;
		cin >> n >> m;

		if(n==0 && m==0)
			break;

		int C[100][100];
		int T[100][100];
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++){
				if(i==j){
					C[i][j] = T[i][j] = 0;
				}
				else{
					C[i][j] = T[i][j] = INF;
				}
			}
		}

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

			C[a-1][b-1] = C[b-1][a-1] = c;
			T[a-1][b-1] = T[b-1][a-1] = t;
		}
//???????????§????????£?????????
//????????????
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(C[i][j]>C[i][k]+C[k][j]){
						C[i][j]=C[i][k]+C[k][j];
					}
				}
			}
		}
//????????????
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(T[i][j]>T[i][k]+T[k][j]){
						T[i][j]=T[i][k]+T[k][j];
					}
				}
			}
		}
//???????????????
		int k;
		cin>>k;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin>>p>>q>>r;
			if(r==0)cout<<C[p-1][q-1]<<endl;
			if(r==1)cout<<T[p-1][q-1]<<endl;
		
		}
	//??????
	//	for(int i=0;i<k;i++){
	//		cout<<A[i]<<' ';
	//	}
//???????????????????????????
//	cout<<endl;
	}

	return 0;
}

C++: #include <iostream>

using namespace std;

int main() {
	int a, b, n, m, k, p, q, r, cost, time, datas[300][300][2];
	while (cin >> n >> m, n || m) {
		for (int y = 0; y < m; ++y)
			for (int x = 0; x < m; ++x)
				datas[y][x][0] = 10000, datas[y][x][1] = 10000;

		for (int x = 0; x < m; ++x)
			datas[x][x][0] = 0, datas[x][x][1] = 0;

		for (int i = 0; i < n; ++i) {
			cin >> a >> b >> cost >> time;
			--a, --b;
			datas[a][b][0] = cost, datas[a][b][1] = time;
			datas[b][a][0] = cost, datas[b][a][1] = time;
		}
		
		for (int i = 0; i < m; ++i)
			for (int j = 0; j < m; ++j)
				for (k = 0; k < m; ++k) {
					if (datas[j][i][0]+datas[i][k][0] < datas[j][k][0])
						datas[j][k][0] = datas[j][i][0]+datas[i][k][0];
					
					if (datas[j][i][1]+datas[i][k][1] < datas[j][k][1])
						datas[j][k][1] = datas[j][i][1]+datas[i][k][1];
				}
		
		cin >> k;
		for (int i = 0; i < k; ++i) {
			cin >> p >> q >> r;
			cout << datas[p-1][q-1][r] << endl;
		}

	}
	return 0;
}