Ombrophobic Bovines poj 2391 二分+拆点+最大流sap模板

Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13342 Accepted: 2936

Description

FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter. 

The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction. 

Some of the farm's fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse. 

Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.

Input

* Line 1: Two space-separated integers: F and P 

* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field i. 

* Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.

Output

* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".

Sample Input

3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120

Sample Output

110

Hint

OUTPUT DETAILS: 

In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.

Source



这个题目遇到的时候虽然知道是图论,但是真是没思路,后来终于捋情了怎么做了。

首先这是个最大流问题,必须要拆点来做。例如当1-2为1,2-3为1时,1-3就有路可以走了,但是我们不允许1-3,所以是1-2‘和2''-3才可以,要求进行拆点。还有就是我们

要用floyd求出每两个点之间最短的距离,用findmax()求出最大的距离maxx,在0和maxx之间进行二分查找,使其在这个最短距离范围内的路径上的两个端点在网络流的图中相连,容量是无穷。那么我们应该咋么来建图呢??



就是要这么建出来,然后用sap算法求出最大流,看是不是等于奶牛总数量,另外人家求得是距离,可不是奶牛数量的,如果是奶牛,那么二分查找就没有意义了


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define inff 200000000007
#define inf 0x3f3f3f
#define M 1007
struct node
{

    int v,w,next;

}edge[500000];
int dis[2000],gap[2000],head[2000],nodes;
int sourse,sink,nn;
int cow[M][2],n,s,t,count;
long long g[M][M],maxx;
void init()
{
    nodes=0;
    maxx=0;
    count=0;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    if(i==j)
    g[i][j]=0;
    else
    g[i][j]=inff;
}
void floyd()
{
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    if(g[i][k]+g[k][j]<g[i][j])
    g[i][j]=g[i][k]+g[k][j];
}
long long findmax()
{

    long long maxx=0;
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if((i!=j)&&(g[i][j]!=inff))
            maxx=max(maxx,g[i][j]);
        }

    }
    return maxx;
}
//模板
void addedge(int u,int v,int w)
{
    edge[nodes].v=v;
    edge[nodes].w=w;
    edge[nodes].next=head[u];
    head[u]=nodes++;
    edge[nodes].v=u;
    edge[nodes].w=0;
    edge[nodes].next=head[v];
    head[v]=nodes++;
}
//模板
int dfs(int src,int aug)
{
    if(src==sink)return aug;
    int left=aug,mindis=nn;
    for(int j=head[src];j!=-1;j=edge[j].next)
    {
    	int v=edge[j].v;
    	if(edge[j].w)
        {
           if(dis[v]+1==dis[src])
           {
               int minn=min(left,edge[j].w);
               minn=dfs(v,minn);
               edge[j].w-=minn;
               edge[j^1].w+=minn;
               left-=minn;
               if(dis[sourse]>=nn)return aug-left;
               if(left==0)break;
           }
           if(dis[v]<mindis)
           mindis=dis[v];
        }
    }

        if(left==aug)
        {
            if(!(--gap[dis[src]]))dis[sourse]=nn;
            dis[src]=mindis+1;
            gap[dis[src]]++;
        }
        return aug-left;
}
//模板
int sap(int s,int e)
{
    int ans=0;
	nn=e+1;
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    gap[0]=nn;
    sourse=s;
    sink=e;
    while(dis[sourse]<nn)
    ans+=dfs(sourse,inf);
    return ans;
}
bool search(long long mid)//二分查找
{

    s=0,t=n+n+1,nodes=0;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++)
    {
        addedge(s,i,cow[i][0]);
        addedge(i+n,t,cow[i][1]);
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(g[i][j]<=mid)
                addedge(i,j+n,inf);
    if(sap(s,t)==count)
    return true;
    return false;
}
long long solve()
{
    long long l=0,r=maxx,mid,ans=-1;
    while(l<=r)
    {
        mid=(l+r)>>1;//  相当于除以2
        if(search(mid))
        {
            ans=mid;
            r=mid-1;
        }
        else
        l=mid+1;
    }
    return ans;
}
int main()
{
    int i,j,k,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int a,b;
        long long c;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&cow[i][0],&cow[i][1]);
            count=count+cow[i][0];
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%lld",&a,&b,&c);
            if(g[a][b]>c)
            g[a][b]=g[b][a]=c;
        }
        floyd();
        maxx=findmax();
        //printf("maxx      %lld\n",maxx);
        printf("%lld\n",solve());








    }
    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值