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<vector>
#include<queue>
#define MAX_M 101
using namespace std;

struct list{
	int a;
	int b;
	int cost;
	int time;
	struct list *next;
};

//Å¬àzðÔ·
int checkcost(int m, int p, int q, struct list *head);

//ÅZÔðÔ·
int checktime(int m, int p, int q, struct list *head);

//XgÉVKo^
struct list *addlist(int a, int b, int cost, int time, struct list *head);

int main(){
	//n = üHÌîñÌ, m = wÌ
	int n, m;
	while(cin >>n >>m){
		if(n == 0 && m == 0) break;
		struct list *head;
		head = NULL;
		for(int i = 0; i < n; i++){
			int a, b, cost, time;
			cin >>a >>b >>cost >>time;
			head = addlist(a, b, cost, time, head);
		}
		//k = â¢í¹Ì, p = o­n, q = ÚIn, r = oÍ·élÌíÞ(0 : Å¬àz, 1 : ÅZÔ)
		int k, p, q, r;
		cin >>k;
		for(int i = 0; i < k; i++){
			cin >>p >>q >>r;
			int ans;
			if(r == 0){
				ans = checkcost(m, p, q, head);
			} else{
				ans = checktime(m, p, q, head);
			}
			cout <<ans <<endl;
		}
	}
	return 0;
}

int checkcost(int m, int p, int q, struct list *head){
	//Y¦ÌÔðÂwÉs­½ßÌAÅ¬àzªmèµÄ¢é©Ç¤©ð²×ézñ
	//(mèµÄ¢éê:»Ìàz, mèµÄ¢È¢ê:-1)
	int checkend[MAX_M + 1] = {-1};
	for(int i = 0; i < MAX_M; i++){
		checkend[i] = -1;
	}
	
	checkend[p] = 0;
	
	//Y¦ÌÔðÂwÉs­½ßÌAÅ¬àzÌóâðüêévector
	vector<int> costlist[MAX_M + 1];
	
	while(1){
		
		//`FbN·×«costlistÌY¦ðÛ¶·équeue
		queue<int> check_i;
		
		struct list *po = head;
		
		while(po != NULL){
			
			//cout <<"po->a = " <<po->a <<" po->b = " <<po->b <<endl;
			
			//po->aÌwÌàzªmèµÄ¨èApo->bÌwÌàzªmèµÄ¢È¢Æ«
			if((checkend[po->a] != -1 && checkend[po->b] == -1)){
				
				int temp = po->cost + checkend[po->a];
				costlist[po->b].push_back(temp);
				check_i.push(po->b);
				//cout <<"case1 : temp = " <<po->cost <<" + " <<checkend[po->a] <<" push = " <<po->b <<endl;
				//cout <<"in = " <<costlist[po->b][0] <<endl;
				
			//po->bÌwÌàzªmèµÄ¨èApo->aÌwÌàzªmèµÄ¢È¢Æ«
			} else if((checkend[po->a] == -1 && checkend[po->b] != -1)){
				int temp = po->cost + checkend[po->b];
				costlist[po->a].push_back(temp);
				check_i.push(po->a);
				//cout <<"case2 : temp = " <<po->cost <<" + " <<checkend[po->b] <<" push = " <<po->a <<endl;
			}
			po = po->next;
		}
		
		//cout <<"end" <<endl;
		
		int tempi = check_i.front();
		int mincost = costlist[tempi][0], mini = tempi;
		while(1){
			check_i.pop();
			
			//check_iÅwèµ½Y¦Ìcostlist[tempi]Ì©çAÅ¬ÌàzÆ»ÌY¦ðIÔ
			for(int k = 0; k < costlist[tempi].size(); k++){
				//cout <<"costlist[" <<tempi <<"][" <<k <<"] = " <<costlist[tempi][k] <<endl;
				if(mincost > costlist[tempi][k]){
					mincost = costlist[tempi][k];
					mini = tempi;
				}
			}
			if(check_i.empty()) break;
			tempi = check_i.front();
		}
		//²×½Å¬ÌàzÆ»ÌY¦ðcheckend[i]Éüêé
		//cout <<"@mini = " <<mini <<" mincost = " <<mincost <<endl;
		checkend[mini] = mincost;
		
		for(int i = 0; i < m; i++){
			//cout <<"checkend[" <<i <<"] = " <<checkend[i] <<endl;
		}
		
		//ß½¢wÌÅ¬àzðmèµI¦½©Ì`FbN
		if(checkend[q] != -1) break;
	}
	return checkend[q];
}

