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: """ Created by Jieyi on 7/28/16. """
import copy
import io
import sys

if len(sys.argv) > 1:
    filename = sys.argv[1]
    inp = ''.join(open(filename, "r").readlines())
    sys.stdin = io.StringIO(inp)


def floyd_warshall(path_map):
    """
    This is following to Floyd???Warshall algorithm. All path the shortest path.
    """

    for k in range(len(path_map)):
        for i in range(len(path_map)):
            for j in range(len(path_map)):
                if path_map[i][j] > path_map[i][k] + path_map[k][j]:
                    path_map[i][j] = path_map[i][k] + path_map[k][j]

    return path_map


def algorithm():
    while True:
        n, s = map(int, input().split())
        if n == 0 and s == 0:
            break

        # Create two maps.
        g_money = [[sys.maxsize for _ in range(s)] for _ in range(s)]
        g_time = copy.deepcopy(g_money)

        # Set two maps.
        for _ in range(n):
            x, y, m, t = map(int, input().split())
            x, y = x - 1, y - 1
            g_money[x][y] = m
            g_time[x][y] = t

        g_money = floyd_warshall(g_money)
        g_time = floyd_warshall(g_time)

        # Calculate the shortest path.
        for n in range(int(input())):
            start, end, category = list(map(int, input().split()))
            ans = {0: g_money[start - 1][end - 1], 1: g_time[start - 1][end - 1]}.get(category)
            print(ans)


def main():
    algorithm()


if __name__ == '__main__':
    main()

Python: # coding: utf-8
class Route:
    def __init__(self, next_station, need_price, need_time):
        self.next_station = next_station
        self.need_price = need_price
        self.need_time = need_time
 
class Station:
    def __init__(self):
        self.routes = []
    def addRoute(self,route):
        self.routes.append(route)
 
class Status:
    def __init__(self, now_station, use_price, use_time):
        self.now_station = now_station
        self.use_path = [now_station]
        self.use_price = use_price
        self.use_time = use_time
    def already_path(self,station):
        return station in self.use_path
 
def search(stations, start, end, search_type):
    result = [9999999999]
    queue = []
    for route in stations[start].routes:
        queue.append(Status(route.next_station, route.need_price, route.need_time))
    while len(queue) > 0:
        status = queue.pop(0)
        if status.now_station == end:
            r = status.use_price if search_type == 0 else status.use_time
            result.append(r)
        for route in stations[status.now_station].routes:
            if status.already_path(route.next_station):
                continue
            status.use_price = route.need_price + status.use_price
            status.use_time = route.need_time + status.use_time
            status.now_station = route.next_station
            status.use_path.append(route.next_station)
            queue.append(status)
    print(sorted(result)[0])
 
while True:
    read_line = input()
    if read_line == "0 0":
        break
    route_count, station_count = map(int, read_line.split(" "))
    stations = [Station() for station in range(station_count)]
    for count in range(route_count):
        now_station, next_station, need_price, need_time = map(int, input().split(" "))
        stations[now_station - 1].addRoute(Route(next_station - 1, need_price, need_time))
    search_count = int(input())
    for count in range(search_count):
        start, end, search_type = map(int, input().split(" "))
        search(stations, start - 1, end - 1, search_type)

Python: import sys
from copy import deepcopy


def dijkstra(path_map, start=0):
    arr_shortest_path = path_map[start]
    stack = []

    # Until all vertices put into stack.
    while 1:
        if len(stack) is len(arr_shortest_path):
            break

        # Pick the smallest value from the arr_shortest_path is not including vertices in the stack.
        tmp_arr = arr_shortest_path[:]
        for i in range(len(stack)):
            tmp_arr[stack[i]] = sys.maxsize
        mini_vertex = arr_shortest_path.index(min(tmp_arr))
        stack.append(mini_vertex)

        # Calculate the smallest value's vertex with all vertices which it could go.
        for i in range(len(arr_shortest_path)):
            if arr_shortest_path[i] > arr_shortest_path[mini_vertex] + path_map[mini_vertex][i]:
                arr_shortest_path[i] = arr_shortest_path[mini_vertex] + path_map[mini_vertex][i]

    return arr_shortest_path


def algorithm():
    n, s = map(int, input().split(' '))
    g_money = [[sys.maxsize for _ in range(s)] for _ in range(s)]
    for c in range(s):
        g_money[c][c] = 0
    g_time = deepcopy(g_money)

    for _ in range(n):
        x, y, money, time = map(int, input().split(' '))
        x -= 1
        y -= 1
        g_money[x][y] = money
        g_time[x][y] = time

    for n in range(int(input())):
        inp = list(map(int, input().split(' ')))
        if len(inp) == 2:
            break
        else:
            start, end, category = inp
        m = {0: g_money, 1: g_time}.get(category)
        ans = dijkstra(m, start - 1)
        print(ans[end - 1])


def main():
    algorithm()


if __name__ == '__main__':
    main()