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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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])
			}
		}
	}
}

Python: cost_ans = []
time_ans = []

def set_ans(cost , time):
    cost_ans.append(cost)
    time_ans.append(time)

def reset_ans():
    global cost_ans
    global time_ans
    cost_ans = []
    time_ans = []

class Sta(object):
    """station"""
    def __init__(self, station_name):
        super(Sta, self).__init__()
        self.station_name = station_name
        self.route = []

    def add_route(self, route):
        self.route.append(route)



class Route(object):
    """route"""
    def __init__(self, cost, time, next_station):
        super(Route, self).__init__()
        self.time = time
        self.cost = cost
        self.next_station = next_station

def search_route(now_station, gool_station, cost, time):
    """search route"""

    if now_station.station_name == gool_station.station_name:
        set_ans(cost, time)
        return

    elif len(now_station.route) == 0:
        return

    for r in now_station.route:
        search_route(
                r.next_station,
                gool_station,
                cost + r.cost,
                time + r.time)
def main():

    while True:
        reset_ans([],[])
        n,m = map(int, raw_input().split())
        if n == 0 and m == 0:
            return

        stations = [Sta(x) for x in xrange(1,m+1) ]

        for i in xrange(n):
            a, b, cost, time = map(int, raw_input().split())
            tmp = Route(cost, time, stations[b-1])
            stations[a-1].add_route(tmp)

        k = input()
        for i in xrange(k):
            p, q, r = map(int, raw_input().split())
            search_route(stations[p-1], stations[q-1], 0, 0)
            if r == 0:
                print(min(cost_ans))
            else:
                print(min(time_ans))



if __name__ == '__main__':
    main()

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0200

"""
import sys
from sys import stdin
input = stdin.readline


def warshallFloyd(V, dp):
    for k in range(1, V):
        for i in range(1, V):
            for j in range(i, V):
                if dp[i][k] + dp[k][j] < dp[i][j]:
                    dp[i][j] = dp[i][k] + dp[k][j]


def main(args):
    while True:
        n, m = map(int, input().split())
        if n == 0 and m == 0:
            break
        dp_time = [[float('inf')] * (m+1) for _ in range(m+1)]
        dp_cost = [[float('inf')] * (m+1) for _ in range(m+1)]
        for i in range(m+1):
            dp_time[i][i] = 0
            dp_cost[i][i] = 0

        for _ in range(n):
            a, b, cost, time = map(int, input().split())
            dp_time[a][b] = time
            dp_time[b][a] = time
            dp_cost[a][b] = cost
            dp_cost[b][a] = cost

        warshallFloyd(m+1, dp_time)
        warshallFloyd(m+1, dp_cost)

        k = int(input())
        for _ in range(k):
            p, q, r = map(int, input().split())
            if r == 0:
                print(dp_cost[p][q])
            else:
                print(dp_time[p][q])



if __name__ == '__main__':
    main(sys.argv[1:])

Python: import heapq
import sys
from collections import defaultdict

def dijkstra(graph, size, start):
    d = [float('inf')] * size
    d[start] = 0
    q = [(0, start)]
    while len(q):
        du, u = heapq.heappop(q)
        for length, v in graph[u]:
            if d[v] > du + length:
                d[v] = du + length
                heapq.heappush(q, (d[v], v))
    return d

f = sys.stdin

while True:
    n, m = map(int, f.readline().split())
    if n == 0:
        break
    abct = [map(int, f.readline().split()) for _ in range(n)]
    d = [defaultdict(list), defaultdict(list)]
    
    for ai, bi, ci, ti in abct:
        ai -= 1
        bi -= 1
        d[0][ai].append((ci, bi))
        d[0][bi].append((ci, ai))
        d[1][ai].append((ti, bi))
        d[1][bi].append((ti, ai))
    
    dist = {}
    for i in range(int(f.readline())):
        p, q, r = map(int, f.readline().split())
        p -= 1
        q -= 1
        try:
            print(dist[(q,r)][p])
        except KeyError:
            try:
                print(dist[(p,r)][q])
            except:
                dist[(p,r)] = dijkstra(d[r], m, p)
                print(dist[(p,r)][q])