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<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<s;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<long long,long long> pii;
#define MAX 99999999
signed main(){
while(1){
	int n,m;
	cin>>n>>m;
    if(!(n+m))break;
	vector<vector<pii>>v(n+1,vector<pii>(n+1,pii(INF,INF)));
	rep(i,n+1)v[i][i]=pii(0,0);
	rep(i,n){
		pii temp;
		int s,g;
		cin>>s>>g>>temp.first>>temp.second;
        v[--s][--g]=temp;
	}
	int k; cin>>k;
    rep(u,k){
    int s,g; int r;
    cin>>s>>g>>r;
    if(!r){
        rep(k,m){
            rep(j,m){
                rep(i,m){
                    v[i][j].first=min(v[i][k].first+v[k][j].first,v[i][j].first);
                }
            }
        }
        cout<<v[--s][--g].first<<endl;
    }else{
        rep(k,m){
            rep(j,m){
                rep(i,m){
                    v[i][j].second=min(v[i][k].second+v[k][j].second,v[i][j].second);
                }
            }
        }
        cout<<v[--s][--g].second<<endl;
    }
    }
    }
}

C++: #include <bits/stdc++.h>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
const double PI = acos(-1.0);
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using edge = struct
{
    int to;
    int cost;
};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll costs[100][100];
ll times[100][100];

void Warshall_Floyd(int N, ll (&cost)[100][100])
{
    rep(k, N) rep(i, N) rep(j, N)
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
}

int main()
{
    int N, M;
    while (cin >> N >> M && N)
    {
        rep(i, 100) rep(j, 100) costs[i][j] = llINF;
        rep(i, 100) rep(j, 100) times[i][j] = llINF;

        rep(i, N)
        {
            ll a, b, c, t;
            cin >> a >> b >> c >> t;
            a--;
            b--;

            costs[a][b] = min(costs[a][b], c);
            costs[b][a] = min(costs[b][a], c);

            times[a][b] = min(times[a][b], t);
            times[b][a] = min(times[b][a], t);
        }

        Warshall_Floyd(N, costs);
        Warshall_Floyd(N, times);

        int K;
        cin >> K;

        int p, q, r;

        rep(i, K)
        {
            cin >> p >> q >> r;
            p--;
            q--;

            cout << (r == 1 ? times[p][q] : costs[p][q]) << endl;
        }
    }
    return 0;
}

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

typedef vector<vector<int> > Matrix;

const int INF = 100000000;
Matrix d; // ??°??????????????¢???????´???????2?¬???????????????£??\?????????
Matrix e; // ??°??????????????¢???????´???????2?¬???????????????£??\?????????
          // d[u][v]??????e=(u,v)??????????????????????????¨???????????´??????INF????????????d[i][i]=0???

void warshall_floyd1(int n) { // n???????????°
  for (int i = 0; i < n; i++)      // ?????±????????????
    for (int j = 0; j < n; j++)    // ????§???????
      for (int k = 0; k < n; k++)  // ??????
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
}

void warshall_floyd2(int n) { // n???????????°
  for (int i = 0; i < n; i++)      // ?????±????????????
    for (int j = 0; j < n; j++)    // ????§???????
      for (int k = 0; k < n; k++)  // ??????
        e[j][k] = min(e[j][k], e[j][i] + e[i][k]);
}

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

  d = Matrix(m, vector<int>(m, INF));
  e = Matrix(m, vector<int>(m, INF));
  for (int i = 0; i < m; i++) d[i][i] = 0;
  for (int i = 0; i < m; i++) e[i][i] = 0;
  
  for (int i = 0; i < n; i++) {
    int from, to, cost,time;
    cin >> from >> to >> cost >> time;
    d[from-1][to-1] = cost;
    e[from-1][to-1] = time;
  }
  
  warshall_floyd1(m);
  warshall_floyd2(m);
  
  int quiry;
  cin >> quiry;
  
  for(int i= 0; i < quiry; i++){
  	int a,b,c;
  	cin >> a >> b >> c;
  	if(c == 0) cout << d[a-1][b-1] << endl;
  	else cout << e[a-1][b-1] << endl;
  }
  
  
  return 0;
}