Problem:


Score : 500 points

Problem Statement

There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.

Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.

Process the following Q queries:

The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.

Constraints

All values in input are integers.

2 \leq N \leq 300

0 \leq M \leq \frac{N(N-1)}{2}

1 \leq L \leq 10^9

1 \leq A_i, B_i \leq N

A_i \neq B_i

\left(A_i, B_i\right) \neq \left(A_j, B_j\right) (if i \neq j)

\left(A_i, B_i\right) \neq \left(B_j, A_j\right) (if i \neq j)

1 \leq C_i \leq 10^9

1 \leq Q \leq N\left(N-1\right)

1 \leq s_i, t_i \leq N

s_i \neq t_i

\left(s_i, t_i\right) \neq \left(s_j, t_j\right) (if i \neq j)

Input

Input is given from Standard Input in the following format:

N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

Output

Print Q lines.

The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.

Sample Input 1

3 2 5
1 2 3
2 3 3
2
3 2
1 3

Sample Output 1

0
1

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.

To travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.

Sample Input 2

4 0 1
1
2 1

Sample Output 2

-1

There may be no road at all.

Sample Input 3

5 4 4
1 2 2
2 3 2
3 4 3
4 5 2
20
2 1
3 1
4 1
5 1
1 2
3 2
4 2
5 2
1 3
2 3
4 3
5 3
1 4
2 4
3 4
5 4
1 5
2 5
3 5
4 5

Sample Output 3

0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0



Sample input:
3 2 5
1 2 3
2 3 3
2
3 2
1 3


Sample output:
0
1

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**5)

N,M,L=map(int,input().split())

G=[[] for i in range(N)]
for i in range(M):
	a,b,c=map(int,input().split())
	a-=1
	b-=1
	G[a].append([b,c])
	G[b].append([a,c])

Q=int(input())
st = []
for i in range(Q):
	s,t=map(lambda x:int(x)-1,input().split())
	st.append([s,t])


T = [[-1]*N for i in range(N)]
seen=[0]*N

for i in range(N):
	T[i][i]=0



def dfs(p,x):
	seen[x]=1
	for i,c in G[x]:
		if i!=p:
			dfs(x,i)
	
	for i,c in G[x]:
		for j in range(N):
			if T[i][j]!=-1:
				if T[x][j]==-1:
					T[x][j]=T[i][j]+c
					T[j][x]=T[i][j]+c
				else:
					T[x][j] = min(T[i][j]+c,T[x][j])
					T[j][x] = min(T[i][j]+c,T[x][j])


dfs(-1,0)


for i in range(Q):
	u = T[st[i][0]][st[i][1]]
	print(-1 if u<0 else int((u-0.1)//L))

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np
from scipy.sparse.csgraph import dijkstra
from scipy.sparse.csgraph import floyd_warshall

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M, L = list(map(int, sys.stdin.readline().split()))
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
Q = int(sys.stdin.readline())
S, T = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))

D = np.full((N, N), 0)
for a, b, c in ABC:
    D[a - 1][b - 1] = c
    D[b - 1][a - 1] = c

floyd_warshall(D, directed=False, overwrite=True)
counts = dijkstra(D <= L, directed=False, unweighted=True)
S = np.array(S, dtype=int) - 1
T = np.array(T, dtype=int) - 1

ans = counts[S, T] - 1
ans[ans == np.inf] = -1
print(*ans.astype(int), sep='\n')

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np
from scipy.sparse.csgraph import dijkstra
from scipy.sparse.csgraph import floyd_warshall

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M, L = list(map(int, sys.stdin.readline().split()))
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
Q = int(sys.stdin.readline())
S, T = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))

D = np.full((N, N), IINF)
for a, b, c in ABC:
    D[a - 1][b - 1] = c
    D[b - 1][a - 1] = c

floyd_warshall(D, directed=False, overwrite=True)
counts = dijkstra(D <= L, directed=False, unweighted=True)
S = np.array(S, dtype=int) - 1
T = np.array(T, dtype=int) - 1

ans = counts[S, T] - 1
ans[ans == np.inf] = -1
print(*ans.astype(int), sep='\n')

Submission status: Runtime Error

Solution code:
N,M,L=map(int,input().split())

G=[[] for i in range(N)]
for i in range(M):
	a,b,c=map(int,input().split())
	a-=1
	b-=1
	G[a].append([b,c])
	G[b].append([a,c])

Q=int(input())
st = []
for i in range(Q):
	s,t=map(lambda x:int(x)-1,input().split())
	st.append([s,t])


T = [[-1]*N for i in range(N)]
seen=[0]*N

for i in range(N):
	T[i][i]=0



def dfs(p,x):
	seen[x]=1
	for i,c in G[x]:
		if i!=p:
			dfs(x,i)
	
	for i,c in G[x]:
		for j in range(N):
			if T[i][j]!=-1:
				if T[x][j]==-1:
					T[x][j]=T[i][j]+c
					T[j][x]=T[i][j]+c
				else:
					T[x][j] = min(T[i][j]+c,T[x][j])
					T[j][x] = min(T[i][j]+c,T[x][j])


dfs(-1,0)


for i in range(Q):
	u = T[st[i][0]][st[i][1]]
	print(-1 if u<0 else int((u-0.1)//L))

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np
from scipy.sparse.csgraph import dijkstra

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M, L = list(map(int, sys.stdin.readline().split()))
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
Q = int(sys.stdin.readline())
S, T = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))


D = np.full((N, N), IINF)
for a, b, c in ABC:
    D[a - 1][b - 1] = c
    D[b - 1][a - 1] = c

# warshall floyd
for via in range(N):
    D = np.minimum(D, sum(np.meshgrid(D[via], D[via], sparse=True)))

counts = dijkstra(D <= L, directed=False)
S = np.array(S, dtype=int) - 1
T = np.array(T, dtype=int) - 1

ans = counts[S, T] - 1
ans[ans == np.inf] = -1
print(*ans.astype(int), sep='\n')

Submission status: Runtime Error

Solution code:
nm = lambda : map(int,input().split())
nl = lambda : list(nm())
import heapq as h

n,m,l = nm()
g = [list() for _ in range(n+1)]
ans = [0]
for _ in range(m):
    a,b,c = nm()
    g[a].append((b,c))
    g[b].append((a,c))
q = int(input())

def move(tup,cost):
    if tup[1] + cost > 0:
        if l < cost:
            return -1
        return (tup[0]+1,cost-l)
    return (tup[0],tup[1]+cost)

for st in range(1,n+1):
    dist = [-1]*(n+1)
    que = [((0,-l),st)]
    while que:
        tup,v = h.heappop(que)
        if dist[v] != -1 and dist[v] < tup:
            continue
        dist[v] = tup
        for x,c in g[v]:
            new = move(tup,c)
            if dist[x] == -1 or dist[x] > new:
                h.heappush(que,(new,x))
    ans.append(dist)

for _ in range(q):
    s,t = nm()
    if ans[s][t] == -1:
        print(-1)
    else:
        print(ans[s][t][0])