Go: // Warshall–Floyd Algorithm

package main

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

var scanner = bufio.NewScanner(os.Stdin)

const INF = 2000000000

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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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>
#include<algorithm>
using namespace std;

#define MAX 256
#define INF 1000000

struct edge {
	int money;
	int time;
};

int main(){
	int n, m;
	int count;
	edge G[100][100];

	while(cin >> n >> m, n || m){
		for(int i=0; i < 100; i++){
			for(int j=0; j < 100; j++){
				G[i][j].money = INF;
				G[i][j].time = INF;
			}
		}
		for(int i=0; i < 100; i++){
			G[i][i].money = 0;
			G[i][i].time = 0;
		}

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

			G[a][b].time=t;
			G[a][b].money=mo;
			G[b][a].time=t;
			G[b][a].money=mo;
		}

		for(int k=0; k < n; k++){
			for(int i=0; i < n; i++){
				for(int j=0; j < n; j++){
					G[i][j].time = min(G[i][j].time, G[i][k].time+G[k][j].time);
					G[i][j].money = min(G[i][j].money, G[i][k].money+G[k][j].money);
				}
			}
		}

		cin >> count;
		for(int i=0; i < count; i++){
			int p;
			cin >> n >> m >> p;

			if(p) cout << G[n][m].time << endl;
			else cout << G[n][m].money << endl;
		}
	}

}

C++: #include<iostream>
#include<set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define INF 1e6
using namespace std;
int n,m,k,a,b,c,alt,p,q,r;
long long cost[101][101],tm[101][101],d[101];
bool used[101];

void dfunc(int s,int r){
	d[s]=0;
	rep(i,101){
		used[i]=true;
		d[i]=INF;
	}
	while(1){
		int mem=INF,now=-1;
		rep(i,101){
			if(used[i] && mem>d[i]){
				now=i;
				mem=d[i];
			}
		}
		if(now==-1)break;
		used[now]=false;
		rep(i,101){
			if(r==0){
				if(cost[now][i]!=INF)d[i]=d[now]+cost[now][i];
			}else{
				if(tm[now][i]!=INF)d[i]=d[now]+tm[now][i];
			}
		}
	}
}

int main(){
	while(cin>>n>>m,n||m){
		rep(i,101)rep(j,101){
			cost[i][j]=INF;
			tm[i][j]=INF;
		}
		rep(i,n){
			cin>>a>>b>>c>>alt;
			a--;
			b--;
			cost[b][a]=cost[a][b]=c;
			tm[b][a]=tm[a][b]=alt;
		}
		cin>>k;
		rep(i,k){
			cin>>p>>q>>r;
			dfunc(p,r);
			cout<<d[q]<<endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int,int> P;
#define INF (1<<24)
#define MAX_V 100

int V,E,a,b,c,d,R;

vector< vector<P> > t;
vector< vector<P> > u;

void init();
int dijkstra(int,int,vector< vector<P> >&);

int main(){
  while(cin>>E>>V){
    if(V==0&&E==0)break;
    init();
    for(int i=0;i<E;i++){
      cin>>a>>b>>c>>d;
      b--;a--; 
      t[a].push_back(P(b,c));
      t[b].push_back(P(a,c));
      u[a].push_back(P(b,d));
      u[b].push_back(P(a,d));
    }
    cin>>R;
    for(int i=0;i<R;i++){
      cin>>a>>b>>c;
      a--;b--;
      cout<<dijkstra(a,b,(c?u:t))<<endl;
    }
  }
  return 0;
}


int dijkstra(int si,int ti,vector< vector<P> > &G){
  int d[MAX_V];
  for(int i=0;i<MAX_V;i++)d[i]=INF;
  d[si]=0;
  priority_queue< P , vector<P> , greater<P> > Q;
  Q.push(P(0,si));
  while(!Q.empty()){
    P p=Q.top();Q.pop();
    int pos=p.second,cost=p.first;
    for(int i=0;i<(int)G[pos].size();i++){
      int v=G[pos][i].first;
      int w=G[pos][i].second;
      if(cost+w>=d[v])continue;
      d[v]=cost+w;
      Q.push(P(d[v],v));
    }
  }
  return d[ti];
}

void init(){
  t.clear();
  u.clear();
  t.resize(V);
  u.resize(V);
}