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]
					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+1)]
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: from heapq import heappush, heappop
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
    M, N = map(int, readline().split())
    if M == N == 0:
        return False
    G = [[] for i in range(N)]
    INF = 10**18
    def solve(s, t, u):
        dist = [INF]*N
        dist[s] = 0
        que = [(0, s)]
        while que:
            cost, v = heappop(que)
            if dist[v] < cost:
                continue
            for e in G[v]:
                w = e[0]
                if cost + e[u] < dist[w]:
                    dist[w] = d = cost + e[u]
                    heappush(que, (d, w))
        return dist[t]
    for i in range(M):
        a, b, c, t = map(int, readline().split()); a -= 1; b -= 1
        G[a].append((b, c, t))
        G[b].append((a, c, t))
    K = int(readline())
    for i in range(K):
        p, q, r = map(int, readline().split())
        write("%d\n" % solve(p-1, q-1, r+1))
    return True
while solve():
    ...

Python: from heapq import heappop as hpop
from heapq import heappush as hpush

def main():
  
  while True:
    n, m = map(int, input().split())
    
    if not n:
      break
    
    cost_edges = [[] for _ in range(m)]
    time_edges = [[] for _ in range(m)]
  
    for _ in range(n):
      a, b, c, t = map(int, input().split())
      cost_edges[a - 1].append((c, b - 1))
      cost_edges[b - 1].append((c, a - 1))
      time_edges[a - 1].append((t, b - 1))
      time_edges[b - 1].append((t, a - 1))
    
    k = int(input())
  
    for _ in range(k):
      start, goal, r = map(int ,input().split())
      start -= 1
      goal -= 1
      
      if r:
        edges = time_edges
      else:
        edges = cost_edges
  
      que = [(0, start)]
      visited = [False for _ in range(m)]
      
      while True:
        score, station = hpop(que)
        visited[station] = True
        
        if station == goal:
          print(score)
          break
  
        for c, to_station in edges[station]:
          if not visited[to_station]:
            hpush(que, (score + c, to_station))

main()

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 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():
    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

        # Calculate the shortest path.
        for n in range(int(input())):
            start, end, category = list(map(int, input().split()))
            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()