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<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#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<s;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<long long,long long> pii;
#define MAX 99999999
signed main(){
while(1){
	int n,m;
	cin>>n>>m;
    if(!(n+m))break;
	vector<vector<pii>>v(n+1,vector<pii>(n+1,pii(INF,INF)));
	rep(i,n+1)v[i][i]=pii(INF,INF);
	rep(i,n){
		pii temp;
		int s,g;
		cin>>s>>g>>temp.first>>temp.second;
        v[--s][--g]=temp;
	}
	int k; cin>>k;
    rep(u,k){
    int s,g; int r;
    cin>>s>>g>>r;
    if(!r){
        rep(k,m){
            rep(j,m){
                rep(i,m){
                    v[i][j].first=min(v[i][k].first+v[k][j].first,v[i][j].first);
                }
            }
        }
        cout<<v[--s][--g].first<<endl;
    }else{
        rep(k,m){
            rep(j,m){
                rep(i,m){
                    v[i][j].second=min(v[i][k].second+v[k][j].second,v[i][j].second);
                }
            }
        }
        cout<<v[--s][--g].second<<endl;
    }
    }
    }
}

C++: #include <algorithm>
#include <iostream>
#define INF 100000000
using namespace std;

int main() {
	int n, m;
	while(cin >> n >> m, n && m) {
		int s[2][101][101];

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

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

		for(int i = 1; i <= m; ++i) {
			for(int j = 1; j <= m; ++j) {
				for(int k = 1; k <= m; ++k) {
					s[0][j][k] = min(s[0][j][k], s[0][j][i] + s[0][i][k]);
					s[1][j][k] = min(s[1][j][k], s[1][j][i] + s[1][i][k]);
				}
			}
		}

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

C++: #include <cstdio>
#include <vector>
#include <queue>
using namespace std;
static const int MAX = 101;
static const int INFTY = (1 << 30);
int V;
vector< pair<int, int> > G[MAX], G2[MAX];

void dijkstra(int r, int t, int type) {
    std::priority_queue<std::pair<int, int> > pq;
    bool visited[V];
    int d[V];
    for (int i = 0; i < V; i++) {
        visited[i] = false;
        d[i] = INFTY;
    }
    d[r] = 0;
    pq.push({0, r}); //dist from target * -1, current node
    visited[r] = true;
    if (type == 0) {
        while ( !pq.empty() ) {
            pair<int, int> f = pq.top(); pq.pop();
            int current = f.second;
            visited[current] = true;

            if (d[current] < f.first * (-1)) continue;

            for (unsigned long i = 0; i < G[current].size(); i++) {
                int target = G[current][i].first;
                if (visited[target]) continue;
                if (d[target] > d[current] + G[current][i].second) {
                    d[target] = d[current] + G[current][i].second;
                    pq.push({d[target] * (-1), target});
                }
            }
        }
    }

    else if (type == 1) {
        while ( !pq.empty() ) {
            pair<int, int> f = pq.top(); pq.pop();
            int current = f.second;
            visited[current] = true;

            if (d[current] < f.first * (-1)) continue;

            for (unsigned long i = 0; i < G2[current].size(); i++) {
                int target = G2[current][i].first;
                if (visited[target]) continue;
                if (d[target] > d[current] + G2[current][i].second) {
                    d[target] = d[current] + G2[current][i].second;
                    pq.push({d[target] * (-1), target});
                }
            }
        }
    }

    printf("%d\n", d[t]);

}

int main()
{
    int E, source, target, dist, cost, type, m;
    for (; ;) {
        std::scanf("%d %d", &E, &V);
        if (E == 0 && V == 0) break;
        for (int i = 0; i < E; i++) {
            std::scanf("%d %d %d %d", &source, &target, &dist, &cost);
            source--; target--;
            G[source].push_back({target, dist});
            G[target].push_back({source, dist});
            G2[target].push_back({source, cost});
            G2[source].push_back({target, cost});
        }
        scanf("%d", &m);
        for (int i = 0; i < m; i++) {
            scanf("%d %d %d", &source, &target, &type);
            source--; target--;
            dijkstra(source, target, type);
        }
        for (int i = 0; i < V; i++) { G[i].clear(); G2[i].clear(); }
    }
    return 0;
}