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:
def main():
    import sys
    input = sys.stdin.readline
    n,m,l = map(int,input().split())
    E = [[float("INF")]*n for i in range(n)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a-1][b-1] = c
        E[b-1][a-1] = c

    for k in range(n):
        for i in range(n):
            for j in range(n):
                E[i][j] = min(E[i][j],E[i][k]+E[k][j])
    wa = [[float("INF")]*n for i in range(n)]
    for i in range(n):
        for j in range(n):
            if E[i][j] <= l:
                wa[i][j] = 1
    for k in range(n):
        for i in range(n):
            for j in range(n):
                wa[i][j] = min(wa[i][j],wa[i][k]+wa[k][j])
    print(wa)
    x = int(input())
    for i in range(x):
        s,g = map(int,input().split())
        if wa[s-1][g-1] == float("INF"):
            print(-1)
        else:
            print(wa[s-1][g-1]-1)

    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    
    n,m,l = map(int,input().split())
    E = [[] for i in range(n)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a-1].append((b-1,c))
        E[b-1].append((a-1,c))

    import heapq

    def dijkstra(graph, node, start):
        dist = [[float("INF")]*2 for _ in range(node)]
        
        dist[start] = [0,l]
        q = []
        heapq.heappush(q, (0,start,l))
        
        while q:
            count,cur,fuel = heapq.heappop(q)
            print(count,cur,fuel)
            for nex, nex_len in graph[cur]:
                if fuel >= nex_len:
                    nex_fuel = fuel - nex_len
                    if dist[nex][0] > count or (dist[nex][0]==count and nex_fuel > dist[nex][1]):
                        heapq.heappush(q, (count,nex,nex_fuel))
                        dist[nex] = [count,nex_fuel]
                else:
                    if l >= nex_len:
                        nex_fuel = l-nex_len
                        nex_count = count+1
                        if dist[nex][0] > nex_count or (dist[nex][0]==nex_count and nex_fuel > dist[nex][1]):
                            heapq.heappush(q, (nex_count,nex,nex_fuel))
                            dist[nex] = [nex_count,nex_fuel]

        return dist


    ans = []
    for i in range(n):
        ans.append(dijkstra(E, n, i))

    x = int(input())
    for i in range(x):
        s,g = map(int,input().split())
        if ans[s-1][g-1][0] == float("INF"):
            print(-1)
        else:
            print(ans[s-1][g-1][0]) 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    
    n,m,l = map(int,input().split())
    E = [[] for i in range(n)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a-1].append((b-1,c))
        E[b-1].append((a-1,c))

    import heapq

    def dijkstra(graph, node, start):
        dist = [[float("INF")]*2 for _ in range(node)]
        
        dist[start] = [0,l]
        q = []
        heapq.heappush(q, (0,start,l))
        
        while q:
            count,cur,fuel = heapq.heappop(q)
            print(count,cur,fuel)
            for nex, nex_len in graph[cur]:
                if fuel >= nex_len:
                    nex_fuel = fuel - nex_len
                    if dist[nex][0] > count or (dist[nex][0]==count and nex_fuel > dist[nex][1]):
                        heapq.heappush(q, (count,nex,nex_fuel))
                        dist[nex] = [count,nex_fuel]
                else:
                    if l >= nex_len:
                        nex_fuel = l-nex_len
                        nex_count = count+1
                        if dist[nex][0] > nex_count or (dist[nex][0]==nex_count and nex_fuel > dist[nex][1]):
                            heapq.heappush(q, (nex_count,nex,nex_fuel))
                            dist[nex] = [nex_count,nex_fuel]

        return dist


    ans = []
    for i in range(n):
        ans.append(dijkstra(E, n, i))
    print(ans)
    x = int(input())
    for i in range(x):
        s,g = map(int,input().split())
        if ans[s-1][g-1][0] == float("INF"):
            print(-1)
        else:
            print(ans[s-1][g-1][0]) 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,m,l = map(int, input().split())
