Ombrophobic Bovines
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11558 | Accepted: 2566 |
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.
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.
* 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
本题思路同poj2112,但需要拆点。因为本题是一个典型的最大最小问题,所以可以二分答案,但在二分时,必须要保证所建的的图是二部图(将源点和汇点分成两部分),因为在源点和源点或汇点和汇点之间若存在边的话,则无法保证每次增广时得到的路径的时间不大于当前枚举的最长时间,如对于下面的图,存在一条增广路径s->1->2->3->t,所以当枚举maxtime=60时即可以让所有的牛到达遮雨棚,但这条路径显然不符合题意。
理解这一点后,剩下的就好做了。
拆点:对于每一个点v,拆成两个点v'、v'',一个对应源点,另一个对应汇点。
建图:引入超级源点s和超级汇点t,对于每一个源点s',建一条边s->s',边容量为该草坪的牛的数量;对于每一个汇点t',建一条边t'->t,边容量为遮雨棚能容纳的牛的数量;先用floyd求出任意两点的最短路径,然后在枚举最长时间时,对于任意一个源点i和汇点j,若dist(i,j)<=maxtime,则建一条边,边容量为INF。
ps:时间会超int32,所以注意在需要地方使用long long.
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const long long INF=100000000000000LL; const int MAXN=205; int field[MAXN][2]; long long dist[MAXN][MAXN]; int level[MAXN*2]; int flow[MAXN*2][MAXN*2]; int n,m,num,s,t; long long min(long long a,long long b) { return a<b?a:b; } void floyd() { int i,j,k; for(k=1;k<=n;k++) for(i=1;i<=n;i++) if(dist[i][k]<INF) for(j=1;j<=n;j++) dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]); } void build_graph(long long limit) { int i,j; memset(flow,0,sizeof(flow)); for(i=1;i<=n;i++) { if(field[i][0]) flow[s][i]=field[i][0]; if(field[i][1]) flow[i+n][t]=field[i][1]; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(dist[i][j]<=limit) flow[i][j+n]=100000000; } int bfs() { int queue[MAXN*2],front,rear; front=rear=0; memset(level,0,sizeof(level)); level[s]=1; queue[rear++]=s; while(front!=rear) { int v=queue[front++]; for(int i=0;i<=t;i++) { if(!level[i]&&flow[v][i]) { level[i]=level[v]+1; queue[rear++]=i; } } } return level[t]; } int dfs(int i,int f) { if(i==t) return f; int sum=0; for(int j=0;f&&j<=t;j++) { if(level[j]==level[i]+1&&flow[i][j]) { int tmp=dfs(j,min(f,flow[i][j])); sum+=tmp; f-=tmp; flow[i][j]-=tmp; flow[j][i]+=tmp; } } return sum; } long long dinic() { long long low,mid,up,maxflow; int i,j; low=up=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(dist[i][j]<INF&&dist[i][j]>up) up=dist[i][j]; maxflow=0; build_graph(up); while(bfs()) maxflow+=dfs(s,100000000); if(maxflow<num) return -1; while(low<=up) { mid=(low+up)/2; build_graph(mid); maxflow=0; while(bfs()) maxflow+=dfs(s,100000000); if(maxflow<num) low=mid+1; else up=mid-1; } return up+1; } int main() { int i,j,u,v,w; while(~scanf("%d%d",&n,&m)) { s=0; t=n*2+1; num=0; for(i=1;i<=n;i++) { scanf("%d%d",&field[i][0],&field[i][1]); num+=field[i][0]; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(i!=j) dist[i][j]=INF; else dist[i][j]=0; while(m--) { scanf("%d%d%d",&u,&v,&w); if(w<dist[u][v])//判重 dist[u][v]=dist[v][u]=w; } floyd(); printf("%lld\n",dinic()); } return 0; }