Python 的判题是不是有问题
查看原帖
Python 的判题是不是有问题
839480
StanMarsh楼主2023/9/16 01:27

试了 KM 算法、费用流,都是全 WA, 板子的正确性应该是没问题的。下面是我的建图的代码:

def min_cost_match(n, cost_matrix):
    # print(cost_matrix)
    N = 2*n + 2
    S, T = N-2, N-1
    mcf = MinCostFlow(N)
    for i in range(n):
        mcf.add_edge(S, i, 1, 0)
        mcf.add_edge(n+i, T, 1, 0)
    for i in range(n):
        for j in range(n):
            mcf.add_edge(i, n+j, 1, cost_matrix[i][j])
    flow, cost = mcf.flow(S, T)
    return cost


def solve(n, warehouses):
    """
    计算整理货物所需的最小代价

    :param n: int, 仓库的数量,范围为(1 <= n <= 150)
    :param warehouses: List[List[int]], 每个仓库的货物数量列表,其中每个货物数量的范围为(0 <= x <= 100)
    """

    # 仓库 货物
    cost_matrix = [[0]*n for _ in range(n)]
    xs = [0 for _ in range(n)]  # 每种货物重量之和
    for i in range(n):
        for j in range(n):
            xs[j] += warehouses[i][j]
    for i in range(n):
        for j in range(n):
            cost_matrix[i][j] = xs[j] - warehouses[i][j]
    # indexes = Munkres().compute(cost_matrix)
    # # print(indexes)
    # total_cost = 0
    # for r, c in indexes:
    #     total_cost += cost_matrix[r][c]
    # return total_cost
    return min_cost_match(n, cost_matrix)


n = int(input().strip())
warehouses = [list(map(int, input().split())) for _ in range(n)]
print(solve(n, warehouses))

2023/9/16 01:27
加载中...