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<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 = 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];
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<functional>
#include<limits.h>

using namespace std;

struct Edge{
	int to,cost;
	Edge(){}
	Edge(int to,int cost):to(to),cost(cost){}
};

int dijkstra(const vector< vector<Edge> > vve,int s,int t){
	priority_queue<pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > > que;
	pair<int,int> pii;
	pii.first = 0;
	pii.second = s;
	que.push(pii);

	vector<int> dist(vve.size(), INT_MAX);
	
	while(!que.empty()){
		int cost = que.top().first;
		int now = que.top().second;
		que.pop();

		if(dist[now] <= cost){
			continue;
		}

		dist[now] = cost;

		for(int i=0;i<vve[now].size();i++){
			que.push( make_pair(vve[now][i].cost + cost, vve[now][i].to) );
		}
	}

	return dist[t];
}

int main(){
	int n,m;
	while(true){
		cin >> n >> m;
		if(n == 0 && m==0){
			break;
		}

		vector< vector<Edge> > coin(m);
		vector< vector<Edge> > time(m);

		for(int i=0;i<n;i++){
			int a,b,c,t;
			cin >> a >> b >> c >> t;
			coin[a-1].push_back(Edge(b-1,c));
			coin[b-1].push_back(Edge(a-1,c));
			time[a-1].push_back(Edge(b-1,t));
			time[b-1].push_back(Edge(a-1,t));
		}

		int k;
		cin >> k;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin >> p >> q >> r;
			int ans;
			if(r){
				ans = dijkstra(time,p-1,q-1);
			}else{
				ans = dijkstra(coin,p-1,q-1);
			}
			cout << ans << endl;
		}
	}
}

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

const int INF = 1000000000;

int main(){

int n,m;
int a,b,c,t;


int k;
int p[200];
int q[200];
int r[200];

while(true){
 cin >> n >> m;
 if(n==0 && m==0) break;
 int C[100][100];
 int T[100][100];
 for(int i=0; i<m; i++){
  for(int j=0; j<m; j++){
   if(i==j){
    C[i][j] = T[i][j] = 0;
   }else{
    C[i][j] = T[i][j] = INF;
   }
  }
 }
 for(int i=0; i<n; i++){
  cin >> a >> b >> c >> t;
  C[a-1][b-1] = C[b-1][a-1] = c;
  T[a-1][b-1] = T[b-1][a-1] = t;
 }

 for(int x=0; x<m; x++){
  for(int y=0; y<m; y++){ 
   for(int z=0; z<m; z++){
    if(T[y][z]>T[y][x]+T[x][z]){
     T[y][z]=T[y][x]+T[x][z];
    }
    if(C[y][z]>C[y][x]+C[x][z]){
     C[y][z]=C[y][x]+C[x][z];
    }
   }
  }
 }



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

 for(int i=0; i<k; i++){
  if(r[i]==0){
   cout << C[p[i]-1][q[i]-1] << endl;
  }else{
   cout << T[p[i]-1][q[i]-1] << endl;
  }
 }
} 

return 0;
}

C++: #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int n,m; cin>>n>>m;
    vector<vector<pii>>d(m+1,vector<pii>(m+1,pii(INF,INF)));
    rep(i,n){
        int a = 0,b = 0;pii hoge; cin>>a>>b>>hoge.first>>hoge.second;
        --a,--b;
        d[a][b]=hoge;
        d[b][a]=hoge;
    }
    rep(k,m)rep(i,m)rep(j,m)cmin(d[i][j].first,d[i][k].first+d[k][j].first);
    rep(k,m)rep(i,m)rep(j,m)cmin(d[i][j].second,d[i][k].second+d[k][j].second);
    int k; cin>>k;
    rep(i,k){
        int to,from,q; cin>>to>>from>>q;
        to--,from--;
        if(q)cout<<d[to][from].second<<endl;
        else cout<<d[to][from].first<<endl;
    }
    
}