wf=[[float("inf") for i in range(n)] for j in range(n)]
for i in range(m):
    a,b,c = list(map(int,input().split()))
    if c<=l:
        wf[a-1][b-1] = c
        wf[b-1][a-1] = c
q = int(input())
st=[list(map(int,input().split())) for i in range(q)]

nxt=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        nxt[i][j]=j
for i in range(n):
    wf[i][i]=0
for k in range(n):
    for i in range(n):
        for j in range(n):
            if wf[i][k]+wf[k][j]< wf[i][j] :
                wf[i][j] = wf[i][k]+wf[k][j]
                nxt[i][j] = nxt[i][k]
def get_p(s,g):
    p=[]
    while s!=g:
        p.append(s)
        s=nxt[s][g]
    p.append(s)
    return p

for que in st:
    s,t=que
    if wf[s-1][t-1] == float("inf"):
        print(-1)
        continue
    path = get_p(s-1,t-1)
    cnt=0
    now=l
    for i in range(len(path)-1):
        dist=wf[path[i]][path[i+1]]
        if now<dist:
            cnt+=1
            now=l
        now-=dist
    print(cnt)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int, input().split())
wf=[[float("inf") for i in range(n)] for j in range(n)]
for i in range(m):
    a,b,c = list(map(int,input().split()))
    if c<=l:
        wf[a-1][b-1] = c
        wf[b-1][a-1] = c
q = int(input())
st=[list(map(int,input().split())) for i in range(q)]
nxt=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        nxt[i][j]=j
for i in range(n):
    wf[i][i]=0
for k in range(n):
    for i in range(n):
        for j in range(i+1,n):
            if wf[i][k]+wf[k][j]< wf[i][j] :
                wf[i][j] = wf[i][k]+wf[k][j]
                nxt[i][j] = nxt[i][k]
                wf[j][i] = wf[i][j]
                nxt[j][i] = nxt[j][k]
def get_p(s,g):
    p=[]
    while s!=g:
        p.append(s)
        s=nxt[s][g]
    p.append(s)
    return p

for que in st:
    s,t=que
    if wf[s-1][t-1] == float("inf"):
        print(-1)
        continue
    path = get_p(s-1,t-1)
    cnt=0
    now=l
    for i in range(len(path)-1):
        dist=wf[path[i]][path[i+1]]
        if now<dist:
            cnt+=1
            now=l
        now-=dist
    print(cnt)

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    n,m,l = map(int,input().split())
    E = [[] for i in range(n+1)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a].append((b,c))
        E[b].append((a,c))
    dis = []
    def bfs(s):
        d = [-1]*(n+1)
        d[s] = 0
        count = 0
        li = l
        q = deque([])
        q.append((s,-1,count,li))
        while q:
            now,pre,c,fuel = q.popleft()

            for nex,le in E[now]:
                if nex == pre:
                    continue
                if d[nex] == -1:
                    if fuel >= le:
                        k1 = fuel-le
                        d[nex] = c
                        q.append((nex,now,c,k1))
                    
                    elif l >= le > fuel:    
                        k1 = l-le
                        c += 1
                        d[nex] = c
                        q.append((nex,now,c,k1))
                else:
                    if fuel >= le:
                        k1 = fuel-le
                        if c < d[nex]:
                            d[nex] = c
                            q.append((nex,now,c,k1))
                    
                    elif l >= le > fuel:    
                        k1 = l-le
                        c += 1
                        if c < d[nex]:
                            d[nex] = c
                            q.append((nex,now,c,k1))

                
        dis.append(d)

    x = int(input())
    for i in range(1,n+1):
        bfs(i)
    
    for i in range(x):
        s,g = map(int,input().split())
        print(dis[s-1][g])
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    n,m,l = map(int,input().split())
    E = [[] for i in range(n+1)]
    for i in range(m):
        a,b,c = map(int,input().split())
        E[a].append((b,c))
        E[b].append((a,c))
    dis = []
    def bfs(s):
        d = [-1]*(n+1)
        d[s] = 0
        count = 0
        li = l
        q = deque([])
        q.append((s,-1,count,li))
        while q:
            now,pre,c,fuel = q.popleft()

            for nex,le in E[now]:
                if nex == pre:
                    continue
                if d[nex] == -1:
                    if fuel >= le:
                        k1 = fuel-le
                        d[nex] = c
                        q.append((nex,now,c,k1))
                    
                    elif l >= le > fuel:    
                        k1 = l-le
                        c += 1
                        d[nex] = c
                        q.append((nex,now,c,k1))
                else:
                    if fuel >= le:
                        k1 = fuel-le
                        if c < d[nex]:
                            d[nex] = c
                            q.append((nex,now,c,k1))
                    
                    elif l >= le > fuel:    
                        k1 = l-le
                        c += 1
                        if c < d[nex]:
                            d[nex] = c
                            q.append((nex,now,c,k1))

                
        dis.append(d)

    x = int(input())
    for i in range(1,n+1):
        bfs(i)
    print(dis)
    for i in range(x):
        s,g = map(int,input().split())
        print(dis[s-1][g])
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

