結果

問題 No.1393 Median of Walk
ユーザー chineristACchineristAC
提出日時 2020-11-12 12:37:58
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,161 bytes
コンパイル時間 409 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 164,788 KB
最終ジャッジ日時 2023-09-27 04:53:02
合計ジャッジ時間 5,849 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 = []
for _ in range(m):
    u,v,c = map(int,input().split())
    edge[u-1].append(v-1)
    edge_cost[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)]
dfs = [[] for i in range(n)]

for u,v,w in Edge:
    edge_cost[u,v] = -1
    for i in range(n):
        if cost[i][u] != 10**18:
            if cost[i][v] > cost[i][u] + edge_cost[u,v]:
                cost[i][v] = cost[i][u] + edge_cost[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] = w
                dfs[i].append([v,0])

            while dfs[i]:
                v,idx = dfs[i][-1]
                if idx!=len(edge[v]):
                    dfs[i][-1][1] += 1
                    nv = edge[v][idx]
                    if cost[i][v]!=-10**18:
                        if cost[i][nv] > cost[i][v] + edge_cost[v,nv]:
                            cost[i][nv] = cost[i][v] + edge_cost[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].append([nv,0])
                    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].append([nv,0])
                else:
                    dfs[i].pop()

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