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<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){
				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<bits/stdc++.h>
using namespace std;
typedef struct E{
  int to,cost,tim;
}edge;
vector<vector<edge> >root(310);
int d[2][101];
void dijkstraco(int s){
  priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >que;
  d[0][s] = 0;
  que.push(pair<int,int>(0,s));
  while(!que.empty()){
    pair<int,int> p=que.top();que.pop();
    int v = p.second;
    if(d[0][v]<p.first)continue;
    for(int j=0;j<root[v].size();j++){
      edge t = root[v][j];
      if(d[0][t.to]>d[0][v]+t.cost){
	d[0][t.to]=d[0][v]+t.cost;
	que.push(pair<int,int>(d[0][t.to],t.to));
      }
    }
  }
}
void dijkstrati(int s){
  priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >que;
  d[1][s] = 0;
  que.push(pair<int,int>(0,s));
  while(!que.empty()){
    pair<int,int> p=que.top();que.pop();
    int v = p.second;
    if(d[1][v]<p.first)continue;
    for(int j=0;j<root[v].size();j++){
      edge t = root[v][j];
      if(d[1][t.to]>d[1][v]+t.tim){
	d[1][t.to]=d[1][v]+t.tim;
	que.push(pair<int,int>(d[1][t.to],t.to));
      }
    }
  }
}
int main(){
  int n,m;
  while(cin>>n>>m,n+m!=0){
    for(int i=0;i<n;i++){
      int f;cin>>f;
      edge e;cin>>e.to>>e.cost>>e.tim;
      root[f].push_back(e);
      int tmp=f;
      f=e.to;
      e.to=tmp;
      root[f].push_back(e);
    }
    int k;cin>>k;
    for(int i=0;i<k;i++){
      for(int j=0;j<2;j++){
	for(int a=0;a<101;a++)
	  d[j][a]=1000000000;
      }
      int p,q,r;cin>>p>>q>>r;
      if(r==0)
	dijkstraco(p);
      else
	dijkstrati(p);
      cout<<d[r][q]<<endl;

    }
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int line[3001][3001],c[3001][3001],t[3001][3001];
int stop[3001]={0};
int ls[3001]={0}; 
int route_cost(int p,int q){
  int top = -1;
  for(int i=0;i<ls[p];i++){
    //cout << line[p][i] << endl;
    if(stop[line[p][i]]!=0) continue;
    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]];
    else if(top<0||top>h){
      top = h+c[p][line[p][i]];
    }
  }
  stop[p] = 0;
  return top;
}
int route_time(int p,int q){
  int top = -1;
  for(int i=0;i<ls[p];i++){
    //cout << "P " << p << endl;
    //cout << line[p][i] << endl;
    if(stop[line[p][i]]!=0) continue;
    stop[p]++;
      if(q==line[p][i]){
	stop[p] = 0;
	 return t[p][q];
      }
h = route_time(line[p][i],q)+t[p][line[p][i]];
      else if(top<0||top>h){
	top = h;
	//cout << 'O' << endl;
      }
  }
  stop[p] = 0;
  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;
    }
  }
  return 0;
}

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

#define MAX_M 110

struct edge{int to,cost;};
typedef pair<int,int> P;

vector<edge> c[2][MAX_M];

int dijkstra(int s,int e,bool r){
	int d[MAX_M];
	for(int i=0;i<MAX_M;i++){
		d[i]=INT_MAX;
	}
	priority_queue<P,vector<P>,greater< P > > que;
	
	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;
		for(int i=0;i<c[r][v].size();i++){
			edge e=c[r][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));
			}
		}
	}
	
	return d[e];
}

int main(){
	int n,m;
	while(cin>>n>>m,n!=0||m!=0){
		for(int r=0;r<2;r++){
			for(int i=0;i<MAX_M;i++){
				c[r][i].resize(0);
			}
		}
		for(int i=0;i<n;i++){
			int a,b,cos,tim;
			cin>>a>>b>>cos>>tim;
			edge co,t;
			co.to=b;co.cost=cos;
			t.to=b;t.cost=tim;
			c[0][a].push_back(co);
			c[1][a].push_back(t);
		}
		cin>>n;
		for(int i=0;i<n;i++){
			int s,e;
			bool r;
			cin>>s>>e>>r;
			int ans=dijkstra(s,e,r);
			cout<<ans<<endl;
		}
	}
}