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
			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>
#include <vector>
#include <algorithm>
#include <queue>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define SORT(c) sort((c).begin(), (c).end())
#define pb push_back
using namespace std;

const int INF=10000000;
int V;
struct edge {
    int to,money, time;
};
struct state {
    int from, d;
    bool operator<(const state& a)const{
        return d>a.d;
    }
};
vector<vector<edge> > G;

int d[111];
int ds(int s,int g, int r){
    FOR(i,0,111)d[i]=INF;
    d[s]=0;
    priority_queue<state> q;
    q.push({s,0});
    while(q.size()){
        state sta=q.top();
        q.pop();
        int from=sta.from;
        FOR(i,0,G[from].size()){
            const edge& e=G[from][i];
            int to=e.to;
            int cost=r?e.time:e.money;
            if(d[to]>d[from]+cost){
                d[to]=d[from]+cost;
                q.push({to,d[to]});
            }
        }
    }
    return d[g];
}

int main(){
    int n;
    while(1){
        cin>>n>>V;
        if(!(n||V)) break;
        G=vector<vector<edge> >(n+1);
        FOR(i,0,n){
            int a,b,m,t;cin>>a>>b>>m>>t;
            G[a].pb({b,m,t});
            G[b].pb({a,m,t});
        }
        int k;cin>>k;
        FOR(i,0,k){
            int p,q,r;cin>>p>>q>>r;
            cout<<ds(p,q,r)<<endl;
        }
    }
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
#include<set>
using namespace std;
int stoi(string x){stringstream ss;ss<<x;int tmp;ss>>tmp;return tmp;}
string itos(int x){stringstream ss;ss<<x;return ss.str();}
const int INF=1100000000;
int G1[250][250],G2[250][250];
int d1[250],d2[250];
bool u1[250],u2[250];
int main(){
	int n,m,r,s,e,f;
	while(cin>>n>>m,n!=0||m!=0){
	
	for(int i=0;i<250;i++)fill(G1[i],G1[i]+250,INF),fill(G2[i],G2[i]+250,INF);
	for(int i=0;i<n;i++){
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	G1[a][b]=G1[b][a]=c;
	G2[a][b]=G2[b][a]=d;
	}
	cin>>r;
for(int ccnt=0;ccnt<r;ccnt++){
	cin>>s>>e>>f;
	for(int i=0;i<250;i++)d1[i]=d2[i]=INF,u1[i]=u2[i]=0;
	d1[s]=0;
	int mini,to;
	for(int i=0;i<m;i++){
	mini=INF,to=0;
		for(int j=1;j<=m;j++){if(mini>d1[j]&&u1[j]==0)mini=d1[j],to=j;}
	for(int j=1;j<=m;j++)d1[j]=min(d1[j],G1[to][j]+d1[to]);
	u1[to]=1;
	}
	d2[s]=0;
	for(int i=0;i<m;i++){
		mini=INF,to=0;
		for(int j=1;j<=m;j++)if(mini>d2[j]&&u2[j]==0)mini=d2[j],to=j;
		for(int j=1;j<=m;j++)d2[j]=min(d2[j],G2[to][j]+d2[to]);
		u2[to]=1;
	}
	if(f==0)cout<<d1[e]<<endl;
	else cout<<d2[e]<<endl;
}
}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <queue>
using namespace std;
typedef pair<int,int> pr;
typedef priority_queue<pr,vector<pr >,greater<pr> > pq;
const int INF=1<<30;
int Dijkstra(int start,int goal,vector<pr> cost[101]){
	int ans[101];
	for(int i=0;i<101;i++)
		ans[i]=INF;	
	ans[start]=0;
	pq p;
	p.push(pr(0,start));
	while(!p.empty()){
		int f=p.top().first,s=p.top().second;
		p.pop();
		if(f!=ans[s]) continue;
		for(int i=0;i<cost[s].size();i++){
			int next=cost[s][i].second;
			if(ans[next]>ans[s]+cost[s][i].first){
				ans[next]=ans[s]+cost[s][i].first;
				p.push(pr(ans[next],next));
			}
		}
	}
	return ans[goal];
}
int main() {
	// your code goes here
	int m,n,a,b,c,d;
	while(cin >> n >> m && n){
	vector<pr> price[101],time[101];
	for(int i=0;i<n;i++){
		cin >> a>>b>>c>>d;
		price[a].push_back(pr(c,b));
		price[b].push_back(pr(c,a));
		time[a].push_back(pr(d,b));
		time[b].push_back(pr(d,a));
	}
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> a>> b >>c;
		if(!c)
	cout << Dijkstra(a,b,price) << endl;
	else
	cout <<	Dijkstra(a,b,time) << endl;
	}
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>

#define NMAX 3000+10
#define MMAX 100+10
#define TMAX 1000+10
#define CMAX 1000+10
#define KMAX 200+10

using namespace std;

int main(){
	std::ios_base::sync_with_stdio(false);

	int n,m;
	int a,b,time,cost;
	int data[MMAX+5][MMAX+5][2+1];
	int k;
	int p,q,r;
	int dp[MMAX+5];
	queue<int> now;

	while(1){
		cin>>n>>m;
		for(int i=0;i<m+5;i++){
			for(int j=0;j<m+5;j++){
				for(int k=0;k<2;k++){
					data[i][j][k]=-1;
				}
			}
		}
		if(n==0&&m==0) break;
		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			data[a][b][1]=time;
			data[b][a][1]=time;
			data[a][b][0]=cost;
			data[b][a][0]=cost;
		}

		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;

			for(int i=0;i<m+5;i++){
				dp[i]=-1;
		    }

			now.push(p);
			dp[p]=0;
			while(!now.empty()){
				for(int i=0;i<n;i++){
					if(data[now.front()][i][r]!=-1){
						if(dp[i]==-1||dp[i]>dp[now.front()]+data[now.front()][i][r]){
							dp[i]=dp[now.front()]+data[now.front()][i][r];
							now.push(i);
						}
					}
				}
				now.pop();
			}
			cout<<dp[q]<<endl;
		}

	}



	return 0;
}

C++: #include<iostream>
using namespace std;
#include<algorithm>
#include<string>
#define REP(i,a) for(int  i=0;i<a;i++)
int A[101][101];
int B[101][101];
int main(){
	int a,b=0,c=0,d,n,m;	
	cin>>n>>m;
	while(n!=0||m!=0){
	REP(i,m){
		REP(j,m){
			A[i][j]=1002;
			B[i][j]=1002;
		}
		A[i][i]=0;
		B[i][i]=0;
	}
	REP(i,n){
	cin>>a>>b>>c>>d;
	A[a-1][b-1]=c;
	B[a-1][b-1]=d;	
	A[b-1][a-1]=c;
	B[b-1][a-1]=d;
	}
	REP(k,m){
		REP(i,m){
		if(i==k) continue;
	 		REP(j,m){
		if(i==j)continue;
		A[i][j]=min(A[i][j],A[i][k]+A[k][j]);
		B[i][j]=min(B[i][j],B[i][k]+B[k][j]);
			}
		}	
	}
	cin>>n;
	REP(i,n){
	cin>>a>>b>>c;
	if(c==0) cout<<A[a-1][b-1]<<endl;
	else cout<<B[a-1][b-1]<<endl;
	}
	cin>>n>>m;
	}
	return 0;
}