結果

問題 No.134 走れ!サブロー君
ユーザー Ueki
提出日時 2019-05-16 12:40:55
言語 Python3
(3.7.1 + numpy 1.14.5 + scipy 1.1.0)
結果
AC  
実行時間 1,678 ms
コード長 1,762 Byte
コンパイル時間 47 ms
使用メモリ 8,912 KB
最終ジャッジ日時 2019-08-12 17:48:43

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
01.txt AC 20 ms
6,872 KB
02.txt AC 20 ms
6,868 KB
03.txt AC 20 ms
6,872 KB
04.txt AC 35 ms
6,872 KB
05.txt AC 60 ms
8,912 KB
06.txt AC 126 ms
6,868 KB
07.txt AC 283 ms
6,868 KB
08.txt AC 682 ms
6,868 KB
09.txt AC 1,666 ms
8,204 KB
10.txt AC 1,678 ms
8,200 KB
system_test1.txt AC 20 ms
6,868 KB
system_test2.txt AC 19 ms
6,872 KB
system_test3.txt AC 20 ms
6,868 KB
system_test4.txt AC 20 ms
6,872 KB
system_test5.txt AC 20 ms
6,868 KB
テストケース一括ダウンロード

ソースコード

diff #
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
# python template for atcoder1


def manhattan(item1, item2):

    dist = abs(item1[0]-item2[0])+abs(item1[1]-item2[1])
    #print("dist->", dist)
    return dist


def calcCost(dropItem, last_visit, weight_sum):
    dist = manhattan(dropItem, last_visit)
    cost = (weight_sum+100)/120*dist
    ret = cost+dropItem[2]
    #print("ret->", ret)
    return ret


SX, SY = map(int, input().split())
N = int(input())
items = [list(map(float, input().split())) for _ in range(N)]

dp = [[float('inf')]*N for _ in range(1 << N)]

dp[0][0] = 0
for i in range(1):
    dp[0][i] = 0

for mask in range(1 << N):
    for new_item in range(N):
        if mask >> new_item & 1:
            continue

        new_state = mask | 1 << new_item
        if mask == 0:
            weight_sum = 0
            for loaded in range(N):
                if mask >> loaded & 1 == 0:
                    weight_sum += items[loaded][2]
            cost = calcCost(items[new_item], [SX, SY], weight_sum)
            dp[new_state][new_item] = cost
        else:
            for last_visit in range(N):
                if mask >> last_visit & 1:
                    weight_sum = 0
                    for loaded in range(N):
                        if mask >> loaded & 1 == 0:
                            weight_sum += items[loaded][2]

                    cost = calcCost(items[new_item],
                                    items[last_visit], weight_sum)
                    dp[new_state][new_item] = min(
                        dp[new_state][new_item], dp[mask][last_visit]+cost)

ans = float('inf')
for i in range(N):
    ans = min(ans, dp[-1][i]+manhattan([SX, SY], items[i])*100/120)
print(ans)
0