結果

問題 No.1393 Median of Walk
ユーザー chineristACchineristAC
提出日時 2020-11-12 12:25:26
言語 PyPy3
(7.3.15)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,722 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,308 KB
実行使用メモリ 91,560 KB
最終ジャッジ日時 2023-09-27 04:52:50
合計ジャッジ時間 5,831 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(2*10**5)

n,m = map(int,input().split())
edge = [{} for i in range(n)]
Edge = []
for _ in range(m):
    u,v,c = map(int,input().split())
    edge[u-1][v-1] = 1
    Edge.append((u-1,v-1,c))

cost = [[10**18 for j in range(n)] for i in range(n)]
for i in range(n):
    cost[i][i] = 0
for u,v,c in Edge:
    cost[u][v] = 1

for k in range(n):
    for i in range(n):
        for j in range(n):
            if cost[i][j] > cost[i][k] + cost[k][j]:
                cost[i][j] = cost[i][k] + cost[k][j]

Edge.sort(key=lambda x:x[2])
ans  = [[-1 for j in range(n)] for i in range(n)]

def dfs(i,v,w):
    for nv in edge[v]:
        if cost[i][v]!=-10**18:
            if cost[i][nv] > cost[i][v] + edge[v][nv]:
                cost[i][nv] = cost[i][v] + edge[v][nv]
                if cost[i][nv] < -n:
                    cost[i][nv] = -10**18
                if cost[i][nv]<=0 and ans[i][nv]==-1:
                    ans[i][nv] = w
                dfs(i,nv,w)
        else:
            if cost[i][nv]!=-10**18:
                cost[i][nv] = -10**18
                if cost[i][nv]<=0 and ans[i][nv]==-1:
                    ans[i][nv] = w
                dfs(i,nv,w)

for u,v,c in Edge:
    edge[u][v] = -1
    for i in range(n):
        if cost[i][u] != 10**18:
            if cost[i][v] > cost[i][u] + edge[u][v]:
                cost[i][v] = cost[i][u] + edge[u][v]
                if cost[i][v] < -n:
                    cost[i][v] = -10**18
                if cost[i][v]<=0 and ans[i][v] == -1:
                    ans[i][v] = c
                dfs(i,v,c)

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

print(*res,sep="\n")
0