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]
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[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<cstring>
#include<vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define INF 300000

struct E
{
    int a,b,c,t;
    E(){};
    E(int a,int b,int c,int t):a(a),b(b),c(c),t(t){};
};
//E cs[700];
unsigned int d[150];

int main()
{
    int n,m,k,p,q,r;
    while(cin >> n >> m)
    {
        if(n==0 && m==0)break;
        vector<E> cs;   
        int ta,tb,tc,tt;
        rep(i,n)
        {
            cin >> ta>>tb>>tc >>tt;
            cs[2*i] = E(ta,tb,tc,tt);
            cs[2*i+1] = E(tb,ta,tc,tt);
        }

        cin >> k;
        rep(i,k)
        {
            memset(d,INF,150*sizeof(int));
            cin >> p >> q >>r;
            d[p] = 0;

            bool update = false;
            if(r==0)while(true)
            {
                update = false;
                rep(j,m*2)
                {
                   if(d[cs[j].a] != INF && d[cs[j].b] > d[cs[j].a] + cs[j].c)
                   {
                       update = true;
                       d[cs[j].b] =  d[cs[j].a] + cs[j].c;
                   }
                }
                if(!update) break;
            }
            if(r==1)while(true)
            {
                update = false;
                rep(j,m*2)
                {
                   if(d[cs[j].a] != INF && d[cs[j].b] > d[cs[j].a] + cs[j].t)
                   {
                       update = true;
                       d[cs[j].b] =  d[cs[j].a] + cs[j].t;
                   }
                }
                if(!update) break;
            }
            cout << d[q] << endl;
        }
        
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#define int long long
using namespace std;

signed main() {
	int a, b;
	while (cin >>b>>a, a | b) {
		vector<vector<int>>cost(a, vector<int>(a, 2147483647));
		vector<vector<int>>time(a, vector<int>(a, 2147483647)); 
		for (int c = 0; c < b; c++) {
			int d, e, f, g;
			cin >> d >> e >> f >> g;
			d--; e--;
			cost[d][e] = cost[e][d] = f;
			time[d][e] = time[e][d] = g;
		}
		for (int h = 0; h < a; h++) {
			for (int i = 0; i < a; i++) {
				for (int j = 0; j < a; j++) {
					if (i == j)cost[i][j] = time[i][j] = 0;
					else {
						cost[i][j] = (cost[i][j] < cost[i][h] + cost[h][j]) ? (cost[i][j]) : (cost[i][h] + cost[h][j]);
						time[i][j] = (time[i][j] < time[i][h] + time[h][j]) ? (time[i][j]) : (time[i][h] + time[h][j]);
					}
				}
			}
		}
		int l;
		cin >> l;
		for (int r = 0; r < l; r++) {
			int x, y, z;
			cin >> x >> y >> z;
			x--; y--;
			if (z) { cout << time[x][y] << endl; }
			else { cout << cost[x][y] << endl; }
		}
	}
}

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){
			if(dist[t] < cost){
				break;
			}
			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;
		}
	}
}