def WarshallFloyd(dist, N):
    for i in range(N):
        for j in range(N):
            for k in range(N):
                dist[i, j] = min(dist[i, j], dist[i, k] + dist[k, j])
    return dist

def main():
    INF = float('inf')
    N, M, L = map(int, input().split())
    dist = np.full((N, N), INF)
    
    for _ in range(M):
        x, y, cost = map(int, input().split())
        dist[x-1, y-1] = cost
        dist[y-1, x-1] = cost
    
    dist = WarshallFloyd(dist, N) # 任意の点からの最小距離
    dist = np.where(dist <= L, 1, INF) # 任意の点から一回の補給で行ける場所
    dist = WarshallFloyd(dist, N) # 任意の点からの最小補給回数
    
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        if dist[s-1, t-1] == INF:
            print(-1)
        else:
            print(int(dist[s-1, t-1] - 1)) # 最初は満タンで開始
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
way=[[float("inf")]*n for _ in range(n)]
for i in range(m):
    x,y,z=map(int,input().split())
    way[x-1][y-1]=z
    way[y-1][x-1]=z
for j in range(n):
    way[j][j]=0
def warshall_floyd(d):
    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
s=warshall_floyd(way)
def foo(s):
  for i in range(n):
    for j in range(n):
      if s[i][j]!=0 and s[i][j]<=l:
        s[i][j]=1
  return s
a=foo(s)
ans=warshall_floyd(a)
q=int(input())
for _ in range(q):
  aa,bb=map(int,input().split())
  if ans[aa-1][bb-1]!=float('inf'):
    print(ans[aa-1][bb-1]-1)
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
way=[[float("inf")]*n for _ in range(n)]
for i in range(m):
    x,y,z=map(int,input().split())
    way[x-1][y-1]=z
    way[y-1][x-1]=z
for j in range(n):
    way[j][j]=0
def warshall_floyd(d):
    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
s=warshall_floyd(way)
def foo(s):
  for i in range(n):
    for j in range(n):
      if s[i][j]!=0 and s[i][j]<l:
        s[i][j]=1
  return s
a=foo(s)
ans=warshall_floyd(a)
q=int(input())
for _ in range(q):
  aa,bb=map(int,input().split())
  if ans[aa-1][bb-1]!=float('inf'):
    print(ans[aa-1][bb-1])
  else:
    print(-1)

Submission status: Wrong Answer

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

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

INF = 10 ** 15

def warshall_floyd(dist, N):
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j])
    return dist

N, M, L = lr()
dist = [[INF] * (N+1) for _ in range(N+1)] # 1-indexed
for _ in range(M):
    a, b, c = lr()
    if c > L:
        continue
    dist[a][b] = c
    dist[b][a] = c

dist = warshall_floyd(dist, N+1)
supply = [[INF] * (N+1) for _ in range(N+1)] # 1-indexed 距離L以下で供給が1回必要
for i in range(1, N+1):
    for j in range(i+1, N+1):
        if dist[i][j] <= L:
            supply[i][j] = supply[j][i] =  1

