Submission #6901873


Source Code Expand

Copy
# -*- coding: utf-8 -*-

import sys
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

class MinCostFlow:
    INF = 10**18

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

    def add_edge(self, fr, to, cap, cost):
        G = self.G
        G[fr].append([to, cap, cost, len(G[to])])
        G[to].append([fr, 0, -cost, len(G[fr])-1])

    def flow(self, s, t, f):
        N = self.N; G = self.G
        INF = MinCostFlow.INF

        res = 0
        H = [0]*N
        prv_v = [0]*N
        prv_e = [0]*N

        while f:
            dist = [INF]*N
            dist[s] = 0
            que = [(0, s)]

            while que:
                c, v = heappop(que)
                if dist[v] < c:
                    continue
                for i, (w, cap, cost, _) in enumerate(G[v]):
                    if cap > 0 and dist[w] > dist[v] + cost + H[v] - H[w]:
                        dist[w] = r = dist[v] + cost + H[v] - H[w]
                        prv_v[w] = v; prv_e[w] = i
                        heappush(que, (r, w))
            if dist[t] == INF:
                return -1

            for i in range(N):
                H[i] += dist[i]

            d = f; v = t
            while v != s:
                d = min(d, G[prv_v[v]][prv_e[v]][1])
                v = prv_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = G[prv_v[v]][prv_e[v]]
                e[1] -= d
                G[v][e[3]][1] += d
                v = prv_v[v]
        return res

R, G, B = MAP()
N = R + G + B

# 最小費用流
mcf = MinCostFlow(1006)
# 始点からRGBの移動前
mcf.add_edge(1001, 1002, R, 0)
mcf.add_edge(1001, 1003, G, 0)
mcf.add_edge(1001, 1004, B, 0)

# 0~1000の頂点番号は座標位置と一致させてある
for v in range(1001):
    # R => 各座標
    mcf.add_edge(1002, v, 1, abs(400-v))
    # G => 各座標
    mcf.add_edge(1003, v, 1, abs(500-v))
    # B => 各座標
    mcf.add_edge(1004, v, 1, abs(600-v))
    # 各座標 => 終点
    mcf.add_edge(v, 1005, 1, 0)

print(mcf.flow(1001, 1005, N))

Submission Info

Submission Time
Task D - マーブル
User Coki628
Language Python3 (3.4.3)
Score 40
Code Size 2712 Byte
Status
Exec Time 2104 ms
Memory 4596 KB

Test Cases

