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: Wrong Answer

Solution code:
from scipy.sparse.csgraph import floyd_warshall as f
from scipy.sparse import csr_matrix
import numpy as np
n,m,l,*L=map(int,open(0).read().split())
abc=L[:3*m]
st=L[3*m+1:]
a=abc[::3]
b=abc[1::3]
c=abc[2::3]
s=st[::2]
t=st[1::2]
fw=f(csr_matrix((c,(a,b)),(n+1,n+1)),0)
fw2=f(np.where(fw<=l,1,fw),0)-1
res=np.where(fw2 < np.inf, fw2, -1)
ans=res[s,t].astype(int)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys


def main():
	input = sys.stdin.readline
	N, M, L = list(map(int,input().split()))
	P = [list(map(int,input().split())) for i in range(M)]
	Q = int(input())
	st = [list(map(int,input().split())) for i in range(Q)]

	neighbor = [[] for i in range(N)]
	for s,e,d in P:
		if d<=L:
			neighbor[s-1].append((e-1,d))
			neighbor[e-1].append((s-1,d))

	fuellist = [0]*N
	def bfs(start, end):
		# fuel = [[10000000,L+1] for i in range(N)]
		fuel = [100000*(10**10)+L+1]*N
		# fuel[start][0] = 0
		# fuel[start][1] = L
		fuel[start] = L
		queue = [start]
		while len(queue)>0:
			nq = []						# next target node
			nq_append = nq.append
			for i in queue:				# target node
				# oc, om = fuel[i]
				oc = fuel[i]//(10**10)
				om = fuel[i]%(10**10)
				for j, d in neighbor[i]:	# next node
					if om<d:
						nc = oc+1
						nm = L-d
					else:
						nc = oc
						nm = om-d

					# print(i,j,nc,nm)

					nf = nc*(10**10)+nm
					# if (nc<fuel[j][0]) or (nc==fuel[j][0] and nm>fuel[j][1]):
					if nf<fuel[j]:
						# fuel[j][0], fuel[j][1] = nc, nm
						fuel[j] = nf
						nq_append(j)

			queue = nq					# update target node

		# ans = fuel[end][0]
		ans = fuel[end]//(10**10)
		# if fuel[end][0]==10000000: ans = -1
		if ans==100000: ans = -1

		fuellist[start] = fuel

		print(ans)

	for s, t in st:
		start, end = s-1, t-1
		if fuellist[start]==0:
			bfs(start,end)
		else:
			# ans = fuellist[start][end][0]
			ans = fuellist[start][end]//(10**10)
			if ans==100000: ans = -1
			print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
n, m, full = map(int, input().split())
edges = []

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


def warshall_floyd(num_of_node, edge):
    distance = [[float('INF')]*num_of_node for _ in range(num_of_node)]
    for i in range(num_of_node):
        distance[i][i] = 0

    for i, j, d in edge:
        distance[i][j], distance[j][i] = d, d

    for i in range(num_of_node):
        for j in range(num_of_node):
            for k in range(num_of_node):
                distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j])

    return distance


min_routes = warshall_floyd(n, edges)
L_edges = []
for i in range(n-1):
    for j in range(i+1, n):
        if min_routes[i][j] <= full:
            L_edges.append([i, j, 1])
            L_edges.append([j, i, 1])

answers = warshall_floyd(n, L_edges)