int checktime(int m, int p, int q, struct list *head){
	//Y¦ÌÔðÂwÉs­½ßÌAÅ¬àzªmèµÄ¢é©Ç¤©ð²×ézñ
	//(mèµÄ¢éê:»Ìàz, mèµÄ¢È¢ê:-1)
	int checkend[MAX_M + 1] = {-1};
	for(int i = 0; i < MAX_M; i++){
		checkend[i] = -1;
	}
	
	checkend[p] = 0;
	
	//Y¦ÌÔðÂwÉs­½ßÌAÅ¬àzÌóâðüêévector
	vector<int> timelist[MAX_M + 1];
	
	while(1){
		
		//`FbN·×«costlistÌY¦ðÛ¶·équeue
		queue<int> check_i;
		
		struct list *po = head;
		
		while(po != NULL){
			
			//cout <<"po->a = " <<po->a <<" po->b = " <<po->b <<endl;
			
			//po->aÌwÌàzªmèµÄ¨èApo->bÌwÌàzªmèµÄ¢È¢Æ«
			if((checkend[po->a] != -1 && checkend[po->b] == -1)){
				
				int temp = po->time + checkend[po->a];
				timelist[po->b].push_back(temp);
				check_i.push(po->b);
				//cout <<"case1 : temp = " <<po->cost <<" + " <<checkend[po->a] <<" push = " <<po->b <<endl;
				//cout <<"in = " <<costlist[po->b][0] <<endl;
				
			//po->bÌwÌàzªmèµÄ¨èApo->aÌwÌàzªmèµÄ¢È¢Æ«
			} else if((checkend[po->a] == -1 && checkend[po->b] != -1)){
				int temp = po->time + checkend[po->b];
				timelist[po->a].push_back(temp);
				check_i.push(po->a);
				//cout <<"case2 : temp = " <<po->cost <<" + " <<checkend[po->b] <<" push = " <<po->a <<endl;
			}
			po = po->next;
		}
		
		//cout <<"end" <<endl;
		
		int tempi = check_i.front();
		int mintime = timelist[tempi][0], mini = tempi;
		while(1){
			check_i.pop();
			
			//check_iÅwèµ½Y¦Ìcostlist[tempi]Ì©çAÅ¬ÌàzÆ»ÌY¦ðIÔ
			for(int k = 0; k < timelist[tempi].size(); k++){
				//cout <<"costlist[" <<tempi <<"][" <<k <<"] = " <<costlist[tempi][k] <<endl;
				if(mintime > timelist[tempi][k]){
					mintime = timelist[tempi][k];
					mini = tempi;
				}
			}
			if(check_i.empty()) break;
			tempi = check_i.front();
		}
		//²×½Å¬ÌàzÆ»ÌY¦ðcheckend[i]Éüêé
		//cout <<"@mini = " <<mini <<" mincost = " <<mincost <<endl;
		checkend[mini] = mintime;
		
		for(int i = 0; i < m; i++){
			//cout <<"checkend[" <<i <<"] = " <<checkend[i] <<endl;
		}
		
		//ß½¢wÌÅ¬àzðmèµI¦½©Ì`FbN
		if(checkend[q] != -1) break;
	}
	return checkend[q];
}

struct list *addlist(int a, int b, int cost, int time, struct list *head){
	struct list *po;
	
	if ((po = (struct list *) malloc(sizeof(struct list))) == NULL) {
		printf("malloc error\n");
		exit(EXIT_FAILURE);
	}
	
	po->a = a;
	po->b = b;
	po->cost = cost;
	po->time = time;
	po->next = head;
	head = po;
	
	return head;
}