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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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<cstring>
#include<algorithm>
#include<functional>
#include<string>
#include<iostream>
#define M 100000000
#define rep(i,a) for(i=0;i<a;i++)
using namespace std;
int main(void)
{
	int n,m,i,j,k;
	int cost[101][101],time[101][101];
	int a,b,c,t;
	while(1) {
		cin>>n>>m;
		if(n==0&&m==0) break;
		rep(i,m)rep(j,m){cost[i][j]=M;time[i][j]=M;}
		rep(i,m){cost[i][i]=0;time[i][i]=0;}
		rep(i,n) {
			cin>>a>>b>>c>>t;
			a--;b--;
			cost[a][b]=c;cost[b][a]=c;
			time[a][b]=t;time[b][a]=t;
		}
		rep(k,m)rep(i,m)rep(j,m) {
			cost[i][j]=min(cost[i][k]+cost[k][j],cost[i][j]);
			time[i][j]=min(time[i][k]+time[k][j],time[i][j]);
		}
		int q,s,g,kk;
		cin>>kk;
		rep(i,kk) {
			cin>>s>>g>>q;
			s--;g--;
			if(q==0) cout<<cost[s][g]<<endl;
			else cout<<time[s][g]<<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <utility>
#include <stack>
#include <string.h>
using namespace std;
typedef pair<int,int> P;
const int INF = ~(1<<31) / 2;

int c[100][100];
int t[100][100];
int n,m,k;

int main(){
    while(1){
        scanf("%d %d",&n,&m);
        if(n==0) break;
        for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
                c[i][j] = 1000000;
                t[i][j] = 1000000;
            }
        }
        for(int i=0;i<n;i++){
            int a,b,co,ti;
            scanf("%d %d %d %d",&a,&b,&co,&ti);
            a--;b--;
            c[a][b] = co;
            c[b][a] = co;
            t[a][b] = ti;
            t[b][a] = ti;
        }

        for(int k=0;k<m;k++){
            for(int i=0;i<m;i++){
                for(int j=0;j<m;j++){
                    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]);
                }
            }
        }

        scanf("%d",&k);
        for(int i=0;i<k;i++){
            int p,q,r;
            scanf("%d %d %d",&p,&q,&r);
            p--;q--;
            if(r)printf("%d\n",t[p][q]);
            else printf("%d\n",c[p][q]);
        }
    }
    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 Cost[100][100];
		int Time[100][100];
		for(int i=0; i<m; i++){
			for(int j=0; j<m; j++){
				if(i==j){
					Cost[i][j] = Time[i][j] = 0;
				}
				else{
					Cost[i][j] = Time[i][j] = INF;
				}
			}
		}

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

			Cost[a-1][b-1] = Cost[b-1][a-1] = c;
			Time[a-1][b-1] = Time[b-1][a-1] = t;
		}
    for(int i =0;i<m;i++){
      for(int j =0;j<m;j++){
        for(int k = 0;k<m;k++){
          if(Cost[j][k]>Cost[j][i]+Cost[i][k]){
            Cost[j][k]>Cost[j][i]+Cost[i][k];
          }
          if(Time[j][k]>Time[j][i]+Time[i][k]){
            Time[j][k]>Time[j][i]+Time[i][k];
          }
        }
      }
    }
    int k;
    cin >>k;
    for(int i =0;i<k;i++){
      int p,q,r;
      cin >>p>>q>>r;
      if(r)cout << Time[p][q]<<endl;
      else cout << Cost[p][q]<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int dp[105][105][2], n, m;

void solve(int dp[][105][2], int st, int en, int d){
    rep(i, n){
        rep(j, n){
            rep(k, n){
                dp[i][j][d] = min(dp[i][k][d] + dp[k][j][d], dp[i][j][d]);
            }
        }
    }
}

int main(){
    int k;
    int a, b, c, d;
    int st, en, de;
    rep(i, 105)
        rep(j, 105)
            rep(k, 2)
                dp[i][j][k] = 2 << 24;

    while(cin >> n >> m, n||m){
        rep(i, n){
            cin >> a >> b >> c >> d;
            dp[a][b][0] = c; dp[b][a][0] = c;
            dp[a][b][1] = d; dp[b][a][1] = d;
        }
        cin >> k;
        rep(i, k){
            cin >> st >> en >> de;
            solve(dp, st, en, de);
            cout << dp[st][en][de] << endl;
        }
    }
}