LoginSignup
0
1

More than 3 years have passed since last update.

[Python]探索(itertools) ABC167C

Last updated at Posted at 2020-05-19

ABC167C

itertoolsを活用した探索
探索は bit全探索が推奨されることが多いが、Pythonなら基本的にはitertoolsで対応した方が実戦的ではないかと思っている。
Pythonのitertoolsでできる全列挙のまとめ

サンプルコード
import itertools

N, M, X = list(map(int, input().split()))
a = [list( map( int, input().split() ) ) for i in range(N)]

for x in itertools.product([0,1], repeat=2):
  cost = 0
  level = [] * M
  judge = True
  ans = 1234567
  for i in range(N):
    if a[i] == 1:
      cost += a[i][0]
      level = [y + z for (y, z) in zip(level, a[i][1:])]
  for j in range(M):
    if any(level[k] < x for k in range(M)):
      judge = False
  if judge:
    ans = min(ans, cost)

if ans == 1234567:
  print(-1)
else:
  print(ans)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1