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 <algorithm>
#include <cstdio>
#include <climits>
#include <vector>
using namespace std;

int main() {
    while(true) {
        int n,m; scanf("%d%d", &n, &m);
        if(m==0) break;
        vector<vector<pair<int,pair<int,int> > > > graph;
        graph.resize(m);
        for(int i = 0; i < n; i++) {
            int a,b,c,t; scanf("%d%d%d%d", &a, &b, &c, &t); a--; b--;
            graph[a].push_back(make_pair(b,make_pair(c,t)));
            graph[b].push_back(make_pair(a,make_pair(c,t)));
        }
        int k; scanf("%d", &k);
        for(int ii = 0; ii < k; ii++) {
            int p,q,r; scanf("%d%d%d", &p, &q, &r); p--; q--;
            const int INIT = INT_MAX/2;
            const int FIN = INT_MAX/2-1;
            static int segt[1<<8];
            fill(segt, segt+(1<<8), INIT);
            segt[p+(1<<7)] = 0;
            for(int j = p+(1<<7); j > 1; j>>=1) segt[j>>1] = min(segt[j],segt[j^1]);
            while(segt[1] < FIN) {
                int j;
                for(j = 1; j < (1<<7);) if(segt[j*2]<segt[j*2+1]) j=j*2; else j=j*2+1;
                int cur = j&~(1<<7);
                int curcost = segt[j];
                segt[j] = FIN;
                for(; j > 1; j>>=1) segt[j>>1] = min(segt[j],segt[j^1]);
                if(cur == q) {
                    printf("%d\n", curcost);
                    break;
                }
                for(int i = 0; i < (int)graph[cur].size(); i++) {
                    int to = graph[cur][i].first;
                    int ecost = graph[cur][i].second.first;
                    if(r==1) ecost = graph[cur][i].second.second;
                    if(segt[to+(1<<7)] != FIN && segt[to+(1<<7)] > curcost+ecost) {
                        segt[to+(1<<7)] = curcost+ecost;
                        for(j = to+(1<<7); j > 1; j>>=1) segt[j>>1] = min(segt[j],segt[j^1]);
                    }
                }
            }
        }
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

void warshall_floyd(int n, vector<vector<int>> &dist){
    for(int k=0; k<n; k++){
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
}

int main(){
    while(1){
        int n, m; cin>>n>>m;
        if(n == 0) return 0;

        vector<vector<int>> cost(m, vector<int>(m, 1e9));
        vector<vector<int>> time(m, vector<int>(m, 1e9));
        for(int i=0; i<m; i++){
            cost[i][i] = time[i][i] = 0;
        }

        for(int i=0; i<n; i++){
            int s, t, co, ti; cin>>s>>t>>co>>ti;
            s--, t--;
            cost[s][t] = cost[t][s] = co;
            time[s][t] = time[t][s] = ti;
        }

        warshall_floyd(m, cost);
        warshall_floyd(m, time);

        int q; cin>>q;
        while(q--){
            int s, t, r; cin>>s>>t>>r;
            s--, t--;
            if(r == 0){
                cout << cost[s][t] << endl;
            }
            else{
                cout << time[s][t] << endl;
            }
        }
    }
}

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

using namespace std;

struct RequestObj {
	int begin, end, type;
};

int n, m;//???,???????????°
int k;//????????¨????????°
int time_table[100][100];
int cheap_table[100][100];
RequestObj request[200];


const int INF = 10000000;

int main() {
	while (cin >> n >> m, n * m) {

		for (int i = 0; i < m; i++)for (int j = 0; j < m; j++)time_table[i][j] = INF, cheap_table[i][j] = INF;
		
		for (int i = 0; i < n; i++) {
			int a, b;
			cin >> a >> b;
			cin >> cheap_table[a - 1][b - 1] >> time_table[a - 1][b - 1];
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> request[i].begin >> request[i].end >> request[i].type;
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					if (cheap_table[j][i] + cheap_table[i][k] < cheap_table[j][k]) {
						cheap_table[j][k] = cheap_table[j][i] + cheap_table[i][k];
					}
				}
			}
		}

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					if (time_table[j][i] + time_table[i][k] < time_table[j][k]) {
						time_table[j][k] = time_table[j][i] + time_table[i][k];
					}
				}
			}
		}
		for (int i = 0; i < k; i++) {
			if (request[i].type == 0) {
				cout << cheap_table[request[i].begin - 1][request[i].end - 1] << endl;
			}
			else {
				cout << time_table[request[i].begin - 1][request[i].end - 1] << endl;
			}
		}

		for (int i = 0; i < m; i++)for (int j = 0; j < m; j++)time_table[i][j] = INF, cheap_table[i][j] = INF;

	}
	return 0;
}