Go: // Warshall–Floyd Algorithm

package main

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

var scanner = bufio.NewScanner(os.Stdin)

const INF = 2000000000

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 <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M, Q;
vector<P> G[100][2];
int dist[100][100][2];

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (cin >> M >> N) {
    if (M == 0 && N == 0) break;
    rep(i, N) rep(k, 2) G[i][k].clear();
    rep(i, N) rep(j, N) rep(k, 2) dist[i][j][k] = INF;
    rep(i, M) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      a--, b--;
      G[a][0].pb(P(b, c));
      G[b][0].pb(P(a, c));
      G[a][1].pb(P(b, t));
      G[b][1].pb(P(a, t));
    }
    rep(k, 2) {
      rep(s, N) {
        priority_queue<P, vector<P>, greater<P> > q;
        dist[s][s][k] = 0;
        q.push(P(0, s));

        while (!q.empty()) {
          int x = q.top()._2, r = q.top()._1;
          q.pop();
          if (dist[s][x][k] < r) continue;
          for (P p : G[x][k]) {
            int t = p._1, l = p._2;
            if (dist[s][t][k] > r+l) {
              dist[s][t][k] = r+l;
              q.push(P(r+l, t));
            }
          }
        }
      }
    }
    cin >> Q;
    rep(i, Q) {
      int p, q, r;
      cin >> p >> q >> r;
      p--, q--;
      cout << dist[p][q][r] << "\n";
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#define INF 1001
using namespace std;

int main(){
  int i,j,k,kk,aa,bb,n,m,a,b,d,dd,c[101][101],t[101][101],f;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    for(i=0;i<=m;i++){
      for(j=0;j<=m;j++){
	if(i == j){
	  c[i][j] = 0;
	  t[i][j] = 0;
	}
	else{
	  c[i][j] = INF;
	  t[i][j] = INF;
	}
      }
    }

    for(i=0;i<n;i++){
      cin >> a >> b >> d >> dd;
      c[a][b] = c[b][a] = d;
      t[a][b] = t[b][a] = dd;
    }

    for(i=0;i<=m;i++){
      for(j=0;j<=m;j++){
	for(k=0;k<=m;k++){
	  t[i][j] = min(t[i][j],t[i][k] + t[k][j]);
	  c[i][j] = min(c[i][j],c[i][k] + c[k][j]);
	}
      }
    }

    cin >> kk;
    for(i=0;i<kk;i++){
      cin >> aa >> bb >> f;
      if(f) cout << t[aa][bb] << endl;
      else cout << c[aa][bb] << endl;
    }

  }
  return 0;
}

C++: #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;
		}
              int k;
              int p,q,r;
              cin >> k;
              for(int i=0;i<k;i++){
                 cin >> p >> q >> r;
                 int A[m][m];
                 if(r==0){
                    for(int j=0;j<m;j++)
                       for(int k=0;k<m;k++)
                          A[j][k]=C[j][k];
                    for(int j=0;j<m;j++)
                       for(int k=0;k<m;k++)
                          for(int l=0;l<m;l++)
                             if(A[j][k]>A[j][l]+A[l][k])
                                A[j][k]=A[j][l]+A[l][k];
                    cout << A[p-1][q-1] << endl;
                 }
                 else{
                    for(int j=0;j<m;j++)
                       for(int k=0;k<m;k++)
                          A[j][k]=T[j][k];
                    for(int j=0;j<m;j++)
                       for(int k=0;k<m;k++)
                          for(int l=0;l<m;l++)
                             if(A[j][k]>A[j][l]+A[l][k])
                                A[j][k]=A[j][l]+A[l][k];
                    cout << A[p-1][q-1] << endl;
                 }
              }
	}

	return 0;
}

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

int time[100][100];
int cost[100][100];
const int INF = 10000;


int main(void){
	while(1){
		//init
		int rosen,eki,a,b,incost,intime;
		for(int i=0; i<100; i++){ 
			for(int j=0; j<100; j++)
				time[i][j] = cost[i][j] = INF;
		}
		for(int i=0; i<100; i++)
			time[i][i] = cost[i][i] = 0;
	
		//in
		cin >> rosen >> eki; 
		if(rosen==0&&eki==0)break;
		for(int i=0; i<rosen; i++){
			cin >> a >> b >> incost >> intime;
			a--; b--;
			cost[a][b] = cost[b][a] = incost;
			time[a][b] = time[b][a] = intime;
		}

		//calc
		for(int k=0; k<eki; k++){
			for(int i=0; i<eki; i++){
				for(int j=0; j<eki; j++){
					time[i][j] = min(time[i][j],time[i][k]+time[k][j]);
					cost[i][j] = min(cost[i][j],cost[i][k]+cost[k][j]);
				}
			}
		}

		//out
		int toi;
		cin >> toi;
		for(int i=0; i<toi; i++){
			int a,b,r;
			cin >> a >> b >> r;
			a--; b--;
			if(r==0)
				cout << cost[a][b] << endl;
			else
				cout << time[a][b] << endl;
		}
	}

	return 0;
}