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])
			}
		}
	}
}

Python: import heapq
def dijkstra(graph, size, start, r):
    distance = [float('inf')] * size
    distance[start] = 0
    que = []
    heapq.heappush(que, (0, start))
    while len(que):
        _, u = heapq.heappop(que)
        for length, vi in graph[u]:
            if distance[vi] > distance[u] + length[r]:
                distance[vi] = distance[u] + length[r]
                heapq.heappush(que, (distance[vi], vi))
    return distance

import sys
f = sys.stdin

import collections

while True:
    n, m = map(int, f.readline().split())
    if n == 0:
        break
    abct = [list(map(int, f.readline().split())) for _ in range(n)]
    graph = collections.defaultdict(list)
    for ai, bi, cost, time in abct:
        graph[ai].append(((cost, time), bi))
        graph[bi].append(((cost, time), ai))
        
    k = int(f.readline())
    for i in range(k):
        p, q, r = map(int, f.readline().split())
        dist = dijkstra(graph, m + 1, p, r)
        print(dist[q])

Python: from heapq import heappop, heappush


class WeightedEdge:
    """Weighted edge."""

    def __init__(self, a, b, weight):
        self.a = a
        self.b = b
        self.weight = weight

    def __repr__(self):
        return "(%d - %d: %d)" % (self.a, self.b, self.weight)

    def __lt__(self, other):
        return self.a < other.b


class EdgeWeightedGraph:
    """Undirected Edge-weighted graph (1-n indexed)."""

    def __init__(self, V):
        self.__adj = [[] for _ in range(V + 1)]

    def add_edge(self, a, b, weight):
        self.__adj[a].append(WeightedEdge(a, b, weight))
        self.__adj[b].append(WeightedEdge(b, a, weight))

    def adj(self, a):
        return self.__adj[a]

    def num_nodes(self):
        return len(self.__adj) - 1


class Dijkstra:
    """Single root shortest path by Dijkstra."""

    def __init__(self, graph, s, t):
        self.dist_to = [float('inf') for _ in range(graph.num_nodes() + 1)]
        self.dist_to[s] = 0
        pq = []
        heappush(pq, (0, s))
        while pq:
            w, a = heappop(pq)
            if a == t:
                return
            if self.dist_to[a] < w:
                continue
            for edge in graph.adj(a):
                if self.dist_to[edge.b] > self.dist_to[a] + edge.weight:
                    self.dist_to[edge.b] = self.dist_to[a] + edge.weight
                    heappush(pq, (self.dist_to[edge.b], edge.b))


if __name__ == '__main__':
    while True:
        n, m = map(int, input().split())
        if n == 0 and m == 0:
            break

        g_cost = EdgeWeightedGraph(m)
        g_time = EdgeWeightedGraph(m)
        for _ in range(n):
            a, b, cost, time = map(int, input().split())
            g_cost.add_edge(a, b, cost)
            g_time.add_edge(a, b, time)

        k = int(input())
        for _ in range(k):
            p, q, r = map(int, input().split())
            if r == 0:
                fw_cost = Dijkstra(g_cost, p, q)
                ans = fw_cost.dist_to[q]
            else:
                fw_time = Dijkstra(g_time, p, q)
                ans = fw_time.dist_to[q]
            print(ans)

Python: max_ = 1000000
#def solv(p,q):
    #global costs
    #global times
    #global stack
    #if times[p][q] < max_:
        ##最短経路算出済み
        #return costs[p][q],times[p][q]
#
    #if p == q:
        #return 0,0
#
    #min_cost = max_
    #min_time = max_
    #stack.append(p)
#
    #for s in senro :
        #if (s[0] == p or s[1] == p) :
            #next_ = s[1] if s[0]==p else s[0]
            #if next_ in stack: #経過済みは無視
                #continue
#
            #c,t = solv(next_,q)
            #min_cost = min(c + s[2],min_cost)
            #min_time = min(t + s[3],min_time)
    #
    #costs[p][q] = min_cost
    #times[p][q] = min_time
#
    #stack.pop()
#
    #return min_cost,min_time

while True:
    n,m = list(map(int,input().split()))
    if n == 0 and m == 0:
        break

    costs = [[max_ for i in range(m+1)] for j in range(m+1)]
    times = [[max_ for i in range(m+1)] for j in range(m+1)]

    #senro = [list(map(int,input().split())) for i in range(n)]
    for i in range(n):
        s = list(map(int,input().split()))
        costs[s[0]][s[1]] = s[2]
        costs[s[1]][s[0]] = s[2]
        times[s[0]][s[1]] = s[3]
        times[s[1]][s[0]] = s[3]

    for i in range(1,m+1):
        for k in range(1,m+1):
            for j in range(1,m+1):
                costs[i][j] = min(costs[i][j],costs[i][k] + costs[k][j])
                times[i][j] = min(times[i][j],times[i][k] + times[k][j])

    k = int(input())
    for i in range(k):
        p,q,r = list(map(int,input().split()))
        print(times[p][q] if r==1 else costs[p][q])