Optimal Milking POJ - 2112 (flody + 二分 + 网络流)

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

 

思路:先看题,没有什么一条边只能走几次这种限制,还要找最短距离,数组又给你了。。。Flody搞起,再看要求最大值最小,祖传二分,每次建边的时候长度小于等于mid的才建立,且容量为1,s连所有奶牛,容量为1,所有机器连t,容量为M,跑Dinic,若最大流等于奶牛数,则可行

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
int INF = 1e8;
const int maxn = 1e5 + 50;
struct Edge
{
    int to, next, w;    
} edge[maxn];
int k, head[maxn];

void add(int a, int b, int c){
    edge[k].to = b;
    edge[k].w = c;
    edge[k].next = head[a];
    head[a] = k++;
    

    edge[k].to = a;
    edge[k].w = 0;
    edge[k].next = head[b];
    head[b] = k++;
}

int dis[maxn], cur[maxn];

int bfs(int s, int t){
    for(int i = 0; i <= t; i++){
        dis[i] = 0; 
    }
    dis[s] = 1;
    queue<int> que;
    que.push(s);
    while(que.size()){
        int u = que.front();
        que.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            int to = edge[i].to;
            int w = edge[i].w;
            if(w && !dis[to]){
                dis[to] = dis[u] + 1;
                if(to == t){
                    return 1;
                }
                que.push(to);
            }
        }
    }
    return 0;
}

int dfs(int u, int maxf, int t){
    if(u == t){
        return maxf;
    }

    int ret = 0;
    for(int i = cur[u]; i != -1; i = edge[i].next){
        cur[u] = i;
        int to = edge[i].to;
        int w = edge[i].w;
        if(dis[to] == dis[u] + 1 && w){
            int mi = min(maxf - ret, w);
            w = dfs(to, mi, t);
            edge[i].w -= w;
            edge[i ^ 1].w += w;
            ret += w;
            if(ret == maxf){
                return ret;
            }
        }
    }
    return ret;
}

int Dinic(int s, int t){
    int ans = 0;
    while(bfs(s, t)){
        for(int i = 0; i <= t; i++){
            cur[i] = head[i];
        }
        ans += dfs(s, INF, t);
    }
    return ans;
}

int mp[255][255];
int main(int argc, char const *argv[])
{
    int n, m, sum;
    scanf("%d%d%d", &n, &m, &sum);
    for(int i = 1; i <= n + m; i++){
        for(int j = 1; j <= n + m; j++){
            scanf("%d", &mp[i][j]);
            if(mp[i][j] == 0 && i != j){
                mp[i][j] = INF;
            }
        }
    }
    int ma = 0;
    for(int k = 1; k <= n + m; k++){
        for(int i = 1; i <= n + m; i++){
            for(int j = 1; j <= n + m; j++){
                mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
                if(mp[i][j] < INF){
                    ma = max(ma, mp[i][j]);
                }
            }
        }
    }

    int s = 0, t = n + m + 1;
    int le = 0, ri = ma;
    int ans = INF;
    while(le <= ri){
        int mid = (le + ri) >> 1;
        k = 0;
        for(int i = 0; i <= t; i++){
            head[i] = -1;
        }
        for(int i = 1; i <= m; i++){
            add(s, i + n, 1);
        }
        for(int i = 1; i <= n; i++){
            add(i, t, sum);
        }

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(mp[i + n][j] <= mid){
                    add(i + n, j, 1);
                }
            }
        }
        if(Dinic(s, t) == m){
            ans = min(ans, mid);
            ri = mid - 1;
        } else{
            le = mid + 1;
        }
    }

    printf("%d\n", ans);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值