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 <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<int,int> P;


int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int n,m;
  cin >> n >> m;

  vector< vector<int> > cost(m+1, vector<int>(m+1,INF)),
    time(m+1, vector<int>(m+1,INF));

  REP(i,cost.size()){
    cost[i][i] = 0;
    time[i][i] = 0;
  }

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

  int k;
  cin >> k;
  REP(_tmp,k){
    int p,q,r;
    cin >> p >> q >> r;

    vector< vector<int> > data(m+1, vector<int>(m+1,0));
    data = r ? time : cost;

    deque<bool> reached(m+1,0);
    vector<int> dis(m+1,INF);
    dis[p] = 0;

    while(true){
      int minn = -1;
      REP(i,reached.size()){
	if( ! reached[i] ){
	  if( minn==-1 || dis[minn] > dis[i] ){
	    minn = i;
	  }
	}
      }
      if( minn == q )
	break;

      reached[minn] = true;
      REP(i,data[minn].size()+1){
	dis[i] = min( dis[minn] + data[minn][i], dis[i] );
      }
    }

    cout << dis[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;

int main(){
    int k;
    int a, b, c, d;
    int st, en, de;

    while(cin >> n >> m, n||m){
        rep(i, 105){
            rep(j, 105){
                rep(k, 2){
                    dp[i][j][k] = 2 << 24;
                }
            }
        }

        rep(i, n){
            cin >> a >> b >> c >> d;
            dp[a][b][0] = dp[b][a][0] = c;
            dp[a][b][1] = dp[b][a][1] = d;
        }

        rep(i, m){
            rep(j, m){
                rep(k, m){
                    dp[i][j][0] = min(dp[i][k][0] + dp[k][j][0], dp[i][j][0]);
                    dp[i][j][1] = min(dp[i][k][1] + dp[k][j][1], dp[i][j][1]);
                }
            }
        }

        cin >> k;
        rep(i, k){
            cin >> st >> en >> de;
            cout << dp[st][en][de] << endl;
        }
    }
}

C++: #include <iostream>

#define N 101
#define INF 1e+9

using namespace std;

int cost[N][N],time[N][N];

int main(void){
	int n,m;
	while(cin>>n>>m,n||m){
		for(int i=0;i<n;++i)for(int j=0;j<n;++j){cost[i][j]=time[i][j]=INF;}
		for(int i=0,a,b,c,t;i<n;++i){
			cin>>a>>b>>c>>t;
			cost[a][b]=cost[b][a]=c;
			time[a][b]=time[b][a]=t;
		}
		int k;cin>>k;
		for(int i=0,p,q,r;i<k;++i){
			cin>>p>>q>>r;
			if(r){//time
				int d[N];
				for(int j=0;j<n;++j) d[j]=INF;
				d[p]=0;
				bool f=false;
				for(;;){
					for(int j=0;j<n;++j){//from
						for(int k=0;k<n;++k){//to
							if(d[j]!=INF && d[j] + time[j][k] < d[k]){
								f==true;
								d[k] = d[j] + time[j][k];
							}
						}
					}
					if(f==false) break;
					f=false;
				}
				cout<<d[q]<<endl;
			}else{//cost
				int d[N];
				for(int j=0;j<n;++j) d[j]=INF;
				d[p]=0;
				bool f=false;
				for(;;){
					for(int j=0;j<n;++j){//from
						for(int k=0;k<n;++k){//to
							if(d[j]!=INF && d[j] + cost[j][k] < d[k]){
								f==true;
								d[k] = d[j] + cost[j][k];
							}
						}
					}
					if(f==false) break;
					f=false;
				}
				cout<<d[q]<<endl;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <array>
#include <queue>

using namespace std;

int const Inf = 10000000;
int const N = 100;

typedef array<int, 2> line;

int main()
{
    int n, m;
    while( cin >> n >> m, n||m ) {
        line table[N][N] = {};

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

            int const from = a-1, to = b-1;
            line const in = {{cost, time}};
            table[from][to] = in;
            table[to][from] = in;
        }

        int k;
        cin >> k;
        for( int i=0; i<k; ++i ) {
            int p, q, r;
            cin >> p >> q >> r;
            int const from = p-1, to = q-1;
            int d[N];
            fill( d, d+N, Inf );

            priority_queue<int, vector<int>, greater<int>> w;
            w.push( from );
            d[from] = 0;

            while( !w.empty() ) {
                int const cur = w.top();
                w.pop();

                for( int ii=0; ii<m; ++ii ) {
                    if ( table[cur][ii][0] == 0 )
                        continue;

                    if ( d[cur] + table[cur][ii][r] < d[ii] ) {
                        d[ii] = d[cur] + table[cur][ii][r];
                        w.push( ii );
                    }
                }
            }

            cout << d[to] << endl;
        }
    }
}