Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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
					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>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <stack>

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,b,n) for(int i=b;i<n;i++)
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1<<27);

using namespace std;

typedef long long ll;
typedef long double ld;

int n, m, p, q, r;
int C[101][101], V[101][101];

void floyd() {
  rep(k,m) {
    rep(i,m) {
      rep(j,m) {
        C[i][j] = min(C[i][j], C[i][k] + C[k][j]);
        V[i][j] = min(V[i][j], V[i][k] + V[k][j]);
      }
    }
  }
}

int main() {
  while(cin >> n >> m, n) {
    rep(i, m) rep(j, m) {
      C[i][j] = ((i==j)?0:INFTY);
      V[i][j] = ((i==j)?0:INFTY);
    }
    
    rep(i, n) {
      int tmp_i, tmp_j, ti, co;
      cin >> tmp_i >> tmp_j >> co >> ti;
      C[tmp_i-1][tmp_j-1] = co;
      C[tmp_j-1][tmp_i-1] = co;
      V[tmp_i-1][tmp_j-1] = ti;
      V[tmp_j-1][tmp_i-1] = ti;      
    }

    floyd();

    int k;
    cin >> k;

    rep(i,k) {
      cin >> p >> q >> r;
      if(r == 0)
        cout << C[p-1][q-1] << endl;
      else
        cout << V[p-1][q-1] << endl;
    }
  }
  
  return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cstdio>
using namespace std;
const int INF = 1 << 20;

int main()
{
	bool jud[128];
	int rail, stat, cost[128][128], time[128][128], start, goal, kind, route[128], exp[128], num;
	int wk, tk, kt, kr;
	while(cin >> rail >> stat)
	{
		if(!rail && !stat) break;
		for(int i = 0; i < stat; i++)
		{
			for(int j = 0; j < stat; j++)
			{
				cost[i][j] = INF;
				time[i][j] = INF;
			}
		}

		for(int i = 0; i < rail; i++)
		{
			cin >> wk >> tk >> kt >> kr;
			cost[wk-1][tk-1] = kt;
			cost[tk-1][wk-1] = kt;
			time[wk-1][tk-1] = kr;
			time[tk-1][wk-1] = kr;
		}

		cin >> num;

		for(int m = 0; m < num; m++)
		{
			for(int i = 0; i < stat; i++)
			{
				jud[i] = false;
				route[i] = INF;
				exp[i] = INF;
		
			}

			cin >> start >> goal >> kind;
			if(!kind)//min length
			{
				route[start - 1] = 0;
				while(1)
				{	
					int v = -1;
					for(int u = 0; u < stat; u++)
					{
						if(!jud[u] && (v == -1 || route[u] < route[v]) ) v = u;
					}

					if(v == -1) break;

					jud[v] = true;

					for(int u = 0; u < stat; u++)
					{
						route[u] = min(route[u], route[v] + cost[v][u]);
					}
				}

				cout << route[goal - 1] << endl;
			}
			else//min time
			{
				exp[start -1] = 0;
				while(1)
				{
					int v = -1;
					for(int u = 0; u < stat; u++)
					{
						if(!jud[u] && (v == -1 || exp[u] < exp[v]) ) v = u;
					}

					if(v == -1) break;

					jud[v] = true;

					for(int u = 0; u < stat; u++)
					{
						exp[u] = min(exp[u], exp[v] + time[v][u]);
					}
				}

				cout << exp[goal - 1] << endl;
			}
		}
	}
}

C++: #include<iostream>
#include<algorithm>
const int INF = 1<<20;
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)return 0;
		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 = 1; k < m+1; ++k)
			for (int i = 1; i < m+1; ++i)
				for (int j = 1; j < m+1; ++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<cstdio>

int costs[100][100][2];

int main(){
 int n,m,a,b;
 scanf("%d%d",&n,&m);
 while(n--){
  scanf("%d%d",&a,&b);
  scanf("%d%d",&costs[a][b][0],&costs[a][b][1]);
  costs[b][a][0]=costs[a][b][0];
  costs[b][a][1]=costs[a][b][1];
 }
 for(int i=0;i<m;i++){
  for(int j=0;j<m;j++){
   for(int k=0;k<m;k++){
    costs[i][j][0]=costs[i][k][0]+costs[k][j][0];
    costs[j][i][0]=costs[i][j][0];
    costs[i][j][1]=costs[i][k][1]+costs[k][j][1];
    costs[j][i][1]=costs[i][j][1];
   }
  }
 }
 scanf("%d",&n);
 while(n--){
 scanf("%d&d&d",&a,&b,&k);
 printf("%d\n",costs[a][b][k]);
}
 return 0;
}

C++: #include<bits/stdc++.h>
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int a0[110][110], a1[110][110], n, m;
signed main() {
	while (cin >> n >> m, n) {
		for1(i, m)for1(j, m) { a0[i][j] = n * 1001; a1[i][j] = n * 1001; }
		for1(l0y3r, n) {
			int a, b, c, d; cin >> a >> b >> c >> d;
			a0[a][b] = c; a0[b][a] = c;
			a1[a][b] = d; a1[b][a] = d;
		}
		for1(k, m)for1(i, m)for1(j, m) {
			a0[i][j] = min(a0[i][j], a0[i][k] + a0[k][j]);
			a1[i][j] = min(a1[i][j], a1[i][k] + a1[k][j]);
		}
		cin >> n;
		for1(l0y3r, n) {
			int p, q, r; cin >> p >> q >> r;
			if (r)cout << a1[p][q] << endl;
			else cout << a0[p][q] << endl;
		}
	}
}