【POJ 2112 Optimal Milking】网络流 & 二分 & floyd

来源:互联网 发布:静态网页源码 编辑:程序博客网 时间:2024/06/06 20:17

Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 18478 Accepted: 6594
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

题意: 有 k 台挤奶机器和 c 头奶牛,每台挤奶机器可容纳 m 头奶牛同时挤奶,现在给出dis[K + C][K + C] 的矩阵,dis[i][j]若不为0则表示第i个物体到第j个物体之 间有路,dis[i][j]就是该路的长度。(1 <= K <= 30,1 <= C <= 200) 现在问你怎么安排这C头牛到K台机器挤奶,使得需要 走最长路程到挤奶机器的奶牛所走的路程最少,求出 这个最小值。

思路 : 网络流模型,用 floyd 算法预处理出每头奶牛到每个挤奶器的最短距离,没头奶牛最终只能到达一台挤奶器,每个挤奶器最终可容纳 m 头奶牛,把奶牛看成网络中的流,添加一个源点到每头奶牛,流量为 1,添加一个汇点到每台挤奶其流量为 m,二分最大距离,把小于最大距离的边,建入网络流的图中,看其最大流是否是C

AC代码:

#include<cstdio>#include<cmath>#include<deque>#include<queue>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int MAX = 5e5 + 10;const int INF = 1e9 + 7;typedef long long LL;int head[MAX],d[MAX],nl;int read(){    int x = 0, y = 1;    char s = getchar();    while(s < '0' || s > '9') { if(s == '-') y = -1;s = getchar();}    while(s >= '0' && s <= '9') x = x * 10 + s - '0',s = getchar();    return x * y;}struct node{    int from,to,cap,flow,next;}st[MAX];void add(int x,int y,int z){    st[nl] = node{x,y,z,0,head[x]},head[x] = nl++;    st[nl] = node{y,x,0,0,head[y]},head[y] = nl++;}bool BFS(int s,int e){    queue <int> q;    memset(d,-1,sizeof d);    d[s] = 0,q.push(s);    while(!q.empty()){        int o = q.front();        q.pop();        for(int i = head[o]; i != -1; i = st[i].next){            node& w = st[i];            if(d[w.to] == -1 && w.cap - w.flow > 0){                d[w.to] = d[o] + 1;                if(w.to == e) return true;                q.push(w.to);            }        }    }    return false;}int DFS(int s,int x,int e){    if(s == e || x == 0) return x;    int flow = 0,o;    for(int i = head[s]; i != -1; i = st[i].next){        node& w = st[i];        if(d[w.to] == d[s] + 1 && (o = DFS(w.to,min(x,w.cap - w.flow),e)) > 0){            x -= o;            flow += o;            w.flow += o;            st[i^1].flow -= o;            if(x == 0) break;        }    }    return flow;}int Maxflow(int s,int e){    int flow = 0;    while(BFS(s,e)){        flow += DFS(s,INF,e);    }    return flow;}int dis[510][510],k,c,m,md,s,e;void foyld(){    for(int o = 1; o <= k + c; o++)        for(int i = 1; i <= k + c; i++)            for(int j = 1; j <= k + c; j++)                if(dis[i][j] > dis[i][o] + dis[o][j])                    dis[i][j] = dis[i][o] + dis[o][j];}void init(){    int nl = 0;    memset(head,-1,sizeof head);    for(int i = 1; i <= k; i++){        for(int j = k + 1; j <= k + c; j++){            if(dis[i][j] <= md){                add(i,j,1);            }        }    }    for(int i = 1; i <= k; i++)        add(s,i,m);    for(int i = 1 + k; i <= k + c; i++)        add(i,e,1);}int main(){    while(~scanf("%d %d %d",&k,&c,&m)){        s = 0,e = k + c + 1;        for(int i = 1; i <= k + c; i++)            for(int j = 1; j <= k + c; j++){                scanf("%d",&dis[i][j]);                if(i != j && !dis[i][j])                    dis[i][j] = 600000;            }            foyld();            int l = 0,r = INF,ans = 0;            while(l <= r){                md = (l + r) >> 1;                init();                if(Maxflow(s,e) == c)                    ans = md,r = md - 1;                else                    l = md + 1;            }            printf("%d\n",ans);    }    return 0;}
阅读全文
1 0