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

Python: import queue
 
def Dijkstra_mode(vertex,edges,start,end,mode):
    weight = [0 if i==start else float("inf") for i in range(vertex)]
    Q = queue.PriorityQueue()
    Q.put((weight[start],start))
 
    while(not Q.empty()):
        cur = Q.get()
        if weight[cur[1]] < cur[0]:
            continue
        if mode == 0:
            for j in range(len(edges[cur[1]])):
                if weight[edges[cur[1]][j][1]] > weight[cur[1]] + edges[cur[1]][j][2]:
                    weight[edges[cur[1]][j][1]] = weight[cur[1]] + edges[cur[1]][j][2]
                    Q.put((weight[edges[cur[1]][j][1]],edges[cur[1]][j][1]))
        else:
            for j in range(len(edges[cur[1]])):
                if weight[edges[cur[1]][j][1]] > weight[cur[1]] + edges[cur[1]][j][3]:
                    weight[edges[cur[1]][j][1]] = weight[cur[1]] + edges[cur[1]][j][3]
                    Q.put((weight[edges[cur[1]][j][1]],edges[cur[1]][j][1]))
        if weight[end] != float("inf"):
            return weight[end]
    return weight

n,m = map(int,input().split())
edges = [[] for i in range(m)]
for i in range(n):
    data = list(map(input().split()))
    data_b = [data[1],data[0],data[2],data[3]]
    edges[data[0]].append(data)
    edges[data_b[0]].append(data_b)
k = int(input())
for i in range(k):
    p,q,r = map(int,input().split())
    ans = Dijkstra_mode(m,edges,p,q,r)
    print(ans)

Python: import copy as c
CEIL = 1000000
lines=[]

def exploreMin(now, goal, mode, history, ans):
    #mode=0: MinCost;mode=1: MinTime
    #print(lines)
    if now == goal:
        #print('??\????????±??????')
        return ans
    for station in history:
        if station == now:
            return CEIL
    minCostOrTime = CEIL
    for destination in lines[now]:
        exploreAns = exploreMin(destination[0], goal, mode, c.deepcopy(history)+[now],
                                ans+destination[mode+1])
        if minCostOrTime > exploreAns: minCostOrTime = exploreAns
    return minCostOrTime
'''
def findMin(start, goal, mode):
    minAnswer=100000
    if minAnswer > xplore(start, goal, mode, []):

    return minAnswer
'''

def main():
    input1 = input().split(' ')
    global lines
    lines=[[] for i in range(int(input1[1]))]
    for lineIndex in range(int(input1[0])):
        input2 = input().split(' ')
        lines[int(input2[0])-1].append([int(input2[1])-1, int(input2[2]), int(input2[3])])
        lines[int(input2[1])-1].append([int(input2[0])-1, int(input2[2]), int(input2[3])])
    #print(lines[0][0][1])
    for out in range(int(input())):
        input3 = input().split(' ')
        #print([].append(1))
        print(exploreMin(int(input3[0])-1, int(input3[1])-1, int(input3[2]), [], 0))
    input()

if __name__ == '__main__': main()

Python: # coding: utf-8
class Route:
    def __init__(self, station_number, price, time):
        self.station_number = station_number
        self.price = price
        self.time = time

class Station:
    def __init__(self):
        self.routes = []
    def addRoute(self,route):
        self.routes.append(route)

class Path:
    def __init__(self, now_station, next_station, price, time):
        self.now_station = now_station
        self.next_station = next_station
        self.price = price
        self.time = time

def search_path(stations, come_from, go_to, search_type):
    result = [999]
    q = []
    for distination in stations[come_from].routes:
        new_path = Path(come_from, distination.station_number, distination.price, distination.time)
        q.append(new_path)
#   while len(q) > 0:
#       path = q.pop(0)
#       if path.next_station == go_to:
#           r = path.price if search_type == 0 else path.time
#           result.append(r)
#       for distination in stations[path.next_station].routes:
#           update_price = path.price + distination.price
#           update_time = path.time + distination.time
#           next_path = Path(path.next_station, distination.station_number, update_price, update_time)
#           q.append(next_path)
    print(sorted(result)[0])

route_count, station_count = map(int, input().split(" "))
stations = [Station() for i in range(station_count)]
for read_line in range(route_count):
    come_from, go_to, price, time = map(int, input().split(" "))
    route = Route(go_to - 1, price, time)
    stations[come_from - 1].addRoute(route)
search_count = int(input())
for read_line in range(search_count):
    come_from, go_to, search_type = map(int, input().split())
    search_path(stations, come_from - 1, go_to - 1, search_type)