poj 2112 最大流之Dinic算法+Floyd

60 篇文章 0 订阅

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



题意:

安排C头牛到K个机器那里去挤奶,每一个机器最多可以挤奶的挤M头牛

机器的编号为1~k,牛的编号为k+1~k+c

给出一个(k+c )*(k+c )的矩阵,表示某编号到某编号的距离,0表示行不通

求解所有路径中最长的路径中的最短路径

题解:

因为数据量比较小,先用Floyd算法计算所有点之间的最短距离

然后用Dinic算法求最大流;

搜索最大距离的最小值采用二分法进行


Dinic算法思想:

首先构造残留网络  层次网络

然后从源点开始DFS,按照需要从小到大的顺序进行访问顶点

然后找到一条增广路进行增广即可(一直使用深搜的思想进行访问顶点)


连续最短增广路和最短增广路算法区别:

Dinic:算法一次DFS就可以实现多次增广

最短增广路算法:每个阶段执行完一次BFS增广后,要重新启动BFS从源点开始寻找一条增广路



#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXN 300
#define INF 0x3f3f3f3f
int dis[MAXN][MAXN];
int map[MAXN][MAXN];
bool sign[MAXN][MAXN];
bool used[MAXN];
int k,c,m,n;

void Build_Graph(int min_max)
{
    memset(map,0,sizeof(map));
    //源点可以到达所有的点
    for(int i=k+1;i<=n;i++)
        map[0][i]=1;
    //记录每个机器可以挤多少头奶牛
    for(int i=1;i<=k;i++)
        map[i][n+1]=m;

    //构造容量网络map(机器到奶牛)
    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));
    int que[200*MAXN]={0};
    que[0]=0;
    used[0]=1;

    int s=0,e=1;
    while(s<e)
    {
        for(int i=0;i<=n+1;i++){
            if(!used[i]&&map[que[s]][i]){
                que[e++]=i;
                used[i]=1;
                sign[que[s]][i]=1;
            }
        }
        s++;
    }

    //判断汇点是否在层次网络中
    if(used[n+1])
        return true;
    else
        return false;
}

int DFS(int v,int sum)//增广
{
    int i,s,t;
    if(v==n+1)
        return sum;
    s=sum;

    for(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 main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&k,&c,&m)!=EOF)
    {
        n=k+c;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&dis[i][j]);
                if(!dis[i][j])
                    dis[i][j]=INF;
            }
        }

        for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);

        int l=0,r=10000,mid,ans;
        while(l<r)
        {
            mid=(l+r)/2;
            ans=0;
            Build_Graph(mid);
            while(BFS())
                ans+=DFS(0,INF);
            if(ans>=c)
                r=mid;
            else
                l=mid+1;
        }
        printf("%d\n",r);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值