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 <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
#include <unordered_map> //???????????\??¢??°

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
//#define TEST //*******************************************************************************************************************************************

//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
#ifdef TEST
    #define dout cout
#else
    stringstream dummy; //???????????°??????dout?????????????????????????????????
    #define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
    //???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif


#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << (A) << " "
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)

#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl



const int INF = 1e5-1;

int main(void) {
    
    int N; //????????°
    int M; //?????°
    int a, b, c, t;
    
    while(1) {
        cin >> M >> N;
        if( M==0 && N==0 ) break;
    
        //initialization
        int minCost[N+1][N+1]; //1~N??????????????????
        int minTime[N+1][N+1]; //1~N??????????????????
        
        rep(i,0,N+1) {
            rep(j,0,N+1) {
                minCost[i][j] = (i==j ? 0 : INF);
                minTime[i][j] = (i==j ? 0 : INF);
            }
        }

        rep(i,0,M) {
            cin >> a >> b >> c >> t; //??????a??????b?????§????????????t
            
            minCost[a][b] = minCost[b][a] = c;
            minTime[a][b] = minTime[b][a] = t;
        }
        
        
        //test display
        rep(i,0,N+1) {
            disP(i);
            dispAll(minCost[i], N+1);
        }
        dout << endl;
        
        //test display
        rep(i,0,N+1) {
            disP(i);
            dispAll(minTime[i], N+1);
        }
        dout << endl;

        
        
        //Warshall Floyd Algorithm
        rep(k,1,N+1) {
            rep(i,1,N+1) {
                rep(j,1,N+1) {
                    minCost[i][j] = min( minCost[i][j], minCost[i][k] + minCost[k][j] );
                    minTime[i][j] = min( minTime[i][j], minTime[i][k] + minTime[k][j] );
                }
            }
        }
        
        
        //test display
        rep(i,0,N+1) {
            disP(i);
            dispAll(minCost[i], N+1);
        }
        dout << endl;
        
        //test display
        rep(i,0,N+1) {
            disP(i);
            dispAll(minTime[i], N+1);
        }
        dout << endl;
        
        
        //read next dataset (about inquiry)
        int numOfQuery;
        cin >> numOfQuery;
        
        bool needTime = 0;
        
        //answer each query
        rep(i,0,numOfQuery) {
            cin >> a >> b >> needTime;
            
            cout << (needTime ? minTime[a][b] : minCost[a][b] ) << endl;
        }
        
    }
    
    return 0;
}

C++: #include <cstdio>
#include <queue>
#include <algorithm>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef pair<int, int> P;

const int inf = (int)2e5;

struct E{
	E(int t, int c) :
		t(t), c(c)
	{}

	int t, c;
};

int n, m;
vector<E> g[2][100];
int d[100];

int dijkstra(vector<E>* g, int start, int goal){
	fill(d, d + n, inf);

	priority_queue<P, vector<P>, greater<P> > q;
	d[start] = 0;
	q.push(P(0, start));
	while(!q.empty()){
		P v = q.top();
		q.pop();

		if(v.first > d[v.second]){
			continue;
		}

		rep(i, g[v.second].size()){
			E e = g[v.second][i];
			if(d[e.t] > d[v.second] + e.c){
				d[e.t] = d[v.second] + e.c;
				q.push(P(d[e.t], e.t));
			}
		}
	}

	return d[goal];
}

int main(){
	while(1){
		scanf("%d%d", &m, &n);
		if(n == 0 && m == 0){
			break;
		}

		rep(i, m){
			int a, b, c, d;
			scanf("%d%d%d%d", &a, &b, &c, &d);

			g[0][--a].push_back(E(--b, c));
			g[0][b].push_back(E(a, c));

			g[1][a].push_back(E(b, d));
			g[1][b].push_back(E(a, d));
		}

		int z;
		scanf("%d", &z);
		rep(i, z){
			int t, a, b;
			scanf("%d%d%d", &a, &b, &t);
			printf("%d\n", dijkstra(g[t], a - 1, b - 1));
		}

		rep(i, n){
			g[0][i].clear();
			g[1][i].clear();
		}
	}
	return 0;
}