32102023-02-22 15:25:5912BotiJegesmedve (50)python3Wrong answer 34/50215ms18900 KiB
rows, cols = map(int, input().split())

ts = [[int(x) for x in input().split()] for _ in range(rows)]
fs = [[int(x) for x in input().split()] for _ in range(rows)]

x, y = None, None
for ri, r in enumerate(ts):
    for ci, c in enumerate(r):
        if c == -1:
            x, y = ri, ci
ts[x][y] = 1e9


def outside(ri, ci):
    return ri < 0 or ri >= rows or ci < 0 or ci >= cols


def dfs(ri, ci, t, mt):
    global visited
    global minmt
    global minmtc
    if outside(ri, ci) or visited[ri][ci] or ts[ri][ci] <= t:
        return 0
    # print("visit", ri, ci)
    visited[ri][ci] = True
    mt = min(mt, ts[ri][ci])
    if fs[ri][ci] >= mt - t:
        fs[ri][ci] -= mt - t
        return mt - t
    elif fs[ri][ci] > 0:
        if mt < minmt:
            minmt = mt
            minmtc = (ri, ci)
    ns = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    ns = [(ri + i, ci + j) for (i, j) in ns]
    ns = [n for n in ns if not outside(*n)]
    ns = [(ts[i][j], (i, j)) for (i, j) in ns]
    ns.sort(reverse=True)
    for (nt, n) in ns:
        f = dfs(*n, t, mt)
        if f > 0:
            return f
    return 0


t = 0
while True:
    visited = [[False] * cols for _ in range(rows)]
    minmt = 1e10
    minmtc = None
    f = dfs(x, y, t, 1e9)
    if f == 0:
        if minmtc:
            f = fs[minmtc[0]][minmtc[1]]
            fs[minmtc[0]][minmtc[1]] = 0
        else:
            break
    # print(t)
    # for r in ts:
    #     print(*r)
    # for r in fs:
    #     print(*r)
    t += f

print(t + 1)
SubtaskSumTestVerdictTimeMemory
base34/50
1Accepted0/018ms11944 KiB
2Accepted0/046ms12420 KiB
3Accepted3/332ms12288 KiB
4Accepted3/363ms12332 KiB
5Accepted3/321ms13144 KiB
6Accepted4/430ms13440 KiB
7Accepted3/320ms12820 KiB
8Accepted3/319ms13152 KiB
9Accepted3/319ms12900 KiB
10Accepted4/421ms13280 KiB
11Wrong answer0/4215ms15760 KiB
12Accepted4/4185ms15160 KiB
13Accepted4/4143ms14384 KiB
14Runtime error0/435ms18060 KiB
15Runtime error0/435ms18292 KiB
16Runtime error0/439ms18900 KiB