Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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
					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>
#define INF 100000000
using namespace std;
int line[3000][3000],c[3000][3000],t[3000][3000];
int stop[3000]={0};
int ls[3000]={0}; 
int route_cost(int p,int q){
  int top = -1;
  for(int i=0;i<ls[p];i++){
    if(stop[line[p][i]]!=0) continue;
    //cout << line[p][i] << endl;
    stop[p]++;
    if(q==line[p][i]){
      stop[p] = 0;
      return c[p][q];
    }
    int h = route_cost(line[p][i],q)+c[p][line[p][i]];
    if(top<0||top>h) top = h;
  }
  stop[p] = 0;
  if(top<0) return INF;
  return top;
}
int route_time(int p,int q){
  int top = -1;
  for(int i=0;i<ls[p];i++){
    //cout << "P " << p << endl;
    if(stop[line[p][i]]!=0) continue;
    //cout << line[p][i] << endl;
    stop[p]++;
    if(q==line[p][i]){
      stop[p] = 0;
      return t[p][q];
    }
      int h = route_time(line[p][i],q)+t[p][line[p][i]];
      if(top<0||top>h){
	top = h;
      }
  }
  stop[p] = 0;
  if(top<0) return INF;
  return top;
}
int main(){
  int n,m;
  int a,b,cost,time;
  while(cin>>n>>m){
    if(n==0&&m==0) break;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      line[a][ls[a]] = b;
      ls[a]++;
      line[b][ls[b]] = a;
      ls[b]++;
      c[a][b] = cost;
      c[b][a] = cost;
      t[a][b] = time;
      t[b][a] = time;
      //cout << "line " << line[a][ls[a]-1] << ' ' << line[b][ls[b]-1] << endl; 
    }
    int k;
    cin >> k;
    int p,q,r;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      if(r==0) cout << route_cost(p,q) << endl;
      else if(r==1) cout << route_time(p,q) << endl;
      //for(int i=0;i<m;i++) cout << stop[i] << ' ' << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<28

using namespace std;

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

struct edge
{
	int to;
	int cost;

	edge(int to,int cost) : to(to),cost(cost) {}
};

vector<edge> G[3001];
vector<edge> G2[3001];
ll d[3001];

void dijkstra(int s,int n,int type)
{
	priority_queue<P,vector<P>,greater<P> > que;
	fill(d,d+n,INF);
	d[s] = 0;
	que.push(P(0,s));

	while(!que.empty())
	{
		P p = que.top();
		que.pop();

		int v = p.second;
		if(d[v] < p.first) continue;


		if(type == 0)
		{
			rep(i,G[v].size())
			{
				edge e = G[v][i];
				if(d[e.to] > d[v] + e.cost)
				{
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to],e.to));
				}
			}
		}
		else
		{
			rep(i,G2[v].size())
			{
				edge e = G2[v][i];
				if(d[e.to] > d[v] + e.cost)
				{
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to],e.to));
				}
			}
		}


	}
}

int main()
{
	int n,m;
	while(cin >> n >> m)
	{
		if(n == 0 && m == 0) break;
		
		rep(i,n)
		{
			int a,b,cost,time;
			cin >> a >> b >> cost >> time;
		
			a--;
			b--;

			G[a].push_back(edge(b,cost));
			G[b].push_back(edge(a,cost));

			G2[a].push_back(edge(b,time));
			G2[b].push_back(edge(a,time));
		}

		int k;
		cin >> k;

		rep(i,k)
		{
			int s,t,type;
			cin >> s >> t >> type;

			s--;
			t--;

			dijkstra(s,m,type);
			cout << d[t] << endl;
		}

	}
	return 0;
}

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

#define rep(i,n) for(int i = 0 ; i < n ; i++)

int main(){
	int mon[100][100];
	int tim[100][100];
	int n,m;
	while(cin >> m >> n && n ){
		rep(i,n)rep(j,n) mon[i][j] = tim[i][j] = 1e9;
		for(int i = 0 ; i < m ; i++){
			int a,b,c,d;
			cin >> a >> b >> c >> d;
			a--,b--;
			mon[a][b] = mon[b][a] = c;
			tim[a][b] = tim[b][a] = d;
		}
		rep(i,n)rep(j,n)rep(k,n){
			mon[j][k] = min(mon[j][k],mon[j][i]+mon[i][k]);
			tim[j][k] = min(tim[j][k],tim[j][i]+tim[i][k]);
		}
		int R;
		cin >> R;
		rep(i,R){
			int a,b,c;
			cin >> a >> b >> c;
			a--,b--;
			if(c) cout << tim[a][b];
			else cout << mon[a][b];
			cout << endl;
		}
	}
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef pair<int,int> P;
typedef struct{
	int next;
	int cost[2];
}edge;

int n,m;
vector<edge> mapp[111];
int bfs(int now,int goal,int k);

int main(){
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0) break;
		for(int i=0;i<m+1;i++){
			mapp[i].clear();
		}
		for(int i=0;i<n;i++){
			int a,b,cost,tim;
			cin >> a >> b >> cost >> tim;
			edge x;
			//x.next = a; x.cost[0] = cost; x.cost[1] = tim;
			//mapp[b].push_back(x);
			x.next = b; x.cost[0] = cost; x.cost[1] = tim;
			mapp[a].push_back(x);
		}
		int k;
		cin >> k;
		int ans;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin >> p >> q >> r;
			if(r == 0) ans = bfs(p,q,r);
			else if(r == 1) ans = bfs(p,q,r);
			cout << ans << endl;
		}
	}
}

int bfs(int now,int goal,int k){
	vector<int> min_cost;
	min_cost.resize(m+1,-1);
	priority_queue<P, vector<P>,greater<P> > que;
	que.push(P(0,now));
	min_cost[now] = 0;
	while(!que.empty()){
		P p = que.top(); que.pop();
		int no = p.second;
		int co = p.first;
		if(no == goal) return co;
		if(co > min_cost[no]) continue;
		int len=mapp[no].size();
		for(int i=0;i<len;i++){
			edge x = mapp[no][i];
			if(min_cost[x.next] == -1 || co+x.cost[k]<min_cost[x.next]){
				min_cost[x.next] = co + x.cost[k];
				que.push(P(co+x.cost[k],x.next));
			}
		}
	}
	return min_cost[goal];
}