結果

問題 No.177 制作進行の宮森あおいです!
ユーザー e-mone-mon
提出日時 2015-05-28 12:03:50
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 2,528 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 11,144 KB
実行使用メモリ 9,100 KB
最終ジャッジ日時 2023-09-20 17:05:29
合計ジャッジ時間 1,293 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,572 KB
testcase_01 AC 19 ms
8,484 KB
testcase_02 AC 18 ms
8,444 KB
testcase_03 AC 19 ms
8,448 KB
testcase_04 AC 21 ms
8,464 KB
testcase_05 AC 21 ms
8,588 KB
testcase_06 AC 23 ms
8,876 KB
testcase_07 AC 19 ms
8,496 KB
testcase_08 AC 20 ms
8,656 KB
testcase_09 AC 26 ms
9,064 KB
testcase_10 AC 26 ms
9,044 KB
testcase_11 AC 25 ms
9,000 KB
testcase_12 AC 25 ms
9,100 KB
testcase_13 AC 18 ms
8,540 KB
testcase_14 AC 18 ms
8,372 KB
testcase_15 AC 17 ms
8,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Dinic:
    class Edge:
        def __init__(self, to, cap, rev):
            self.to = to
            self.cap = cap
            self.rev = rev
        
        def __repr__(self):
            return "(to: {0} cap: {1} rev: {2})".format(self.to, self.cap, self. rev)

    def __init__(self,V):
        self.V = V
        self.size = [0 for i in range(V)]
        self.G = [[] for i in range(V)]

    def add_edge(self, _from, to, cap):
        self.G[_from].append(self.Edge(to, cap, self.size[to]))
        self.G[to].append(self.Edge(_from, 0, self.size[_from]))
        self.size[_from] += 1
        self.size[to] += 1

    def bfs(self,s):
        level = [-1 for i in range(self.V)]
        level[s] = 0
        q = []
        q.append(s)
        while q != []:
            v = q.pop(0)
            for u in self.G[v]:
                if u.cap > 0 and level[u.to] < 0:
                    level[u.to] = level[v] + 1
                    q.append(u.to)
        return level

    def dfs(self, v, t, f):
        if v == t: return f
        for i in range(self.iterator[v],self.size[v]):
            self.iterator[v] = i
            edge = self.G[v][i]
            if edge.cap > 0 and self.level[v] < self.level[edge.to]:
                d = self.dfs(edge.to, t, f if f < edge.cap else edge.cap)
                if d > 0:
                    self.G[v][i].cap -= d
                    self.G[edge.to][edge.rev].cap += d
                    return d
        return 0

    def max_flow(self, s, t):
        flow = 0
        while True:
            self.level = self.bfs(s)
            if self.level[t] < 0:
                return flow
            self.iterator = [0 for i in range(self.V)]
            while True:
                f = self.dfs(s, t, float('inf'))
                if f == 0:
                    break
                flow += f

W = int(input())
N = int(input())
J = list(map(int,input().split()))
M = int(input())
C = list(map(int,input().split()))

ff = Dinic(N+M+2)
for i in range(1,N+1):
    ff.add_edge(0,i,J[i-1])

for i in range(N+1,N+M+1):
    ff.add_edge(i,N+M+1,C[i-(N+1)])
    Q = list(map(int,input().split()))
    counter = 1
    for j in range(1,N+1):
        if Q[0] == 0 or Q[counter]  != j:
            ff.add_edge(j, i, 10**8) 
        elif Q[0] != 0 and Q[counter]  == j:
            ff.add_edge(j, i, 0)
            counter += 1 if counter < Q[0] else 0


if ff.max_flow(0,N+M+1) >= W:
    print('SHIROBAKO')
else:
    print('BANSAKUTSUKITA')
0