結果

問題 No.807 umg tours
ユーザー tomarint2tomarint2
提出日時 2019-03-22 23:22:54
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,966 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 81,616 KB
実行使用メモリ 839,764 KB
最終ジャッジ日時 2023-10-19 10:47:14
合計ジャッジ時間 5,198 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
76,400 KB
testcase_01 AC 141 ms
78,336 KB
testcase_02 AC 169 ms
78,188 KB
testcase_03 AC 162 ms
78,700 KB
testcase_04 AC 157 ms
78,256 KB
testcase_05 AC 118 ms
76,376 KB
testcase_06 AC 138 ms
77,080 KB
testcase_07 AC 145 ms
76,884 KB
testcase_08 AC 58 ms
66,108 KB
testcase_09 AC 67 ms
70,308 KB
testcase_10 AC 73 ms
72,512 KB
testcase_11 MLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import copy
import collections

N,M=map(int,sys.stdin.readline().rstrip().split())
d=[[0 for j in range(N+1)] for i in range(N+1)]
for i in range(M):
    a,b,c=map(int,sys.stdin.readline().rstrip().split())
    d[a][b]=d[b][a]=c
inf=1<<60

#現在地、ゴール、コスト、チケット利用回数、履歴
def solve0(location, goal, cost, ticket, history):
    cost2 = inf
    #print('solve', location, goal, cost, ticket)
    history2 = copy.copy(history)
    history2.add(location)
    if location==goal:
        return cost
    for i in range(1,N+1):
        if d[location][i]==0:
            continue
        if i in history2:
            continue
        cost2=min(cost2,solve(i,goal,cost+d[location][i],ticket,history2))
        if ticket==0:
            cost2=min(cost2,solve(i,goal,cost,1,history2))
    return cost2

def solve(location, goal, cost, ticket, history):
    q = collections.deque()
    q.append((location, cost, ticket, history))
    cost2 = inf
    lcost=[inf for i in range(N+1)]
    try:
        while True:
            location, cost, ticket, history = q.popleft()
            #print('solve', location, cost, ticket, history)
            if lcost[location]<location:
                continue
            if location==goal:
                cost2=min(cost2,cost)
                continue
            lcost[location]=location
            if cost >= cost2:
                continue
            history2 = copy.copy(history)
            history2.add(location)
            for i in range(1,N+1):
                if d[location][i]==0:
                    continue
                if i in history2:
                    continue
                if ticket==0:
                    q.append((i, cost, 1, history2))
                q.append((i, cost+d[location][i], ticket, history2))
    except IndexError:
        pass
    return cost2

for i in range(1,N+1):
    ans=solve(1, i, 0, 0, set()) + solve(1, i, 0, 1, set())
    print(ans)
0