Set Name Score / Max Score Test Cases
sub1 10 / 10 sample_01_ABC.txt, test_ABC_01.txt, test_ABC_02.txt, test_ABC_03.txt, test_ABC_04.txt, test_ABC_05.txt, test_ABC_06.txt, test_ABC_07.txt, test_ABC_08.txt, test_ABC_09.txt, test_ABC_10.txt, test_ABC_11.txt, test_ABC_12.txt, test_ABC_13.txt, test_ABC_14.txt, test_ABC_15.txt, test_ABC_16.txt, test_ABC_17.txt, test_ABC_18.txt, test_ABC_19.txt, test_ABC_20.txt, test_ABC_21.txt, test_ABC_22.txt, test_ABC_23.txt, test_ABC_24.txt, test_ABC_25.txt, test_ABC_26.txt, test_ABC_27.txt, test_ABC_28.txt
sub2 30 / 30 sample_01_ABC.txt, sample_02_BC.txt, test_ABC_01.txt, test_ABC_02.txt, test_ABC_03.txt, test_ABC_04.txt, test_ABC_05.txt, test_ABC_06.txt, test_ABC_07.txt, test_ABC_08.txt, test_ABC_09.txt, test_ABC_10.txt, test_ABC_11.txt, test_ABC_12.txt, test_ABC_13.txt, test_ABC_14.txt, test_ABC_15.txt, test_ABC_16.txt, test_ABC_17.txt, test_ABC_18.txt, test_ABC_19.txt, test_ABC_20.txt, test_ABC_21.txt, test_ABC_22.txt, test_ABC_23.txt, test_ABC_24.txt, test_ABC_25.txt, test_ABC_26.txt, test_ABC_27.txt, test_ABC_28.txt, test_BC_29.txt, test_BC_30.txt, test_BC_31.txt, test_BC_32.txt, test_BC_33.txt, test_BC_34.txt, test_BC_35.txt, test_BC_36.txt, test_BC_37.txt, test_BC_38.txt, test_BC_39.txt, test_BC_40.txt, test_BC_41.txt, test_BC_42.txt, test_BC_43.txt, test_BC_44.txt, test_BC_45.txt, test_BC_46.txt, test_BC_47.txt, test_BC_48.txt, test_BC_49.txt, test_BC_50.txt, test_BC_51.txt, test_BC_52.txt, test_BC_53.txt, test_BC_54.txt, test_BC_55.txt
All 0 / 60 sample_01_ABC.txt, sample_02_BC.txt, sample_03_C.txt, test_ABC_01.txt, test_ABC_02.txt, test_ABC_03.txt, test_ABC_04.txt, test_ABC_05.txt, test_ABC_06.txt, test_ABC_07.txt, test_ABC_08.txt, test_ABC_09.txt, test_ABC_10.txt, test_ABC_11.txt, test_ABC_12.txt, test_ABC_13.txt, test_ABC_14.txt, test_ABC_15.txt, test_ABC_16.txt, test_ABC_17.txt, test_ABC_18.txt, test_ABC_19.txt, test_ABC_20.txt, test_ABC_21.txt, test_ABC_22.txt, test_ABC_23.txt, test_ABC_24.txt, test_ABC_25.txt, test_ABC_26.txt, test_ABC_27.txt, test_ABC_28.txt, test_BC_29.txt, test_BC_30.txt, test_BC_31.txt, test_BC_32.txt, test_BC_33.txt, test_BC_34.txt, test_BC_35.txt, test_BC_36.txt, test_BC_37.txt, test_BC_38.txt, test_BC_39.txt, test_BC_40.txt, test_BC_41.txt, test_BC_42.txt, test_BC_43.txt, test_BC_44.txt, test_BC_45.txt, test_BC_46.txt, test_BC_47.txt, test_BC_48.txt, test_BC_49.txt, test_BC_50.txt, test_BC_51.txt, test_BC_52.txt, test_BC_53.txt, test_BC_54.txt, test_BC_55.txt, test_C_56.txt, test_C_57.txt, test_C_58.txt, test_C_59.txt, test_C_60.txt, test_C_61.txt, test_C_62.txt, test_C_63.txt, test_C_64.txt, test_C_65.txt, test_C_66.txt, test_C_67.txt, test_C_68.txt, test_C_69.txt, test_C_70.txt, test_C_71.txt, test_C_72.txt, test_C_73.txt, test_C_74.txt, test_C_75.txt, test_C_76.txt, test_C_77.txt, test_C_78.txt, test_C_79.txt, test_C_80.txt, test_C_81.txt, test_C_82.txt
Case Name Status Exec Time Memory
sample_01_ABC.txt 59 ms 4596 KB
sample_02_BC.txt 213 ms 4596 KB
sample_03_C.txt 2104 ms 4596 KB
test_ABC_01.txt 58 ms 4596 KB
test_ABC_02.txt 61 ms 4596 KB
test_ABC_03.txt 61 ms 4596 KB
test_ABC_04.txt 69 ms 4596 KB
test_ABC_05.txt 50 ms 4596 KB
test_ABC_06.txt 59 ms 4596 KB
test_ABC_07.txt 62 ms 4596 KB
test_ABC_08.txt 66 ms 4596 KB
test_ABC_09.txt 55 ms 4596 KB
test_ABC_10.txt 57 ms 4596 KB
test_ABC_11.txt 76 ms 4596 KB
test_ABC_12.txt 54 ms 4596 KB
test_ABC_13.txt 54 ms 4596 KB
test_ABC_14.txt 60 ms 4596 KB
test_ABC_15.txt 56 ms 4596 KB
test_ABC_16.txt 60 ms 4596 KB
test_ABC_17.txt 70 ms 4596 KB
test_ABC_18.txt 69 ms 4596 KB
test_ABC_19.txt 68 ms 4596 KB
test_ABC_20.txt 59 ms 4596 KB
test_ABC_21.txt 37 ms 4596 KB
test_ABC_22.txt 54 ms 4596 KB
test_ABC_23.txt 52 ms 4596 KB
test_ABC_24.txt 50 ms 4596 KB
test_ABC_25.txt 66 ms 4596 KB
test_ABC_26.txt 68 ms 4596 KB
test_ABC_27.txt 70 ms 4596 KB
test_ABC_28.txt 84 ms 4596 KB
test_BC_29.txt 155 ms 4596 KB
test_BC_30.txt 156 ms 4596 KB
test_BC_31.txt 242 ms 4596 KB
test_BC_32.txt 255 ms 4596 KB
test_BC_33.txt 327 ms 4596 KB
test_BC_34.txt 327 ms 4596 KB
test_BC_35.txt 200 ms 4596 KB
test_BC_36.txt 273 ms 4596 KB
test_BC_37.txt 267 ms 4596 KB
test_BC_38.txt 266 ms 4596 KB
test_BC_39.txt 196 ms 4596 KB
test_BC_40.txt 346 ms 4596 KB
test_BC_41.txt 318 ms 4596 KB
test_BC_42.txt 405 ms 4596 KB
test_BC_43.txt 239 ms 4596 KB
test_BC_44.txt 192 ms 4596 KB
test_BC_45.txt 192 ms 4596 KB
test_BC_46.txt 322 ms 4596 KB
test_BC_47.txt 250 ms 4596 KB
test_BC_48.txt 385 ms 4596 KB
test_BC_49.txt 326 ms 4596 KB
test_BC_50.txt 317 ms 4596 KB
test_BC_51.txt 351 ms 4596 KB
test_BC_52.txt 163 ms 4596 KB
test_BC_53.txt 174 ms 4596 KB
test_BC_54.txt 193 ms 4596 KB
test_BC_55.txt 517 ms 4596 KB
test_C_56.txt 1116 ms 4596 KB
test_C_57.txt 1953 ms 4596 KB
test_C_58.txt 2104 ms 4596 KB
test_C_59.txt 1778 ms 4596 KB
test_C_60.txt 900 ms 4596 KB
test_C_61.txt 1539 ms 4596 KB
test_C_62.txt 1442 ms 4596 KB
test_C_63.txt 1542 ms 4596 KB
test_C_64.txt 1254 ms 4596 KB
test_C_65.txt 1708 ms 4596 KB
test_C_66.txt 1647 ms 4596 KB
test_C_67.txt 1844 ms 4596 KB
test_C_68.txt 1015 ms 4596 KB
test_C_69.txt 1219 ms 4596 KB
test_C_70.txt 1465 ms 4596 KB
test_C_71.txt 1948 ms 4596 KB
test_C_72.txt 2104 ms 4596 KB
test_C_73.txt 2031 ms 4596 KB
test_C_74.txt 1946 ms 4596 KB
test_C_75.txt 1844 ms 4596 KB
test_C_76.txt 2103 ms 4596 KB
test_C_77.txt 2104 ms 4596 KB
test_C_78.txt 2104 ms 4596 KB
test_C_79.txt 1004 ms 4596 KB
test_C_80.txt 1066 ms 4596 KB
test_C_81.txt 1177 ms 4596 KB
test_C_82.txt 2104 ms 4596 KB