acm icpc 2017 problem C Mission Improbable(二分图匹配)

Problem C
Mission Improbable
Time limit: 1 second

It is a sunny day in spring and you are about to meet Patrick, a close friend and former partner in crime. Patrick lost most of his money betting on programming contests, so he needs to pull off another job. For this he needs your help, even though you have retired from a life of crime. You are reluctant at first, as you have no desire to return to your old criminal ways, but you figure there is no harm in listening to his plan.

There is a shipment of expensive consumer widgets in a nearby warehouse and Patrick intends to steal as much of it as he can. This entails finding a way into the building, incapacitating security guards, passing through various arrays of laser beams – you know, the usual heist techniques. However, the heart of the warehouse has been equipped with a security system that Patrick cannot disable. This is where he needs your help.
The shipment is stored in large cubical crates, all of which have the same dimensions. The crates are stacked in neat piles, forming a three-dimensional grid. The security system takes pictures of the piles once per hour using three cameras: a front camera, a side camera and a top camera. The image from the front camera shows the height of the tallest pile in each column, the image from the side camera shows the height of the tallest pile in each row, and the image from the top camera shows whether or not each pile is empty. If the security system detects a change in any of the images, it sounds an alarm.
Once Patrick is inside, he will determine the heights of the piles and send them to you. Figure C.1 shows a possible layout of the grid and the view from each of the cameras.
Patrick wants to steal as many crates as possible. Since he cannot disable the security system, he plans to fool it by arranging the remaining crates into piles so that the next set of camera images are the same. In the above example, it is possible to steal nine crates. FigureC.2 shows one possible post-heist configuration that appears identical to the security system.

Patrick asks you to help him determine the maximum number of crates that can be stolen while leaving a configuration of crates that will fool the security system. Will you help him pull off this final job?

acm icpc 2017 problem C Mission Improbable(二分图匹配)_第1张图片
acm icpc 2017 problem C Mission Improbable(二分图匹配)_第2张图片
acm icpc 2017 problem C Mission Improbable(二分图匹配)_第3张图片
acm icpc 2017 problem C Mission Improbable(二分图匹配)_第4张图片
acm icpc 2017 problem C Mission Improbable(二分图匹配)_第5张图片
acm icpc 2017 problem C Mission Improbable(二分图匹配)_第6张图片
acm icpc 2017 problem C Mission Improbable(二分图匹配)_第7张图片
#匈牙利算法
def dfs(x):
    for i in range(m):
        if vis[i] == 0 and a[x][i] !=0 and max_row[x] == max_column[i]:
            vis[i] = 1
            if match[i] == -1 or dfs(match[i]):
                match[i] = x
                return 1
    return 0
if __name__ == '__main__':
    str = input().split()
    n = int(str[0]) #行数
    m = int(str[1]) #列数
    vis = [0 for i in range(m)]
    match = [-1 for j in range(m)]
    a = []
    max_row = []
    max_column = []
    for i in range(n):
        a.append(list(map(int, input().split())))
        max_row.append(max(a[i]))
        if i == n-1:
            max_column = max_column + [max(list(map(list, zip(*a)))[j]) for j in range(m)]
    #在保证俯视图不变的情况下,最多可以拿走的正方体的个数
    ans = sum([sum(a[i]) for i in range(n)]) - (n*m - sum(a,[]).count(0))
    #拿回一些正方体,使得每行和每列的最大高度不变,即使正视图和侧视图不变
    ans = ans - (sum(max_row) - (n - max_row.count(0))) - (sum(max_column) - (m - max_column.count(0)))
    #由于行最大值和列最大值有可能相等,这时只需在行和列的非零交叉点放一个最大值
    #也就是说之前拿回的正方体可能拿多了,跑一遍匈牙利算法,得出满足条件的行(按列也行),把多拿回的拿走
    for i in range(n):
        if dfs(i):
            ans = ans + max_row[i] - 1
    print(ans)
acm icpc 2017 problem C Mission Improbable(二分图匹配)_第8张图片
转载请注明出处。


你可能感兴趣的:(ACM,算法,匈牙利算法,二分图匹配,ACM)