supply = warshall_floyd(supply, N+1)
Q = ir()
for _ in range(Q):
    s, t = lr()
    x = supply[s][j] - 1
    print(-1 if x + 1 == INF else x)

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

INF = 10 ** 15
def dijkstra(start):
    dist = [(INF, INF)] * (N+1) #補給回数、燃料
    dist[start] = (0, L)
    time = 0 #補給回数
    remain = L #燃料
    que = [(time, remain, start)]
    while que:
        t, r, prev = heappop(que)
        if dist[prev][0] < t:
            continue
        for next, c in graph[prev]:
            if r < c:
                t2 = t + 1; r2 = L
            else:
                t2 = t; r2 = r
            r2 -= c
            if dist[next][0] > t2 or (dist[next][0] == t2 and dist[next][1] > r2):
                dist[next] = (t2, r2)
                heappush(que, (t2, r2, next))
    return dist

N, M, L = lr()
graph = [[] for _ in range(N+1)] # 1-indexed
for _ in range(M):
    a, b, c = lr()
    if c > L:
        continue
    graph[a].append((b, c))
    graph[b].append((a, c))

Q = ir()
for _ in range(Q):
    s, t = lr()
    dist = dijkstra(s)
    print(-1 if dist[t][0] == INF else dist[t][0])

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
edge=[[]for _ in range(n)]
for _ in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1
  edge[a].append([b,c])
  edge[b].append([a,c])
from heapq import heappop,heappush
inf=10**10
cost=[[[inf,inf]for _ in range(n)]for _ in range(n)]
for i in range(n):
  cost[i][i]=[0,0]
  h=[[0,i]]
  while h:
    c,v=heappop(h)
    c,co=c//(10**9+7),c%(10**9+7)
    if cost[i][v][0]<c or (cost[i][v][0]==c and cost[i][v][1]<co):continue
    for u,t in edge[v]:
      if co+t>l:
        c2=c+1
        co2=t
      else:
        c2=c
        co2=co+t
      if cost[i][u][0]>c2 or (cost[i][u][0]==c2 and cost[i][u][1]>co2):
        cost[i][u]=[c2,co2]
        heappush(h,[c2*(10**9+7)+co2,u])
ans=[[j[0]for j in i]for i in cost]
q=int(input())
ab=[list(map(int,input().split()))for _ in range(q)]
for a,b in ab:
  if ans[a-1][b-1]==10**10:print(-1)
  else:print(ans[a-1][b-1])

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
edge=[[]for _ in range(n)]
for _ in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1
  edge[a].append([b,c])
  edge[b].append([a,c])
from heapq import heappop,heappush
inf=10**10
cost=[[[inf,inf]for _ in range(n)]for _ in range(n)]
for i in range(n):
  cost[i][i]=[0,0]
  h=[[0,i]]
  while h:
    c,v=heappop(h)
    c,co=c//(10**9+7),c%(10**9+7)
    if cost[i][v][0]<c or (cost[i][v][0]==c and cost[i][v][1]<co):continue
    for u,t in edge[v]:
      co2=co+t
      c2=c+co2//l
      co2%=l
      if cost[i][u][0]>c2 or (cost[i][u][0]==c2 and cost[i][u][1]>co2):
        cost[i][u]=[c2,co2]
        heappush(h,[c2*(10**9+7)+co2,u])
ans=[[j[0]for j in i]for i in cost]
q=int(input())
for _ in range(q):
  a,b=map(int,input().split())
  a-=1;b-=1
  if ans[a][b]==10**10:print(-1)
  else:print(ans[a][b])

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
edge=[[]for _ in range(n)]
for _ in range(m):
  a,b,c=map(int,input().split())
  a-=1;b-=1
  edge[a].append([b,c])
  edge[b].append([a,c])
