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>
#include <algorithm>
const int INF = 1000000;

int map[101][101][2];//a, b, cost, time;
int d[101];//始点からの最短距離
bool used[101];//使われたかのフラグ
int V;//頂点数

int dijkstra(int start, int end, bool flag){
	std::fill(d, d + V + 1, INF);
	std::fill(used, used + V + 1, false);
	d[start] = 0;

	while(1){
		int v = -1;
		for(int u=1;u<=V;u++){
			if(!used[u] && (v == -1 || d[u] < d[v]))v = u;
		}
		
		if(v == -1)break;
		
		used[v] = true;

		for(int u=1;u<=V;u++){
			d[u] = std::min(d[u], d[v] + map[v][u][flag]);
		}
	}

	return d[end];
}

int main(){
	int n, m;//track, station
	while(std::cin >> n >> m, n){
		V = m;

		for(int i=0;i<101;i++){
			for(int j=0;j<101;j++){
				map[i][j][0] = (map[i][j][1] = INF);
			}
		}

		for(int i=n;i;i--){
			int a, b, cost, time;
			std::cin >> a >> b >> cost >> time;
			map[a][b][0] = cost;
			map[a][b][1] = time;
			map[b][a][0] = cost;
			map[b][a][1] = time;
		}

		int k;//取り合わせ数
		std::cin >> k;
		while(k--){
			int p, q, r;
			std::cin >> p >> q >> r;//start, end, bool
			std::cout << dijkstra(p, q, r) << '\n';
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
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 < 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 <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)));
    for(int i=0;i<m;i++)d[i][i]=pii(0,0);
    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;
    }
    
}

C++: #include<iostream>
#include<functional>
#include<vector>
#include<queue>

using namespace std;

#define MMAX 100
#define INF 10000000

typedef pair<int,int> PII;

struct side{
	int dir,wei[2];
};

int main(void){

	int n,m;
	int a,b,cost,time;
	int k;
	int p,q,r;
	int nod[MMAX+1];
	vector<side> ed[MMAX+1];
	priority_queue<PII,vector<PII>,greater<PII> > pq;
	side tmp;
	PII pii;
	int nm;

	while(1){

		for(int i=0;i<=MMAX;i++) nod[i]=0;
		for(int i=0;i<=MMAX;i++){
			if(!ed[i].empty()) ed[i].clear();
		}
		while(!pq.empty()) pq.pop();

		cin>>n>>m;
		if(n==0&&m==0) break;

		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			tmp.dir=b,tmp.wei[0]=cost,tmp.wei[1]=time;
			ed[a].push_back(tmp);
			tmp.dir=a;
			ed[b].push_back(tmp);
		}

		cin>>k;

		for(int i=0;i<k;i++){

			cin>>p>>q>>r;
			for(int j=0;j<=m;j++) nod[j]=INF;
			nod[p]=0;
			pq.push(PII(0,p));

			while(!pq.empty()){
				pii=pq.top();
				pq.pop();
				nm=pii.second;
				if(nod[nm]<pii.first) continue;
				for(int j=0;j<ed[nm].size();j++){
					tmp=ed[nm][j];
					if(nod[tmp.dir]>nod[nm]+tmp.wei[r]){
						nod[tmp.dir]=nod[nm]+tmp.wei[r];
						pq.push(PII(nod[tmp.dir],tmp.dir));
					}
				}
			}
			cout<<nod[q]<<endl;

		}

	}

}