Q = int(input())
for i in range(Q):
    s, t = map(int, input().split())
    print(answers[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

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

n, w = N, M
d = [[float("inf") for i in range(n)] for i in range(n)] 

#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(w):
    x,y,z = map(int,input().split())
    d[x-1][y-1] = z
    d[y-1][x-1] = z
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０
D = warshall_floyd(d)

for i in range(n):
    for j in range(n):
        if i != j and D[i][j] <= L:
            d[i][j] = 1

D = warshall_floyd(d)

Q = int(input())
for i in range(Q):
    s,t = tuple(map(int, input().split()))
    if D[s-1][t-1]-1 == float("inf"):
      print(-1)
    else:
      print(D[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

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

n, w = N, M
d = [[float("inf") for i in range(n)] for i in range(n)] 

#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(w):
    x,y,z = map(int,input().split())
    d[x-1][y-1] = z
    d[y-1][x-1] = z
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０
D = warshall_floyd(d)

for i in range(n):
    for j in range(n):
        if i != j and D[i][j] <= L:
            d[i][j] = 1

D = warshall_floyd(d)

Q = int(input())
for i in range(Q):
    s,t = tuple(map(int, input().split()))
    print(D[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
n,m,l = list(map(lambda x:int(x),input().split()))
d = [[float('inf') for i in range(n)] for j in range(n)]
for i in range(n):
    d[i][i] = 0
for i in range(m):
    a,b,c = list(map(lambda x:int(x),input().split()))
    if c <= l:
        d[a-1][b-1] = c
        d[b-1][a-1] = c
for i in range(n):
    for j in range(n):
        for k in range(n):
            d[i][j] = min(d[i][j],d[i][k]+d[k][j])

dd = [[float('inf') for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            dd[i][j] = 1
for i in range(n):
    dd[i][i] = 0
for i in range(n):
    for j in range(n):
        for k in range(n):
            dd[i][j] = min(dd[i][j],dd[i][k]+dd[k][j])
q = int(input())
for i in range(q):
    s,t = input().split()
    s = int(s)-1
    t = int(t)-1
    if dd[s][t] == float('inf'):
        print(-1)
    else:
        print(dd[s][t]-1)

Submission status: Wrong Answer

Solution code:
from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = np.zeros((N, N))
for _ in range(M):
    s, t, w = map(int, input().split())
    graph[s-1][t-1] = graph[t-1][s-1] = w
floyd_warshall(graph, directed=False, overwrite=True)
graph = np.where((graph == 0) | (graph > L), 0, 1)
floyd_warshall(graph, directed=False, overwrite=True)

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = graph[s-1][t-1]
    if ans == float("inf"):
        print(-1)
    else:
        print(int(ans - 1))

Submission status: Wrong Answer

Solution code:
INF = 10000000000
import math
def main():
    N, M, L = map(int, input().split())

    d = [[INF for i in range(N)] for j in range(N)]
    ans_d = [[INF for i in range(N)] for j in range(N)]
    for i in range(N):
        d[i][i] = 0
        ans_d[i][i] = 0

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

    for k in range(N):
        for i in range(N):
            for j in range(N):
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j])

    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            if d[i][j] <= L:
                ans_d[i][j] = 1

    for k in range(N):
        for i in range(N):
            for j in range(N):
                    ans_d[i][j] = min(ans_d[i][j], ans_d[i][k] + ans_d[k][j])

    Q = int(input())
    for i in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        if d[s][t] == INF:
            print(-1)
        else:
            print(ans_d[s][t] - 1)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from itertools import product, combinations

INF = float('inf')
def floyd_warshall(V, E):
    '''
    Input:
        V = list(range(N))
        E = defaultdict(dict)
    Output:
        If there exists a negative cycle, return (None, prev), where prev is
        the table, whose (i, j)-element is the previous vertices in the shortest (i, j)-path
        Otherwise return (dist, prev), where dist is the table of the minimum cost
        between arbitrary vertex u, v in the given graph
    '''
    dist = {u: {v: INF for v in V} for u in V}
    prev = {u: {v: None for v in V} for u in V}
    for v in V:
        dist[v][v] = 0; prev[v][v] = -1
        for u, c in E[v].items(): dist[v][u] = c; prev[v][u] = v;
    for k in V:        
        for i, j in product(V, repeat=2):
            if dist[i][j] > dist[i][k] + dist[k][j]:
                dist[i][j] = dist[i][k] + dist[k][j]
                prev[i][j] = prev[k][j]
    for v in V:
        if dist[v][v] < 0: return None, prev
    return dist, prev

N, M, L = map(int, input().split())
V = list(range(N))
E = defaultdict(dict)
for _ in range(M):
    a, b, c = map(int, input().split())
    E[a-1][b-1] = c
    E[b-1][a-1] = c

dist, _ = floyd_warshall(V, E)
E_charge = defaultdict(dict)
for u, v in combinations(V, 2):
    if dist[u][v] <= L: E_charge[u][v] = c; E_charge[v][u] = c;
charge, _ = floyd_warshall(V, E_charge)

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    n = charge[s-1][t-1]
    print(n if n != INF else -1)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,M,L=map(int,input().split())
h=[None]*N
for i in range(N):
    h[i]=[None]*N
    for j in range(N):
        h[i][j]=[10**12,[i]]
for i in range(N):
    h[i][i]=[0,[]]
for i in range(M):
    A,B,C=map(int,input().split())
    h[A-1][B-1][0]=C
    h[A-1][B-1][1].append(B-1)
    h[B-1][A-1][0]=C
    h[B-1][A-1][1].append(A-1)
for i in range(N):
    for j in range(N):
        for k in range(N):
            if h[j][i][0]+h[i][k][0]<h[j][k][0]:
                h[j][k][0]=h[j][i][0]+h[i][k][0]
                h[j][k][1]=h[j][i][1]+h[i][k][1]
Q=int(input())
for _ in range(Q):
    l=L
    a=0
    s,t=map(int,input().split())
    if len(h[s-1][t-1][1])!=1:
      for i in range(0,len(h[s-1][t-1][1]),2):
        f=h[h[s-1][t-1][1][i]][h[s-1][t-1][1][i+1]][0]
        if l>f:
            l-=f
        elif f>L:
            print(-1)
            break
        else:
            a+=1
            l=L-f
      else:
        print(a)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,m,l=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(m)]
q=int(input())
st=[list(map(int,input().split())) for _ in range(q)]


d = [[float("inf") for i in range(n)] for i in range(n)]

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

for i in range(m):
    a,b,c=abc[i]
    d[a-1][b-1]=c
    d[b-1][a-1]=c

warshall_floyd(d)

dd=[[float("inf") for _ in range(n)] for _ in range(n)]

for i in range(n):
    for j in range(n):
        if i==j:
            dd[i][j]=0
        if d[i][j]<=l:
            dd[i][j]=1
            dd[j][i]=1

warshall_floyd(dd)

for i in range(q):
    s,t=st[i]
    s-=1
    t-=1
    print(dd[s][t]-1)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,M,L=map(int,input().split())
h=[None]*N
for i in range(N):
    h[i]=[None]*N
    for j in range(N):
        h[i][j]=[10**12,[i]]
for i in range(N):
    h[i][i]=[0,[]]
for i in range(M):
    A,B,C=map(int,input().split())
    h[A-1][B-1][0]=C
    h[A-1][B-1][1].append(B-1)
    h[B-1][A-1][0]=C
    h[B-1][A-1][1].append(A-1)
for i in range(N):
    for j in range(N):
        for k in range(N):
            if h[j][i][0]+h[i][k][0]<h[j][k][0]:
                h[j][k][0]=h[j][i][0]+h[i][k][0]
                h[j][k][1]=h[j][i][1]+h[i][k][1]
Q=int(input())
for _ in range(Q):
    l=L
    a=0
    s,t=map(int,input().split())
    if len(h[s-1][t-1][1])!=1:
      for i in range(0,len(h[s-1][t-1][1]),2):
        print(h[s-1][t-1][1])
        f=h[h[s-1][t-1][1][i]][h[s-1][t-1][1][i+1]][0]
        if l>f:
            l-=f
        elif f>L:
            print(-1)
            break
        else:
            a+=1
            l=L-f
      else:
        print(a)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from scipy.sparse.csgraph import floyd_warshall as fw
INF = float('inf')
N,M,L = map(int,input().split())
cost = [[INF]*N for _ in range(N)]
for i in range(M):
    a,b,c = map(int,input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

for i in range(N):
    for j in range(N):
        if cost[i][j] > L:
            cost[i][j] = INF
        else:
            cost[i][j] = 1

cost = fw(cost)

for i in range(N):
    for j in range(N):
        if cost[i][j] == INF:
            cost[i][j] = -1
        else:
            cost[i][j] -= 1

Q = int(input())

for i in range(Q):
    s,t = map(int,input().split())
    print(int(cost[s-1][t-1]))

Submission status: Wrong Answer

Solution code:
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def tami(start, now, cnt, fuel, visited):
  # ans更新
  if cnt < cost[start][now]:
    cost[start][now] = cnt

    # dfs
    for x,c in root[now]:
      if str(x) not in visited:
        if fuel - c < 0:
          cnt += 1
          fuel = L
        tami(start, x, cnt, fuel - c, visited + str(x))

N, M, L = IL()
abc = [IL() for i in range(M)]
Q = I()
q = [IL() for i in range(Q)]

root = [[] for i in range(N+1)]
cost = [[MAX_INT]*(N+1) for i in range(N+1)]
for a,b,c in abc:
  if c > L:
    continue
  root[a].append([b, c])
  root[b].append([a, c])
#print(root)

for i in range(1,N+1):
  visited = ""
  tami(i, i, 0, L, visited + str(i))
#print(cost)

for a,b in q:
  ans = cost[a][b]
  if ans == MAX_INT:
    print(-1)
  else:
    print(ans)

Submission status: Wrong Answer

Solution code:
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d
 
N, M, L = tuple(map(int, input().split()))
 
n, w = N, M
d = [[float("inf") for i in range(n)] for i in range(n)] 
 
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(w):
    x,y,z = map(int,input().split())
    d[x-1][y-1] = z
    d[y-1][x-1] = z
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０
D = warshall_floyd(d)
 
Q = int(input())
for _ in range(Q):
    s,t = tuple(map(int, input().split()))
    print((D[s-1][t-1]-1)//L)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, M, L = map(int, input().split())
roads = [[float('inf')]*N for _ in range(N)]
kyuyu = [[float('inf')]*N for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    if c <= L:  # 移動出来ない経路は消す
        roads[a-1][b-1] = c
        roads[b-1][a-1] = c

for k in range(N):
    for i in range(N):
        for j in range(N):
            roads[i][j] = min(roads[i][j], roads[i][k]+roads[k][j])

for i in range(N):
    for j in range(N):
        if roads[i][j] <= L:
            kyuyu[i][j] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            kyuyu[i][j] = min(kyuyu[i][j], kyuyu[i][k]+kyuyu[k][j])

Q = int(input())
for _ in range(Q):
    s, t = map(lambda x: int(x)-1, input().split())
    dist = kyuyu[s][t]
    if dist == float('inf'):
        print(-1)
    else:
        print(dist)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, M, L = map(int, input().split())
roads = [[float('inf')]*N for _ in range(N)]
kyuyu = [[float('inf')]*N for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    if c <= L:  # 移動出来ない経路は消す
        roads[a-1][b-1] = c
        roads[b-1][a-1] = c

for k in range(N):
    for i in range(N):
        for j in range(N):
            roads[i][j] = min(roads[i][j], roads[i][k]+roads[k][j])

for i in range(N):
    for j in range(N):
        if roads[i][j] <= L:
            kyuyu[i][j] = 1
            kyuyu[j][i] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            kyuyu[i][j] = min(kyuyu[i][j], kyuyu[i][k]+kyuyu[k][j])

Q = int(input())
for _ in range(Q):
    s, t = map(lambda x: int(x)-1, input().split())
    dist = kyuyu[s][t]
    if dist == float('inf'):
        print(-1)
    else:
        print(dist)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n,m,l = map(int,input().split())
dp = [[-1]*n for _ in range(n)]
for i in range(n):
    dp[i][i] = 0

for _ in range(m):
    a,b,c = map(int,input().split())
    dp[a-1][b-1] = c
    dp[b-1][a-1] = c

for k in range(n):
    for i in range(n):
        for j in range(n):
            if dp[i][k] != -1 and dp[k][j] != -1:
                if dp[i][j] == -1:
                    dp[i][j] = dp[i][k] + dp[k][j]
                else:
                    dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j])

dp2 = [[-1]*n for _ in range(n)]
for i in range(n):
    dp[i][i] = 0

for i in range(n):
    for j in range(n):
        if dp[i][j] <= l:
            dp2[i][j] = 1

for k in range(n):
    for i in range(n):
        for j in range(n):
            if dp2[i][k] != -1 and dp2[k][j] != -1:
                if dp2[i][j] == -1:
                    dp2[i][j] = dp2[i][k] + dp2[k][j]
                else:
                    dp2[i][j] = min(dp2[i][j],dp2[i][k]+dp2[k][j])

for _ in range(int(input())):
    s,t = map(int,input().split())
    print(dp2[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, M, L = map(int, input().split())
roads = [[float('inf')]*N for _ in range(N)]
kyuyu = [[float('inf')]*N for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    if c <= L:  # 移動出来ない経路は消す
        roads[a-1][b-1] = c
        roads[b-1][a-1] = c

for k in range(N):
    for i in range(N):
        for j in range(N):
            roads[i][j] = min(roads[i][j], roads[i][k]+roads[k][j])

for i in range(N):
    for j in range(i+1, N):
        if roads[i][j] <= L:
            kyuyu[i][j] = 1
            kyuyu[j][i] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            kyuyu[i][j] = min(kyuyu[i][j], kyuyu[i][k]+kyuyu[k][j])

Q = int(input())
for _ in range(Q):
    s, t = map(lambda x: int(x)-1, input().split())
    dist = kyuyu[s][t]
    if dist == float('inf'):
        print(-1)
    else:
        print(dist)

Submission status: Wrong Answer

Solution code:
#何とか法, AOJのgrl1c
#初め満杯
import sys
input = sys.stdin.readline
#print = sys.stdout.write

n,m,l =map(int, input().split( ))
Edge = [[] for _ in range(n)]

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

dp = [[10**10 for _ in range(n)] for __ in range(n)]

for i in range(n):
    for ed in Edge[i]:
        dp[i][ed[0]] = ed[1]

for i in range(n):
    dp[i][i] = 0

for k in range(n):
    for j in range(n):
        for i in range(n):
            dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j])

"""
以下が要点
"""
dp2 = [[10**8 for _ in range(n)] for __ in range(n)]
for j in range(n):
    for i in range(j):
        if dp[i][j] <=l:
            dp2[i][j] = 1
            dp2[j][i] = 1
for k in range(n):
    for j in range(n):
        for i in range(n):
            dp2[i][j] = min(dp2[i][j],dp2[i][k] + dp2[k][j])
"""
以上
"""
q = int(input())
ans = []
for i in range(q):
    s,t = map(int, input().split( ))
    if dp[s-1][t-1] >10**10:###
        print(-1)
    else:
        print(dp2[s-1][t-1]-1)
#print(dp)
#print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools

import queue
from collections import deque



def main():
    node_num, edge_num, max_tank = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(edge_num)]
    q_num = int(input())
    query = [list(map(int, input().split())) for i in range(q_num)]

    graph_data = defaultdict(set)
    dist_data = defaultdict(int)

    distance = [[10 ** 5 for i in range(node_num + 1)] for j in range(node_num + 1)]
    for i in range(1, node_num + 1):
        distance[i][i] = 0

    for i in range(edge_num):
        a, b, c = data[i]
        graph_data[a].add(b)
        graph_data[b].add(a)
        dist_data[(a, b)] = c
        dist_data[(b, a)] = c
        distance[a][b] = c
        distance[b][a] = c


    for k in range(1, node_num + 1):
        for i in range(1, node_num + 1):
            for j in range(1, node_num + 1):
                distance[i][j] = min(distance[i][k] + distance[k][j], distance[i][j])


    distance2 = [[10 ** 5 for i in range(node_num + 1)] for j in range(node_num + 1)]
    for i in range(1, node_num + 1):
        distance2[i][i] = 0

    for i in range(1, node_num + 1):
        for j in range(1, node_num + 1):
            if distance[i][j] <= max_tank:
                distance2[i][j] = 1


    for k in range(1, node_num + 1):
        for i in range(1, node_num + 1):
            for j in range(1, node_num + 1):
                distance2[i][j] = min(distance2[i][k] + distance2[k][j], distance2[i][j])

    for i in range(q_num):
        s, t = query[i]
        ans = distance2[s][t]
        if ans == 10 ** 5:
            print(-1)
        else:
            print(ans - 1)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools

import queue
from collections import deque



def main():
    node_num, edge_num, max_tank = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(edge_num)]
    q_num = int(input())
    query = [list(map(int, input().split())) for i in range(q_num)]

    graph_data = defaultdict(set)
    dist_data = defaultdict(int)

    distance = [[10 ** 5 for i in range(node_num + 1)] for j in range(node_num + 1)]
    for i in range(1, node_num + 1):
        distance[i][i] = 0

    for i in range(edge_num):
        a, b, c = data[i]
        graph_data[a].add(b)
        graph_data[b].add(a)
        dist_data[(a, b)] = c
        dist_data[(b, a)] = c
        distance[a][b] = c
        distance[b][a] = c


    for k in range(1, node_num + 1):
        for i in range(1, node_num + 1):
            for j in range(1, node_num + 1):
                distance[i][j] = min(distance[i][k] + distance[k][j], distance[i][j])


    distance2 = [[10 ** 5 for i in range(node_num + 1)] for j in range(node_num + 1)]
    for i in range(1, node_num + 1):
        distance2[i][i] = 0

    for i in range(1, node_num + 1):
        for j in range(1, node_num + 1):
            if distance[i][j] <= max_tank:
                distance2[i][j] = 1


    for k in range(1, node_num + 1):
        for i in range(1, node_num + 1):
            for j in range(1, node_num + 1):
                distance2[i][j] = min(distance2[i][k] + distance2[k][j], distance2[i][j])

    for i in range(q_num):
        s, t = query[i]
        ans = distance2[s][t]
        if ans == 10 ** 5:
            ans = -1
        print(ans - 1)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

gragh = [[0] * N for _ in range(N)]

for _ in range(M):
  a,b,c = map(int,input().split())
  gragh[a-1][b-1] = c

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

min_cost = shortest_path(np.array(gragh))

Q = int(input())

for i in range(Q):

	s, t = map(int,input().split())
	ans_tmp = min_cost[s-1][t-1]
	print("ans_tmp", ans_tmp)

	if L > ans_tmp:
		print(0)
	elif np.isinf(ans_tmp):
		print(-1)
	else:
		print(int((L+ans_tmp-1) / L - 1))

Submission status: Wrong Answer

Solution code:
import sys
n, m, l = map(int, sys.stdin.readline().split())
d = [[10**9] * (n+1) for _ in range(n+1)]
for i in range(n+1):
    d[i][i] = 0
for i in range(m):
    a, b, c = map(int, sys.stdin.readline().split())
    if c > l:
        continue
    d[a][b] = c
    d[b][a] = c

q = int(input())
qs = []
for i in range(q):
    s,t = map(int, sys.stdin.readline().split())
    qs.append([s, t])

for k in range(n+1):
    for i in range(n+1):
        for j in range(n+1):
            if d[i][k] + d[k][j] < d[i][j]:
                d[i][j] = d[i][k] + d[k][j]

for i in range(n+1):
    for j in range(n+1):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = 10**9
for k in range(n+1):
    for i in range(n+1):
        for j in range(n+1):
            if d[i][k] + d[k][j] < d[i][j]:
                d[i][j] = d[i][k] + d[k][j]
for i in range(q):
    st = qs[i][0]
    gl = qs[i][1]
    if d[st][gl] == 10**9:
        print(-1)
    else:
        print(d[st][gl]-1)

Submission status: Wrong Answer

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

gragh = [[0] * N for _ in range(N)]

for _ in range(M):
  a,b,c = map(int,input().split())
  gragh[a-1][b-1] = c

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

min_cost = shortest_path(np.array(gragh))

Q = int(input())

for i in range(Q):

	s, t = map(int,input().split())
	ans_tmp = min_cost[s-1][t-1]

	if L > ans_tmp:
		print(0)
	elif np.isinf(ans_tmp):
		print(-1)
	else:
		print((L+ans_tmp-1) / L)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#!/usr/bin/env python
# -*- coding: utf-8 -*-


class Node(object):

  def __init__(self, idx):
    self.idx = idx
    self.fixed = False
    self.supply = 300
    self.remain = 0
    self.edges = []

  # def __str__(self):
  #  return (f'{self.idx}: {self.supply}/{self.remain} - ' +
  #          ','.join(f'{e.idx}({d})' for e, d in self.edges))


def main():
  n, m, tank = map(int, input().split())
  nodes = [Node(i) for i in range(n + 1)]

  for _ in range(m):
    a, b, c = map(int, input().split())
    nodes[a - 1].edges.append((nodes[b - 1], c))
    nodes[b - 1].edges.append((nodes[a - 1], c))

  queries = [list(map(int, input().split())) for _ in range(int(input()))]
  results = [[None] * len(nodes) for _ in range(len(nodes) - 1)]

  for start in range(len(nodes) - 1):
    for n in nodes:
      n.fixed = False
      n.supply = 300
      n.remain = 0

    nodes[start].supply = 0
    nodes[start].remain = tank

    while True:
      node = min([n for n in nodes if not n.fixed], key=lambda n: (n.supply, -n.remain))
      node.fixed = True

      if node.idx == len(nodes) - 1:
        break

      for e, d in node.edges:
        if e.fixed or d > tank:
          continue

        if d > node.remain:
          remain = tank - d
          supply = node.supply + 1
        else:
          remain = node.remain - d
          supply = node.supply

        if e.supply > supply or e.remain < remain:
          e.supply = supply
          e.remain = remain

    for end in range(len(nodes)):
      results[start][end] = nodes[end].supply

  for start, end in queries:
    if results[start - 1][end - 1] == 300:
      print(-1)
    else:
      print(results[start - 1][end - 1])


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
N, M, L = map(int, input().split())
town = [[] for i in range(N+1)]
inf = 100000000000
for i in range(M):
    A, B, C = map(int, input().split())
    town[A].append([B, C])
    town[B].append([A, C])
Q = int(input())
def check(a):
    if a >= 0:
        return 1
    else:
        return 0
def bfs(s, t, cnt, lngth, l):
    if s == t:
        return cnt
    else:
        l.append(s)
        ans = []
        for i in range(len(town[s])):
            if (town[s][i][0] not in l):
                ans.append(bfs(town[s][i][0], t, cnt + check(town[s][i][1] + lngth - L), (town[s][i][1] + lngth) % L, l))
        if ans != []:
            return min(ans)
        else:
            return inf
for i in range(Q):
    s, t = map(int, input().split())
    ans = bfs(s, t, 0, 0, [])
    if ans == inf:
        print(-1)
    else:
        print(ans)