from heapq import heappop,heappush
inf=10**10
cost=[[[inf,inf]for _ in range(n)]for _ in range(n)]
for i in range(n):
  cost[i][i]=[0,0]
  h=[[0,i]]
  while h:
    c,v=heappop(h)
    c,co=c//(10**9+7),c%(10**9+7)
    if cost[i][v][0]<c or (cost[i][v][0]==c and cost[i][v][1]<co):continue
    for u,t in edge[v]:
      co2=co+t
      c2=c+co2//l
      co2%=l
      if cost[i][u][0]>c2 or (cost[i][u][0]==c2 and cost[i][u][1]>co2):
        cost[i][u]=[c2,co2]
        heappush(h,[c2*(10**9+7)+co2,u])
ans=[[j[0]for j in i]for i in cost]
q=int(input())
for _ in range(q):
  a,b=map(int,input().split())
  a-=1;b-=1
  if ans[a][b]==10**10-1:print(-1)
  else:print(ans[a][b])

Submission status: Wrong Answer

Solution code:
N, M, L = map(int, input().split())
Map = [[[int(1e18), []] for _ in range(N)] for __ in range(N)]

for i in range(M):
  A, B, C = map(int, input().split())
  if C <= L:
    Map[A-1][B-1] = [C, [A-1, B-1]]
    Map[B-1][A-1] = [C, [B-1, A-1]]
  
for k in range(N):
  for i in range(N):
    for j in range(N):
      if Map[i][j][0] > Map[i][k][0] + Map[k][j][0]:
        Map[i][j][0] = Map[i][k][0] + Map[k][j][0]
        Map[i][j][1] = Map[i][k][1][:] + Map[k][j][1][1:]
for i in range(N):
  for j in range(N):
    if Map[i][j][0] == int(1e18):
      Map[i][j][0] = -1
Q = int(input())
for i in range(Q):
  s, t = map(int, input().split())
  s -= 1
  t -= 1
  if Map[s][t][0] == -1:
    print(-1)
  else:
    x = Map[s][t][1][:]
    n = len(x)
    S = 0
    cnt = 0
    for i in range(n-1):
      S += Map[x[i]][x[i+1]][0]
      if S > L:
        cnt += 1
        S = Map[x[i]][x[i+1]][0]
    print(cnt)

Submission status: Wrong Answer

Solution code:
N,M,L=map(int,input().split())
dp=[[10**12]*(N) for _ in range(N)]
memo=[[0]*(N) for _ in range(N)]
rest=[[0]*(N) for _ in range(N)]
for i in range(M):
  x,y,d=map(int,input().split())
  if d>L:
    pass
  else:
    dp[x-1][y-1]=d
    dp[y-1][x-1]=d
    rest[x-1][y-1]=L-d
    rest[y-1][x-1]=L-d
    
for k in range(N):   
  for i in range(N):
    for j in range(N):
      if i==j:
        dp[i][j]=0
      else:
        if dp[i][j]<=dp[i][k]+dp[k][j]:
          pass
        else:
          dp[i][j]=dp[i][k]+dp[k][j]
          if memo[i][k]==0 and memo[k][j]==0 and dp[i][j]>L:
            memo[i][j]=1
            rest[i][j]=L-dp[k][j]
          else:
            memo[i][j]=memo[i][k]+memo[k][j]
            if rest[i][k]<dp[k][j]:
              memo[i][j]+=1
              rest[i][j]=L-dp[k][j]
            else:
              rest[i][j]=rest[i][k]-dp[k][j]              
Q=int(input())
for i in range(Q):
  s,t=map(int,input().split())
  s-=1
  t-=1
  dist=dp[s][t]
  if dist==10**12:
    print(-1)
  else:
    print(memo[s][t])

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
INF = 10**12

N, M, L = map(int, input().split())
if M == 0:
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        print(-1)
    exit()
G = np.zeros((N, N), dtype=int)
A, B, C = np.array([input().split() for _ in range(M)], dtype=int).T
A -= 1
B -= 1
np.add.at(G, (A, B), C)
np.add.at(G, (B, A), C)

fw = floyd_warshall(G)
fw = (fw+0.5).astype(int)
fill = np.zeros_like(fw)
fill[fw <= L] = 1
fill[fw == 0] = 0
fill_fw = floyd_warshall(fill)
fill_fw = (fill_fw + 0.5).astype(int)


