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: def warshall_floyd(d):
    for k in range(len(d)):
        for i in range(len(d)):
            for j in range(len(d)):
                if d[i][j] > d[i][k] + d[k][j]:
                    d[i][j] = d[i][k] + d[k][j]

import sys
f = sys.stdin

while True:
    n, m = map(int, f.readline().split())
    if n == 0:
        break
    abct = [list(map(int, f.readline().split())) for _ in range(n)]
    
    d = [[[float('inf')] * m for i in range(m)], [[float('inf')] * m for i in range(m)]]
    
    for ai, bi, ci, ti in abct:
        d[0][ai - 1][bi - 1] = d[0][bi - 1][ai - 1] = ci
        d[1][ai - 1][bi - 1] = d[1][bi - 1][ai - 1] = ti

    warshall_floyd(d[0])
    warshall_floyd(d[1])
        
    for i in range(int(f.readline())):
        p, q, r = map(int, f.readline().split())
        print(d[r][p - 1][q - 1])

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: # AOJ 0200 Traveling Alone: One-way Ticket of Youth
# Python3 2018.6.24 bal4u
# ワーシャルフロイド法

INF = 0x7fffffff

while 1:
	n, m = map(int, input().split())
	if n == 0: break
	tim = [[INF for j in range(m)] for i in range(m)]
	fee = [[INF for j in range(m)] for i in range(m)]
	for i in range(m): tim[i][i] = fee[i][i] = 0;
	for i in range(n):
		a, b, c, t = map(int, input().split())
		a -= 1
		b -= 1
		if fee[a][b] > c: fee[a][b] = fee[b][a] = c
		if tim[a][b] > t: tim[a][b] = tim[b][a] = t
	for k in range(m):
		for i in range(m):
			for j in range(m):
				if fee[i][k]+fee[k][j] < fee[i][j]: fee[i][j] = fee[i][k]+fee[k][j]
				if tim[i][k]+tim[k][j] < tim[i][j]: tim[i][j] = tim[i][k]+tim[k][j]
	k = int(input())
	for i in range(k):
		p, q, r = map(int, input().split())
		if (r): print(tim[p-1][q-1])
		else:   print(fee[p-1][q-1])

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(1, V):
                if dp[i][k][0] + dp[k][j][0] < dp[i][j][0]:
                    dp[i][j][0] = dp[i][k][0] + dp[k][j][0]
                if dp[i][k][1] + dp[k][j][1] < dp[i][j][1]:
                    dp[i][j][1] = dp[i][k][1] + dp[k][j][1]


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

        warshallFloyd(m+1, dp)

        k = int(input())
        for _ in range(k):
            p, q, r = map(int, input().split())
            if r == 0:          #  cost
                print(dp[p][q][1])
            else:               #  time
                print(dp[p][q][0])



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