Poj 2391 (Floyd + maximum stream + binary)

Source: Internet
Author: User

There are n lawns, some cows graze on the grass, there are m roads between the lawns, and some lawns have shelter from rain. Each shelter is limited, the minimum time required to allow all cows to enter a rain shelter is given through each route.

The two rainproof points can arrive at each other, so they must be split. If I --> j can arrive, edge I-> j + n, traffic is infinite, of course, I-> I + n must also have edges ,,,

Folyd should use long ,,,,,

 

 

 

 

#include<stdio.h>#include<string.h>const int N=410;const int inf=0x3fffffff;int gap[N],dis[N],head[N],num,start,end,ans,n;__int64 map[N][N];struct edge{int st,ed,flow,next;}E[90000];struct node{int w1,w2;}P[210];void addedge(int x,int y,int w){E[num].st=x;E[num].ed=y;E[num].flow=w;E[num].next=head[x];head[x]=num++;E[num].st=y;E[num].ed=x;E[num].flow=0;E[num].next=head[y];head[y]=num++;}int dfs(int u,int minflow){if(u==end)return minflow;int i,v,f,flow=0,min_dis=ans-1;for(i=head[u];i!=-1;i=E[i].next){v=E[i].ed;if(E[i].flow>0){if(dis[v]+1==dis[u]){f=dfs(v,E[i].flow>minflow-flow?minflow-flow:E[i].flow);E[i].flow-=f;E[i^1].flow+=f;flow+=f;if(flow==minflow)break;if(dis[start]>=ans)return flow;}min_dis=min_dis>dis[v]?dis[v]:min_dis;}}if(flow==0){if(--gap[dis[u]]==0)dis[start]=ans;dis[u]=min_dis+1;gap[dis[u]]++;}return flow;}int isap(){int maxflow=0;memset(dis,0,sizeof(dis));memset(gap,0,sizeof(gap));gap[0]=ans;while(dis[start]<ans)maxflow+=dfs(start,inf);//printf("maxflow=%d\n",maxflow);return maxflow;}void makemap(__int64 D){memset(head,-1,sizeof(head));num=0;int i,j;for(i=1;i<=n;i++){addedge(i,i+n,inf);addedge(start,i,P[i].w1);for(j=i+1;j<=n;j++){if(map[i][j]<=D){addedge(i,j+n,inf);addedge(j,i+n,inf);}}addedge(i+n,end,P[i].w2);}}int main(){int i,j,k,x,y,w,m,sum;while(scanf("%d%d",&n,&m)!=-1){start=0;end=n*2+1;ans=end+1;sum=0;for(i=1;i<=n;i++){for(j=i+1;j<=n;j++)map[i][j]=map[j][i]=1000000000000;map[i][i]=0;}for(i=1;i<=n;i++){scanf("%d%d",&P[i].w1,&P[i].w2);sum+=P[i].w1;}for(i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&w);if(map[x][y]>w)map[x][y]=map[y][x]=w;}for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++)if(map[i][j]>map[i][k]+map[k][j])map[i][j]=map[i][k]+map[k][j];__int64 L,R,mid,flag;L=0;flag=-1;R=0;for(i=1;i<=n;i++)for(j=i+1;j<=n;j++)if(R<map[i][j]&&map[i][j]<1000000000000)R=map[i][j];while(L<=R){mid=(L+R)/2;makemap(mid);//printf("mid=%d\n",mid);if(isap()==sum){flag=mid;R=mid-1;}else L=mid+1;}printf("%I64d\n",flag);}return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.