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 <stdio.h>
#include <algorithm>

using namespace std;
int cost[101][101][2];
int d[101];   //始点固定は最初
bool used[101];
int n,m;    //mは頂点数


void dijkstra(int s,int r){
	for(int j=0;j<m;j++){
	d[j]=1000000;
	used[j]=false;
	}


    d[s]=0;
	while(true){
		int v=-1;
		for(int u=0; u<m;u++){
			if(!used[u] && (v == -1 || d[u] < d[v]))
				 v=u;
		}
		
		if(v == -1)
			break;
		used[v] = true;
		for ( int u =0; u < m; u++){
			d[u] = min(d[u], d[v] + cost[v][u][r]);
		}
	}
}

int main(void){
	for(;;){
		int a,b,time,cos;
    	scanf("%d %d",&n,&m);
	if(!(n|m))
		break;
		for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
    						cost[i][j][0]=1000000;
							cost[i][j][1]=1000000;
				}
		}
    	for(int i=0;i<n;i++){
    		scanf("%d %d %d %d",&a,&b,&cos,&time);
			a--; b--;
			cost[a][b][0]=cos;
			cost[b][a][0]=cos;
			cost[a][b][1]=time;
			cost[b][a][1]=time;
		}
		
		int p,q,r;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
		scanf("%d %d %d",&p,&q,&r);
		p--;q--;
		dijkstra(p,r);
		printf("%d\n",d[q]);
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <queue>
#define INF 200000000
using namespace std;
typedef pair<int, int> P;
struct line {
  int to,time,cost;
};

vector<line> L[1000];
int costv[1000];
int timev[1000];

void mincost(int from,int to) {
  priority_queue<P, vector<P>, greater<P> > que;
  for(int i=0;i<1000;i++) {
    costv[i] = INF;
  }
  costv[from] = 0;
  que.push(P(0,from));
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int v = p.second;
    if(costv[v] < p.first) continue;
    for(unsigned int i=0;i < L[v].size();i++) {
      line l = L[v][i];
      if(costv[l.to] > costv[v] + l.cost) {
	costv[l.to] = costv[v] + l.cost;
	que.push(P(costv[l.to], l.to));
      }
    }
  }
  cout << costv[to] << endl;
}

void mintime(int from,int to) {
  priority_queue<P, vector<P>, greater<P> > que;
  for(int i=0;i<1000;i++) {
    timev[i] = INF;
  }
  timev[from] = 0;
  que.push(P(0,from));
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int v = p.second;
    if(timev[v] < p.first) continue;
    for(unsigned int i=0;i < L[v].size();i++) {
      line l = L[v][i];
      if(timev[l.to] > timev[v] + l.time) {
	timev[l.to] = timev[v] + l.time;
	que.push(P(timev[l.to], l.to));
      }
    }
  }
  cout << timev[to] << endl;
}

int main() {
  int n,m,k;
  while(cin >> n >> m) {
    if(n==0&&m==0)break;
    for(int i=0;i<1000;i++) {
      L[i].clear();
    }
    for(int i=0;i<n;i++) { //input line
      line temp;
      int f,t,ti,co;
      cin >> f >> t >> co >> ti;
      temp.to = t;
      temp.time = ti;
      temp.cost = co;
      L[f].push_back(temp);
      temp.to = f;
      L[t].push_back(temp);
    }

    cin >> k;
    for(int i=0;i<k;i++) {
      int p,q,r;
      cin >> p >> q >> r;
      if(r == 0)
	mincost(p,q);
      else
	mintime(p,q);
    }
  }
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<deque>
#include<queue>

#define INF 1<<30

using namespace std;

class state{
	public:
	int p,e;
	state(){};
	state(int pp,int aa){p=pp;e=aa;}
	bool operator < (const state& b) const{
		return e > b.e;
	}
};

int n;
int C[101][101];
int T[101][101];

int dike(int st,int en,int bl){
	priority_queue<state> aa;
	int visit[101];
	int now[101];
	
	for(int i=0;i<101;i++){
		visit[i]=0;
		now[i]=INF;
	}
	
	aa.push(state(st,0));
	now[st]=0;
	while(1){
		if(aa.empty()==1){
			break;
		}
		state u=aa.top();
		aa.pop();
		
		visit[u.p]=1;
		
		if(u.p==en){
			return u.e;
		}
		
		for(int i=1;i<=n;i++){
			int cost=C[i][u.p];
			if(bl==1){
				cost=T[i][u.p];
			}
			if(cost==INF||visit[i]==1){
				continue;
			}
			if(cost+u.e<now[i]){
				now[i]=u.e+cost;
				aa.push(state(i,now[i]));
			}
		}
	}
	return 10;
}

int main(){
	int m;
	scanf("%d%d",&m,&n);
	
	for(int i=0;i<=100;i++){
		for(int j=0;j<=100;j++){
			C[i][j]=T[i][j]=INF;
		}
	}
	for(int i=0;i<m;i++){
		int a,b,c,d;
		scanf("%d%d%d%d",&a,&b,&c,&d);
		C[a][b]=C[b][a]=c;
		T[a][b]=T[b][a]=d;
	}
	scanf("%d",&m);
	for(int i=0;i<m;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		printf("%d\n",dike(a,b,c));
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define REP(i,n) for (int i=0; i<(n); ++i)

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

int main() {
    int n, m;
    while (cin >> n >> m, n || m) {
        vector<edge> edges;
        int a, b, v, u;
        REP(i,n) {
            cin >> a >> b >> v >> u;
            edges.push_back((edge){ a, b, v, u });
            edges.push_back((edge){ b, a, v, u });
        }
        int d[2][101][101];
        REP(i,2) REP(j,101) REP(k,101) {
            d[i][j][k] = (j == k ? 0 : 1e8);
        }
        REP(i,101) REP(j,101) for (auto &e : edges) {
            d[0][j][e.to] = min(d[0][j][e.to], d[0][j][e.from] + e.cost);
            d[1][j][e.to] = min(d[1][j][e.to], d[1][j][e.from] + e.time);
        }
        int k, p, q, r;
        cin >> k;
        REP(i,k) {
            cin >> p >> q >> r;
            cout << d[r][p][q] << endl;
        }
    }
    return 0;
}