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 <iostream>
using namespace std;

signed main(){
    int m,n;
    static int fare[111][111], time[111][111];
    while(cin >> m >> n && m){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                fare[i][j] = time[i][j] = (i == j) ? 0 : 1e8;
            }
        }
        for(int i = 0; i < m; i++){
            int u, v, a, b;
            cin >> u >> v >> a >> b;
            fare[u][v] = fare[v][u] = a;
            time[u][v] = time[v][u] = b;
        }
        for(int k = 1; k <= n; k++){
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= n; j++){
                    fare[i][j] = min(fare[i][j], fare[i][k] + fare[k][j]);
                    time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
                }
            }
        }
        int t;
        cin >> t;
        for(int i = 0; i < t; i++){
            int u, v, q;
            cin >> u >> v >> q;
            if(q == 0) cout << fare[u][v];
            else cout << time[u][v];
            cout << endl;
        }
    }
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<utility>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pi acos(-1)
#define all(v) v.begin(),v.end()
using namespace std;

const double eps = 1e-8;
const double INF = 1e5;

typedef pair<int,int> P;
typedef double Real;
typedef complex<Real> Point;
typedef vector<Point> G;

double cross(const Point& a,const Point& b){//calculate ad-bc(vector product)
	return imag(conj(a)*b);
}

struct L:public vector<Point>{//line data
	L(const Point &a,const Point &b){
		push_back(a);
		push_back(b);
	}
};

int main(){
	int n,m;
	while(1){
		cin>>n>>m;
		if(!n && !m)break;
		int cost[m+1][m+1],ti[m+1][m+1];
		rep(i,m)rep(j,n)cost[i][j]=ti[i][j]=INF;
		int a,b,c,t;
		rep(i,n){
			cin>>a>>b;
			cin>>cost[a][b]>>ti[a][b];
			cost[b][a]=cost[a][b];
			ti[b][a]=ti[a][b];
		}
		loop(k,1,m+1){
			loop(i,1,m+1){
				loop(j,1,m+1){
					cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
					ti[i][j]=min(ti[i][j],ti[i][k]+ti[k][j]);
				}
			}
		}
		int k,p,q,r;
		cin>>k;
		rep(i,k){
			cin>>p>>q>>r;
			if(r==0){
				cout<<cost[p][q]<<endl;
			}else{
				cout<<ti[p][q]<<endl;
			}
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int main(){
  int n,m;
  while(scanf("%d%d",&n,&m), n+m){
    int time[100][100] = {{0}};
    int cost[100][100] = {{0}};
    REP(i,n){
      int a,b,c,d;
      scanf("%d%d%d%d",&a,&b,&c,&d);
      a--; b--;
      cost[a][b] = cost[b][a] = c;
      time[a][b] = time[b][a] = d;
    }
    int k;
    scanf("%d",&k);
    while(k --> 0){
      int p,q,type;
      int (*load)[100];
      scanf("%d%d%d",&p,&q,&type);
      if(type == 0) load = cost;
      else load = time;

      p--; q--;
      int memo[100];
      memset(memo, -1, sizeof(memo));
      priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > qq;
      qq.push(mp(0, p));
      while(qq.size()){
	int c = qq.top().f;
	int pos = qq.top().s;
	qq.pop();
	if(pos == q){
	  printf("%d\n",c);
	  break;
	}
	if(memo[pos] != -1)
	  continue;
	memo[pos] = c;
	REP(i,m){
	  if(memo[i] == -1 && load[pos][i] != 0)
	    qq.push(mp(c+load[pos][i], i));
	}
      }
    }
  }
  return 0;
}

C++: // AOJ 0200
#include<algorithm>
#include<iostream>

const int INF = 1 << 29;

int n, m;
int d[2][3000][3000];

int main()
{
	while( std::cin >> n >> m, n | m )
	{
		for( int i = 0; i != n; ++i )
			for( int j = 0; j != n; ++j )
				for( int k = 0; k != 2; ++k )
					d[k][i][j] = i == j ? 0 : INF;

		for( int i = 0; i != n; ++i )
		{
			int a, b, c, t;
			std::cin >> a >> b >> c >> t;
			d[0][a-1][b-1] = d[0][b-1][a-1] = c;
			d[1][a-1][b-1] = d[1][b-1][a-1] = t;
		}

		for( int k = 0; k != n; ++k )
			for( int i = 0; i != n; ++i )
				for( int j = 0; j != n; ++j )
					for( int l = 0; l != 2; ++l )
						d[l][i][j] = std::min( d[l][i][j], d[l][i][k] + d[l][k][j] );

		int k;
		std::cin >> k;
		while( k-- )
		{
			int p, q, r;
			std::cin >> p >> q >> r;

			std::cout << d[r][p-1][q-1] << std::endl;
		}
	}

	return 0;	
}