結果

問題 No.1393 Median of Walk
ユーザー chineristACchineristAC
提出日時 2020-11-12 15:53:00
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,699 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 82,640 KB
最終ジャッジ日時 2023-09-27 04:53:09
合計ジャッジ時間 5,790 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 #

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

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

for s in range(n):
    for u,v in edge_cost:
        edge_cost[u,v] = 1

    dist = [10**18 for i in range(n)]
    dist[s] = 0

    def dfs(t,w):
        que = [t]
        while que:
            v = que.pop()
            for nv in edge[v]:
                if dist[v]!=-10**18:
                    if dist[nv] > dist[v] + edge_cost[v,nv]:
                        dist[nv] = dist[v] + edge_cost[v,nv]
                        if dist[nv] < -n:
                            dist[nv] = -10**18
                        if dist[nv]<=0 and ans[s][nv]==-1:
                            ans[s][nv] = w
                        que.append(nv)
                else:
                    if dist[nv]!=-10**18:
                        dist[nv] = -10**18
                        if ans[s][nv]==-1:
                            ans[s][nv] = w
                        que.append(nv)

    dfs(s,-1)

    for u,v,w in edge_weight:
        edge_cost[u,v] = -1
        if dist[u]==10**18:
            continue
        if dist[v] > dist[u] + edge_cost[u,v]:
            dist[v] = dist[u] + edge_cost[u,v]
            if dist[v] < -n:
                dist[v] = -10**18
            if dist[v]<=0 and ans[s][v]==-1:
                ans[s][v] = w
            dfs(v,w)


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