Q = int(input())
S, T = np.array([input().split() for _ in range(Q)], dtype=int).T
S -= 1
T -= 1
ans = fill_fw[S, T] - 1
print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
INF = 10**12

N, M, L = map(int, input().split())
if M == 0:
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        print(-1)
        exit()
G = np.zeros((N, N), dtype=int)
A, B, C = np.array([input().split() for _ in range(M)], dtype=int).T
A -= 1
B -= 1
np.add.at(G, (A, B), C)
np.add.at(G, (B, A), C)

fw = floyd_warshall(G)
fw = (fw+0.5).astype(int)
fill = np.zeros_like(fw)
fill[fw <= L] = 1
fill[fw == 0] = 0
fill_fw = floyd_warshall(fill)
fill_fw = (fill_fw + 0.5).astype(int)


Q = int(input())
S, T = np.array([input().split() for _ in range(Q)], dtype=int).T
S -= 1
T -= 1
ans = fill_fw[S, T] - 1
print(*ans, sep="\n")

Submission status: Wrong Answer

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

import sys
from collections import deque
from array import array

def input():
    return sys.stdin.readline().rstrip('\n')

# diagonal square matrix for pypy

class DiagonalMatrix:
    def __init__(self, size):
        self._size = size
        array_size = (size+1)*size//2
        # 三角行列の要素を1次元配列に並べる
        self._storage = array('l', [0]*array_size)

    def _index(self, i, j):
        _n = self._size
        _i = i+1
        _j = j+1
        return (_n+1)*_n//2 - (_n-_i+2)*(_n-_i+1)//2 + (_j-_i+1) - 1
    
    def get(self,i,j):
        if i <= j:
            return self._storage[self._index(i,j)]
        else:
            return self._storage[self._index(j,i)]

    def set(self, i, j, value):
        if i <= j:
            self._storage[self._index(i,j)] = value
        else:
            self._storage[self._index(j,i)] = value
        
'''
N = 5
matrix = DiagonalMatrix(N)
for i in range(N):
    for j in range(i,N):
        matrix.set(i,j,matrix._index(i,j))
        print(i,j,matrix._index(i,j),matrix._storage)

for i in range(N):
    print([matrix.get(i,j) for j in range(N)])
'''
    
##### main

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

C = DiagonalMatrix(N)

for i in range(M):
    a,b,c = list(map(int, input().split()))
    a -= 1
    b -= 1
    if c>L:
        continue
    C.set(a,b,c)

Q = int(input())

#S = array('q', [0]*Q)
#T = array('q', [0]*Q)
#S = [0]*Q
#T = [0]*Q
#ST = {}
ST = [None]*Q

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

# ワーシャルフロイド法

# distance from i to j
#D = [array('q', [0]*N) for i in range(N)]
D = DiagonalMatrix(N)

# initialization
for i in range(N):
    for j in range(i,N):
        c = C.get(i,j)
        if c == 0:
            D.set(i,j,sys.maxsize)
        else:
            D.set(i,j,c)

# 本計算
for k in range(N):
    for i in range(N):
        for j in range(i,N):
            if D.get(i,j) > D.get(i,k) + D.get(k,j):
                D.set(i,j, D.get(i,k) + D.get(k,j))

'''
fo i in range(N):
    print([D.get(i,j) for j in range(N)])
'''


sys.exit()

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,w,l= map(int,input().split()) #n:頂点数　w:辺の数

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

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,w,l= map(int,input().split()) #n:頂点数　w:辺の数

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

Submission status: Wrong Answer

Solution code:
import heapq

n, m, l = map(int, input().split())
v = [[] for i in range(n)]
for i in range(m):
    a, b, c, = map(int, input().split())
    a, b, = a-1, b-1
    if c > l:
        continue
    v[a].append((c, b))
    v[b].append((c, a))


