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]
					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>
using namespace std;

typedef long long int ll;
#define REP(i,p,n) for(int i=p;i<n;++i)
#define rep(i,n) REP(i,0,n)

typedef pair<ll,ll> pii;
const ll INF=(ll)1e12, MAXN=128;
ll C[2][MAXN][MAXN], visited[MAXN], N, M;

int dk(int p,int q,int r)
{
        priority_queue<pii,vector<pii>,greater<pii> > Q;
        Q.push(pii(0,p));

        while(!Q.empty())
        {
                const ll cc=Q.top().first, pp=Q.top().second; Q.pop();
                if(pp==q){ return cc; } else { visited[pp]=1; }

                rep(i,M) if(!visited[i]){ Q.push(pii(cc+C[r][pp][i],i)); }
        }
        return 0;
}

int main()
{
        ll a,b,c,d;
        cin>>N>>M;
        rep(i,M) rep(j,N) C[0][i][j]=C[1][i][j]=(i==j?0:INF);
        rep(i,N){ cin>>a>>b>>c>>d; --a; --b; C[0][a][b]=C[0][b][a]=c; C[1][a][b]=C[1][b][a]=d; }

        cin>>d;
        rep(i,d){ fill(visited,visited+N,0); cin>>a>>b>>c; cout<<dk(a-1,b-1,c)<<endl; }
        return 0;
}

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

using namespace std;

//FILE* in = freopen("./B","r",stdin);

typedef long long LL;
#define REP(i,a,b)for(LL i=(LL)a;i<(LL)b;i++)


#define MAX_V 3100
#define INF 1000000

struct edge{int to,cost,time;edge(int t,int c,int h):to(t),cost(c),time(h){}};
typedef pair<int, int> P;

int V=MAX_V;
vector<edge> G[MAX_V];
int d[MAX_V];

int dijkstra(int s,int g,bool flag){
	priority_queue<P, vector<P>, greater<P> > que;
	fill(d, d+V, INF);
	d[s] = 0;
	que.push(P(0,s));

	while(!que.empty()){
		P p=que.top(); que.pop();
		int v = p.second;
		if(v==g)return d[v];
		if( d[v] < p.first ) continue;
		for(int i = 0;i<G[v].size();i++){
			edge e = G[v][i];
			if(flag==0){
			if(d[e.to] > d[v] + e.cost ){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to],e.to));
			}
			}else{
				if(d[e.to] > d[v] + e.time ){
				d[e.to] = d[v] + e.time;
				que.push(P(d[e.to],e.to));
			}

			}
		}
	}
	return -1;
}

int main(void){

	while(1){


		G->clear();

		int N,M;
		int cost,time,a,b;
		int K,start,goal,R;

		cin >> N>>M;

		if(!N&&!M)break;

		REP(i,0,N){
			cin >> a>>b>>cost>>time;
			G[a].push_back(edge(b,cost,time));
			G[b].push_back(edge(a,cost,time));
		}

		cin >> K;

		REP(i,0,K){
			cin >> start>>goal>>R;
			cout << dijkstra(start,goal,R)<<endl;
		}

	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int main() {
	int n, m, k;

	int costs[100][100];
	int times[100][100];

	while(cin >> n >> m ) {

		if (n == 0 && m == 0) return 0;

		for(int i = 0; i < 100; i++) {
			for(int j = 0; j < 100; j++) {
				costs[i][j] = 1e5;
				times[i][j] = 1e5;
			}
		}

		for(int i = 0; i < n; i++) {
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			a -= 1;
			b -= 1;
			costs[a][b] = costs[b][a] = cost;
			times[a][b] = times[b][a] = time;
		}

		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				for(int k = 0; k < n; k++) {
					costs[j][k] = min(costs[j][k], costs[j][i] + costs[i][k]);
					times[j][k] = min(times[j][k], times[j][i] + times[i][k]);
				}
			}
		}

		cin >> k;
		for(int i = 0; i < k; i++) {
			int p, q, r;
			cin >> p >> q >> r;
			p -= 1;
			q -= 1;
			if(r) cout << times[p][q];
			else cout << costs[p][q];
			cout << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define REP(i,n) for (int i=0; i<(n); ++i)

struct edge {
    int from, to, cost, time;
};

int main() {
    int n, m;
    while (cin >> n >> m, n || m) {
        vector<edge> edges;
        int a, b, v, u;
        REP(i,n) {
            cin >> a >> b >> v >> u;
            edges.push_back((edge){ a, b, v, u });
        }
        int d[2][101][101];
        REP(i,2) REP(j,m+1) REP(k,m+1) {
            d[i][j][k] = (j == k ? 0 : 1e8);
        }
        REP(i,m+1) REP(j,m+1) for (auto &e : edges) {
            d[0][j][e.to] = min(d[0][j][e.to], d[0][j][e.from] + e.cost);
            d[0][j][e.from] = min(d[0][j][e.from], d[0][j][e.to] + e.cost);
            d[1][j][e.to] = min(d[1][j][e.to], d[1][j][e.from] + e.time);
            d[1][j][e.from] = min(d[1][j][e.from], d[1][j][e.to] + e.time);
        }
        int k, p, q, r;
        cin >> k;
        REP(i,k) {
            cin >> p >> q >> r;
            cout << d[r][p][q] << endl;
        }
    }
    return 0;
}