POJ 2112 Optimal Milking 最大流 二分答案

本文介绍了一种解决最优挤奶机分配问题的算法。该问题的目标是在给定K个挤奶机和C头奶牛的情况下,通过二分查找找到一种分配方案,使得每头奶牛到挤奶机的最大距离最小,同时确保每个挤奶机不超过M头奶牛使用。通过建立最大流模型并使用Dinic算法求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有K个能挤M头奶牛的挤奶机和C头奶牛,告诉一些挤奶机和奶牛间距离,求最优分配方案使最大距离最小。

显然是二分答案。接下来考虑如何判定。
为了限制挤奶机发给m头牛,以及一头牛只能分配给1台挤奶机,源点连向挤奶机,容量m,牛连向汇点,容量1,距离小于二分的最大距离的 挤奶机-牛 对,连边,容量1(其实设成多大都无所谓的),跑出来最大流=c(满流?)说明方案可行。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,j,k) for(i=j;i<=k;++i)
using namespace std;
const int inf = 0x3f3f3f, N = 235, M = 15000;

int level[N], cnt, cur[N], v[M], w[M], p[M], h[N], q[M], s, t, a[N][N];
void add(int a, int b, int c) {
    p[++cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt;
    p[++cnt] = h[b]; v[cnt] = a; w[cnt] = 0; h[b] = cnt;
}

bool bfs() {
    int f = 0, r = 0, u, i;
    memset(level, -1, sizeof level);
    q[r++] = s; level[s] = 1;
    while (f < r) {
        u = q[f++];
        for (i = h[u]; i; i = p[i]) {
            if (w[i] && level[v[i]] == -1) {
                level[v[i]] = level[u] + 1;
                q[r++] = v[i];
            }
        }
    }
    return level[t] != -1;
}

int dfs(int u, int low) {
    int i, tmp = 0, res = 0;
    if (u == t) return low;
    for (i = cur[u]; i && res < low; i = p[i]) {
        if (w[i] && level[v[i]] == level[u] + 1) {
            tmp = dfs(v[i], min(w[i], low - res));
            w[i] -= tmp; w[i ^ 1] += tmp; res += tmp;
            if (w[i]) cur[u] = i;
        }
    }
    if (!res) level[u] = -1;
    return res;
}

int dinic() {
    int ans = 0, i;
    while (bfs()) {
        for (i = s; i <= t; ++i) cur[i] = h[i];
        ans += dfs(s, inf);
    }
    return ans;
}


int main() {
    int i, j, p, c, mid, l, r, n, m, k, ans;
    while (scanf("%d%d%d", &k, &c, &m) == 3) {
        n = k + c; s = 0; t = n + 1;
        FOR(i,1,n) FOR(j,1,n) {
            scanf("%d", &a[i][j]);
            if (!a[i][j]) a[i][j] = inf;
        }
        FOR(p,1,n) FOR(i,1,n) FOR(j,1,n)
            a[i][j] = min(a[i][j], a[i][p] + a[p][j]);
        l = 1; r = 200 * n;
        while (l <= r) {
            mid = l + r >> 1; cnt = 1;
            memset(h, 0, sizeof h);
            FOR(i,1,k) add(s, i, m);
            FOR(i,1,c) add(i + k, t, 1);
            FOR(i,1,k) FOR(j,k+1,n)
                if (a[i][j] <= mid)
                    add(i, j, 1);
            if (dinic() == c) ans = mid, r = mid - 1;
            else l = mid + 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}

Optimal Milking

Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 15303 Accepted: 5443
Case Time Limit: 1000MS

Description

FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C.

Each milking point can “process” at most M (1 <= M <= 15) cows each day.

Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.

Input

  • Line 1: A single line with three space-separated integers: K, C, and M.

  • Lines 2.. …: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.

Output

A single line with a single integer that is the minimum possible total distance for the furthest walking cow.

Sample Input

2 3 2
0 3 2 1 1
3 0 3 2 0
2 3 0 1 0
1 2 1 0 2
1 0 0 2 0

Sample Output

2

Source

USACO 2003 U S Open

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值