結果
| 問題 |
No.2039 Copy and Avoid
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 20:05:29 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,410 bytes |
| コンパイル時間 | 225 ms |
| コンパイル使用メモリ | 82,048 KB |
| 実行使用メモリ | 301,152 KB |
| 最終ジャッジ日時 | 2025-06-12 20:12:08 |
| 合計ジャッジ時間 | 6,570 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 1 -- * 30 |
ソースコード
import heapq
def main():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
n = int(data[idx])
idx +=1
m = int(data[idx])
idx +=1
a = int(data[idx])
idx +=1
b = int(data[idx])
idx +=1
c = list(map(int, data[idx:idx+m]))
forbidden = set(c)
heap = []
heapq.heappush(heap, (0, 1, 1))
visited = dict()
visited[(1, 1)] = 0
found = False
answer = -1
while heap:
cost, x, y = heapq.heappop(heap)
if x == n:
answer = cost
found = True
break
if visited.get((x, y), float('inf')) < cost:
continue
# Operation a
new_x_a = x + y
if new_x_a not in forbidden:
new_cost_a = cost + a
key = (new_x_a, y)
if key not in visited or new_cost_a < visited.get(key, float('inf')):
visited[key] = new_cost_a
heapq.heappush(heap, (new_cost_a, new_x_a, y))
# Operation b
new_x_b = x
new_y_b = x
new_cost_b = cost + b
key = (new_x_b, new_y_b)
if key not in visited or new_cost_b < visited.get(key, float('inf')):
visited[key] = new_cost_b
heapq.heappush(heap, (new_cost_b, new_x_b, new_y_b))
print(answer if found else -1)
if __name__ == "__main__":
main()
gew1fw