Poj Optimal Milking 2112 floyd+dinic+二分

博客围绕Poj Optimal Milking 2112问题展开,该问题是安排C头奶牛到挤奶器,使奶牛最大路程最小。求解方法为先通过floyd算法求任意两点最短路径,再用dinic算法求最大流,最后用二分法搜索最大距离最小值,并给出了示例输入输出。

Poj Optimal Milking 2112 floyd+dinic+二分

​ 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

分析:

​ 本题要安排C头奶牛到某个挤奶器,使得每头奶牛需要走的路程中最大路程的距离最小。

​ 本题的求解方法:先用floyd算法求出能达到的任意两点之间的最短路径,然后用dinic算法求最大流;搜索最大距离的最小值采用二分法进行。

代码如下:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = 300;
int dis[MAX][MAX];
int map[MAX][MAX];
bool sign[MAX][MAX];
bool used[MAX];
int K,C,n,M;
queue<int > q;
void floyd() {
    for(int k = 1;k <= n;k++) {
        for(int i = 1;i <= n;i++) {
            if(dis[i][k] == INF) continue;
            for(int j = 1;j <= n;j++) {
                if(dis[k][j] == INF) continue;
                dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
            }
        }
    }
}
void Build_Graph(int min_max) {
    memset(map,0,sizeof(map));
    for(int i = 1;i <= n;i++) {
        if(i<=K) map[i][n+1] = M;
        else map[0][i] = 1;
    }
    for(int i = K+1;i<=n;i++) {
        for(int j = 1;j <=K;j++) {
            if(dis[i][j] <= min_max) map[i][j] = 1;
        }
    }
}
bool bfs() {
    memset(used,0,sizeof(used));
    memset(sign,0,sizeof(sign));
    while(!q.empty()) q.pop();
    used[0] = 1;
    q.push(0);
    while(!q.empty()) {
        for(int i = 0;i <= n+1;i++) {
            if(!used[i] && map[q.front()][i]) {
                q.push(i);
                used[i] = 1;
                sign[q.front()][i] = 1;
            }
        }
        q.pop();
    }
    if(used[n+1]) return true;
    else return false;
}
int dfs(int v,int sum) {
    int s,t;
    if(v == n+1) return sum;
    s = sum;
    for(int i = 0;i <= n+1;i++) {
        if(sign[v][i]) {
            t = dfs(i,min(map[v][i],sum));
            map[v][i] -= t;
            map[i][v] += t;
            sum -= t;
        }
    }
    return s - sum;
}
int dinic() {
    int sum = 0;
    while(bfs()) sum+=dfs(0,INF);
    return sum;
}
int main() {
    int l,r,mid,ans;
    scanf("%d%d%d",&K,&C,&M);
    n = K+C;
    memset(dis,0x3f,sizeof(dis));
    for(int i = 1;i <= n;i++) {
        for(int j = 1;j <= n;j++) {
            scanf("%d",&dis[i][j]);
            if(i !=j && dis[i][j] == 0) dis[i][j] = INF;
        }
    }
    floyd();
    l = 0,r = 10000;
    while(l<r) {
        mid = (l+r)/2;
        Build_Graph(mid);
        ans = dinic();
        if(ans>=C) r = mid;
        else l = mid+1;
    }
    printf("%d\n",r);
    return 0;
}

转载于:https://www.cnblogs.com/pot-a-to/p/10957291.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值