def dijkstra(home):
    visited = [False for i in range(n)]
    visited[home] = -1
    queue = []
    for edge in v[home]:
        heapq.heappush(queue, edge)


    while queue:
        value, x = heapq.heappop(queue)
        if visited[x]:
            continue
        visited[x] = value
        for new_value, new_x in v[x]:
            if not visited[new_x]:
                heapq.heappush(queue, new_value+value)

    return visited


def dijkstra2(home):
    visited = [None for _ in range(n)]
    visited[home] = 0
    queue = []
    for fuel, x in v[home]:
        heapq.heappush(queue, (0, l-fuel, x))


    while queue:
        tanked, fuel, x = heapq.heappop(queue)
        if visited[x] is not None:
            continue
        visited[x] = tanked
        for child_value, child_x in v[x]:
            if visited[child_x] is None:
                if child_value > fuel:
                    heapq.heappush(queue, (tanked+1, l-child_value, child_x))
                else:
                    heapq.heappush(queue, (tanked, fuel-child_value, child_x))

    return visited






visited_collection = [None for _ in range(n)]



q = int(input())
queries = []
for i in range(q):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    if visited_collection[a]:
        print(visited_collection[a][b] if visited_collection[a][b] is not None else -1)
    elif visited_collection[b]:
        print(visited_collection[b][a] if visited_collection[b][a] is not None else -1)
    else:
        visited_collection[a] = dijkstra2(a)
        print(visited_collection[a][b] if visited_collection[a][b] is not None else -1)

    # queries.append((a, b))

Submission status: Wrong Answer

Solution code:

import numpy as np

class graph():
    def __init__(self, N):
        self.N = N # number of vertices
        self.conn = np.full([N,N], -1, int) # -1 indicate unconnected 
    def add(self, A, B, C):
        self.conn[A-1,B-1] = C
        self.conn[B-1,A-1] = C
    def shortest(self):
        self.short = np.copy(self.conn)
        for k in range(self.N):
            for i in range(self.N):
                for j in range(i+1, self.N):
                    if (self.short[i,k] != -1) and (self.short[k,j] != -1):
                        t = self.short[i,k] + self.short[k,j]
                        if (self.short[i,j] == -1) or (t < self.short[i,j]):
                            self.short[i,j] = t
                            self.short[j,i] = t
        return self.short
                    
                    
N, M, L = list(map(int, input().split()))
G = graph(N)
for i in range(M):
    G.add(*tuple(map(int, input().split())))
    
Q = int(input())
ST = []
for i in range(Q):
    ST += [list(map(int, input().split()))]

G_short = G.shortest()
G2 = graph(N)
for i in range(N):
    for j in range(i+1, N):
        if (not G_short[i,j] == -1) and G_short[i,j] <= L:
            G2.add(i+1, j+1, 1)

G2_short = G2.shortest()
for i in range(Q):
    print(G2_short[ST[i][0]-1, ST[i][1]-1]-1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict
import sys
input = sys.stdin.readline

N, M, L = map(int, input().split())
inf = float("inf")
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    d[A-1].append((B-1, C))
    d[B-1].append((A-1, C))

charged_dict = [[(inf,inf)]*N for _ in range(N)]
for j in range(N):
    charged_dict[j][j] = (0, 0)
    q = [(0, 0, j)]
    while q:
        charged, used, u = heappop(q)
        for v, next_cost in d[u]:
            c = (charged+1, next_cost) if used+next_cost>L else (charged, used+next_cost)
            if charged_dict[j][v] > c:
                charged_dict[j][v] = c
                heappush(q, c+(v,))

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

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict
import sys
input = sys.stdin.readline

N, M, L = map(int, input().split())
inf = 10**20
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    d[A-1].append((B-1, C))
    d[B-1].append((A-1, C))

charged_dict = [[(inf,inf)]*N for _ in range(N)]
for j in range(N):
    charged_dict[j][j] = (0, 0)
    q = [(0, 0, j)]
    while q:
        charged, used, u = heappop(q)
        for v, next_cost in d[u]:
            c = (charged+1, next_cost) if used+next_cost>L else (charged, used+next_cost)
            if charged_dict[j][v] > c:
                charged_dict[j][v] = c
                heappush(q, c+(v,))

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