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<cmath>
#include<cctype>
#include<cstdlib>
#include<cstdio>
#include<climits>
#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<utility>
#include<queue>

using namespace std;
int n,m;
const int MAXN = 1000;
int t[MAXN][MAXN];
int c[MAXN][MAXN];

void solve(){
  for(int k = 0;k < n; ++k){
    for(int i = 0;i < n; ++i){
      for(int j = 0;j < n; ++j){
	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]);
      }
    }
  }
}
const int INF = INT_MAX/3;
void init(){
  for(int i = 0;i < MAXN; ++i){
    for(int j = 0;j < MAXN;j++){
      c[i][j] = t[i][j] = INF;
    }
  }
  for(int i = 0;i < MAXN;i++){
    c[i][i] = t[i][i] = 0;
  }
}


int main(){
  ios::sync_with_stdio(false);

  while(cin >> n >> m && n+m > 0){
    init();
    int a,b,icost,itime;
    for(int i = 0;i < n;i++){
      cin >> a >> b >> icost >> itime;
      t[a][b] = t[b][a] = itime;
      c[a][b] = c[b][a] = icost;
    }
    solve();
    int k;
    cin >> k;
    for(int i = 0;i < k; ++i){
      int p,q,r;
      cin >> p >> q >> r;
      if(r == 1){
	cout << t[p][q] << endl;
      }else{
	cout << c[p][q] << endl;
      }
    }
  }
  return 0;
}

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

#define rep(i, n) for(int i = 0;i < (int)n;i++)
#define rep2(i, n) for(int i = 1;i <= (int)n;i++)

int c[101][101];
int t[101][101];

int main() {
    while(true) {
        int n, m;
        cin >> n >> m;
        if(!n && !m) break;

        rep(i, 101) {
            rep(j, 101) {
                c[i][j] = t[i][j] = (i != j ? 1001 : 0);
            }
        }

        rep(i, n) {
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;
            c[a][b] = c[b][a] = cost;
            t[a][b] = t[b][a] = time;
        }

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

        int k;
        cin >> k;
        rep(i, k) {
            int p, q, r;
            cin >> p >> q >> r;
            cout << (r == 0 ? c[p][q] : t[p][q]) << endl;
        }
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

const int INF = 1<<25;

using namespace std;

int main()
{
	int n,m,i,j;
	int cost[101][101],time[101][101];
	
	for(;;)
	{
		cin >> n >> m ;
		if(n==0 && m==0)
			break;
		
		for(i=0;i<m+1;i++)
		{
			for(j=0;j<m+1;j++)
			{
				if(i!=j)
					cost[i][j]=time[i][j]=INF;
				else
					cost[i][j]=time[i][j]=0;
			}
		}
		
		for(i=0;i<n;i++)
		{
			int a,b,co,ti;
			cin >> a >> b >> co >> ti ;
			cost[a][b]=cost[b][a]=co;
			time[a][b]=time[b][a]=ti;
		}
		
		int inp;
		cin >> inp ;
		
		for(int k=0;k<m+1;k++)
		{
			for(i=0;i<m+1;i++)
			{
				for(j=0;j<m+1;j++)
				{
					cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
					time[i][j]=min(time[i][j],time[i][k]+time[k][j]);
				}
			}
		}
		
		for(i=0;i<inp;i++)
		{
			int st,en,se;
			cin >> st >> en >> se ;
			
			if(se==0)
				cout << cost[st][en] << endl;
			else
				cout << time[st][en] << endl;
		}
	}
}

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

struct edge {
    int from, to, cost, time;
};

int main() {
    int n, m, a, b, cost, time, k, p, q, r;
    while (cin >> n >> m, n || m) {
        vector<edge> edges;
        for (int i=0; i<n; ++i) {
            cin >> a >> b >> cost >> time;
            edges.push_back((edge){ a, b, cost, time });
        }
        cin >> k;
        for (int i=0; i<k; ++i) {
            cin >> p >> q >> r;
            int d[100];
            for (int i=0; i<100; ++i) d[i] = 1e8;
            d[p] = 0;
            for (int i=0; i<100; ++i) {
                for (int j=0; j<(int)edges.size(); ++j) {
                    edge &e = edges[j];
                    if (r == 0) {
                        d[e.to] = min(d[e.to], d[e.from] + e.cost);
                    } else {
                        d[e.to] = min(d[e.to], d[e.from] + e.time);
                    }
                }
            }
            cout << d[q] << endl;
        }
    }
    return 0;
}

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

const int INF = 1000000000;

int main()
{
	int C[100][100];
	int T[100][100];
	int z=0;
	int p[200];
	int q[200];
	int r[200];
	int A[200];
	while(true){
		int n, m;
		cin >> n >> m;

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

		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];
						
					}
				}
			}
		}
		cin>>z;
		for(int i=0;i<z;i++){
				cin>>p[i]>>q[i]>>r[i];
			if(r[i]==0){
				cout<<C[p[i]-1][q[i]-1]<<endl;
			}
			if(r[i]==1){
				cout<<T[p[i]-1][q[i]-1]<<endl;
			}
		}
		
	}
	

	return 0;
}