Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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
					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<vector>

using namespace std;

int data_cost[101][101],data_time[101][101],prev[101][101],total_cost[101][101],total_time[101][101];
vector<int> array[101];

void serch_cost(int st,int now)
{
//	cout << "start_serch:" << now << " nowsize:" << array[now].size() << endl;
	int i,flag=0;
	for(i=0;i<array[now].size();i++)
	{
		if(prev[now][array[now][i]]==1)
			flag++;
	}
	if(flag==array[now].size())
	{
		return ;
	}
	else
	{
		total_cost[st][st]=0;
		for(i=0;i<array[now].size();i++)
		{
			if(prev[now][array[now][i]]!=1)
			{
				if(total_cost[st][array[now][i]]>total_cost[st][now]+data_cost[now][array[now][i]])
				{
//					cout << "1:" << total_cost[st][now]+data_cost[now][array[now][i]] << " " << now << endl;
					total_cost[st][array[now][i]]=total_cost[st][now]+data_cost[now][array[now][i]];
//					cout << "2:" << total_cost[st][array[now][i]] << " " << array[now][i] << endl;
				}
			}
//			cout << "cost_1:" << "st:" << st << " " << total_cost[st][now]+data_cost[now][array[now][i]] << " now:" << now << endl;
//			cout << "cost_2:" << "st:" << st << " " << total_cost[st][array[now][i]] << " " << array[now][i] << endl;
//			cout << "st:" << st << " now:" << now << " next:" << array[now][i] << endl;
			prev[now][array[now][i]]=prev[array[now][i]][now]=1;
		}
		for(int j=0;j<array[now].size();j++)
		{
			serch_cost(st,array[now][j]);
		}
	}
	return ;
}

void serch_time(int st,int now)
{
//	cout << "start_serch:" << now << " nowsize:" << array[now].size() << endl;
	int i,flag=0;
	for(i=0;i<array[now].size();i++)
	{
		if(prev[now][array[now][i]]==1)
			flag++;
	}
	if(flag==array[now].size())
	{
		return ;
	}
	else
	{
		total_time[st][st]=0;
		for(i=0;i<array[now].size();i++)
		{
			if(prev[now][array[now][i]]!=1)
			{
				if(total_time[st][array[now][i]]>total_time[st][now]+data_time[array[now][i]][now])
				{
//					cout << "1:" << total_time[st][now]+data_time[array[now][i]][now] << " " << now << endl;
					total_time[st][array[now][i]]=total_time[st][now]+data_time[array[now][i]][now];
//					cout << "2:" << total_time[st][array[now][i]] << " " << array[now][i] << endl;
				}
			}
//			cout << "time_1:" << "st:" << st << " " << total_time[st][now]+data_time[array[now][i]][now] << " now:" << now << endl;
//			cout << "time_2:" << "st:" << st << " " << total_time[st][array[now][i]] << " " << array[now][i] << endl;
//			cout << "time_st:" << st << " now:" << now << " next:" << array[now][i] << endl;
			prev[now][array[now][i]]=prev[array[now][i]][now]=1;
		}
		for(int j=0;j<array[now].size();j++)
		{
			serch_time(st,array[now][j]);
		}
	}
	return ;
}

int main()
{
	
	int n,m,i,j;
	
	for(;;)
	{
		cin >> n >> m ;
		if(n==0 && m==0)
			break;
		
		
		for(i=0;i<101;i++)
		{
			array[i].clear();
		}
		
		for(i=0;i<n;i++)
		{
			int a,b,cost,time;
			cin >> a >> b >> cost >> time ;
			array[a].push_back(b);
			array[b].push_back(a);
			data_cost[a][b]=cost;
			data_time[a][b]=time;
			data_cost[b][a]=cost;
			data_time[b][a]=time;
		}
//		for(i=1;i<m+1;i++)
//		{
//			for(j=1;j<m+1;j++)
//			{
//				cout << data[1][2] << " " ;
//			}
//			cout << endl;
//		}
		
		for(i=1;i<m+1;i++)
		{
			for(j=1;j<m+1;j++)
			{
				total_cost[i][j]=1000000;
				for(int k=1;k<m+1;k++)
				{
					prev[j][k]=0;
				}
			}
			serch_cost(i,i);
			for(j=1;j<m+1;j++)
			{
				total_time[i][j]=1000000;
				for(int k=1;k<m+1;k++)
				{
					prev[j][k]=0;
				}
			}
			serch_time(i,i);
		}
		int  num;
		cin >> num ;
		
		for(int k=0;k<num;k++)
		{
			int st,en,se;
			cin >> st >> en >> se ;
			
/*			cout << "cost" << endl;
			for(int l=1;l<m+1;l++)
			{
				for(int o=1;o<m+1;o++)
				{
					cout << total_cost[l][o] << " " ;
				}
				cout << endl;
			}
			cout << "time" << endl;
			for(int l=1;l<m+1;l++)
			{
				for(int o=1;o<m+1;o++)
				{
					cout << total_time[l][o] << " " ;
				}
				cout << endl;
			}
*/
			
			if(se==0)
				cout << total_cost[st][en] << endl;
			else
				cout << total_time[st][en] << endl;
		}
	}
}