題意:有n塊草地,一些奶牛在草地上吃草,草地間有m條路,一些草地上有避雨點,每個避雨點能容納的奶牛是有限的,給出通過每條路的時間,問最少需要多少時間能讓所有奶牛進入一個避雨點。
兩個避雨點間可以相互到達,所以必須要拆點,如果i-->j可以到達,加邊i->j+n,流量無窮大,當然i->i+n也必須有邊,,,
Folyd要用long 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; }