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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)
#define MIN(a, b) ((a)<(b)?(a):(b))
#define INF 10e6
using namespace std;
 /*temp*/

int main(){
    int n, m, l;
    while(cin>>m>>n,n){
        int cost[n][n],tim[n][n];
        // set table
        REP(i,n){
            REP(j,n){
                cost[i][j]=tim[i][j]=INF;
            }
            cost[i][i]=tim[i][i]=0;
        }
        // read
        REP(i,n){
            int a,b,c,t;
            a--;
            b--;
            cost[a][b]=cost[b][a]=MIN(c,cost[a][b]);
            tim[a][b]=tim[b][a]=MIN(t,tim[a][b]);
        }
        // Warshall???Floyd Algorithm
        REP(a,n){
            REP(b,a+1){
                REP(k,n){
                    cost[a][b]=cost[b][a]=MIN(cost[a][b],cost[a][k]+cost[k][b]);
                    tim[a][b]=tim[b][a]=MIN(tim[a][b],tim[a][k]+tim[k][b]);
                }
            }
        }
        // 
        int q;
        cin>>q;
        REP(i,q){
            int s,d,type;
            cin>>s>>d>>type;
            s--;
            d--;
            cout<<type==0?cost[s][d]:tim[s][d]<<'\n';
        }
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int road1[111][111];
  int road2[111][111];
  int n,m;
  int a,b,cost,time;
  int p,q,r;
  int x;
  while(1){
    for(int i=0;i<101;i++){
      for(int j=0;j<101;j++){
	road1[i][j]=road2[i][j]=1111111;
      }
    }
    cin >> n >> m;
    if(n==0 && m==0)break;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      road1[a][b]=road1[b][a]=cost;
      road2[a][b]=road2[b][a]=time;
    }

    for(int k=1;k<=n;k++){
      for(int i=1;i<=n;i++){
	for(int j=1;j<=n;j++){
	  if(road1[i][j]>road1[i][k]+road1[k][j]){
	    road1[i][j]=road1[i][k]+road1[k][j];
	  }
	  if(road2[i][j]>road2[i][k]+road2[k][j]){
	    road2[i][j]=road2[i][k]+road2[k][j];
	  }
	}
      }
    }
    
    cin >> x;
    for(int i=1;i<=x;i++){
      cin >> p >> q >> r;
      cout << (r==0 ? road1[p][q] : road2[p][q]) << endl;
    }
  }
}

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

typedef long long int ll;
#define REP(i,p,n) for(int i=p;i<n;++i)
#define rep(i,n) REP(i,0,n)

typedef pair<ll,ll> pii;
const ll INF=(ll)1e12, MAXN=128;
ll C[2][MAXN][MAXN], visited[MAXN], N, M;

int dk(int p,int q,int r)
{
        priority_queue<pii,vector<pii>,greater<pii> > Q;
        Q.push(pii(0,p));

        while(!Q.empty())
        {
                const ll cc=Q.top().first, pp=Q.top().second; Q.pop();
                if(pp==q){ return cc; } else { visited[pp]=1; }

                rep(i,M) if(!visited[i]){ Q.push(pii(cc+C[r][pp][i],i)); }
        }
        return 0;
}

int main()
{
        ll a,b,c,d;
        cin>>N>>M;
        rep(i,M) rep(j,N) C[0][i][j]=C[1][i][j]=(i==j?0:INF);
        rep(i,N){ cin>>a>>b>>c>>d; --a; --b; C[0][a][b]=C[0][b][a]=c; C[1][a][b]=C[1][b][a]=d; }

        cin>>d;
        rep(i,d){ fill(visited,visited+N,0); cin>>a>>b>>c; cout<<dk(a-1,b-1,c)<<endl; }
        return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <functional>

using namespace std;

//FILE* in = freopen("./B","r",stdin);

typedef long long LL;
#define REP(i,a,b)for(LL i=(LL)a;i<(LL)b;i++)


#define MAX_V 3100
#define INF 1000000

struct edge{int to,cost,time;edge(int t,int c,int h):to(t),cost(c),time(h){}};
typedef pair<int, int> P;

int V=MAX_V;
vector<edge> G[MAX_V];
int d[MAX_V];

int dijkstra(int s,int g,bool flag){
	priority_queue<P, vector<P>, greater<P> > que;
	fill(d, d+V, INF);
	d[s] = 0;
	que.push(P(0,s));

	while(!que.empty()){
		P p=que.top(); que.pop();
		int v = p.second;
		if(v==g)return d[v];
		if( d[v] < p.first ) continue;
		for(int i = 0;i<G[v].size();i++){
			edge e = G[v][i];
			if(flag==0){
			if(d[e.to] > d[v] + e.cost ){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to],e.to));
			}
			}else{
				if(d[e.to] > d[v] + e.time ){
				d[e.to] = d[v] + e.time;
				que.push(P(d[e.to],e.to));
			}

			}
		}
	}
	return -1;
}

int main(void){

	while(1){


		G->clear();

		int N,M;
		int cost,time,a,b;
		int K,start,goal,R;

		cin >> N>>M;

		if(!N&&!M)break;

		REP(i,0,N){
			cin >> a>>b>>cost>>time;
			G[a].push_back(edge(b,cost,time));
			G[b].push_back(edge(a,cost,time));
		}

		cin >> K;

		REP(i,0,K){
			cin >> start>>goal>>R;
			cout << dijkstra(start,goal,R)<<endl;
		}

	}

	return 0;
}

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

using namespace std;

int main() {
	int n, m, k;

	int costs[100][100];
	int times[100][100];

	while(cin >> n >> m ) {

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

		for(int i = 0; i < 100; i++) {
			for(int j = 0; j < 100; j++) {
				costs[i][j] = 1e5;
				times[i][j] = 1e5;
			}
		}

		for(int i = 0; i < n; i++) {
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			a -= 1;
			b -= 1;
			costs[a][b] = costs[b][a] = cost;
			times[a][b] = times[b][a] = time;
		}

		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				for(int k = 0; k < n; k++) {
					costs[j][k] = min(costs[j][k], costs[j][i] + costs[i][k]);
					times[j][k] = min(times[j][k], times[j][i] + times[i][k]);
				}
			}
		}

		cin >> k;
		for(int i = 0; i < k; i++) {
			int p, q, r;
			cin >> p >> q >> r;
			p -= 1;
			q -= 1;
			if(r) cout << times[p][q];
			else cout << costs[p][q];
			cout << endl;
		}
	}
	return 0;
}