Ombrophobic Bovines
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17547 | Accepted: 3843 |
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
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.
题意:有n个牛棚和m条路,每个牛棚有一些牛,但牛棚只能装入一定数量的牛,所以有些牛必须走去其他有空位的牛棚,(注意这里很多牛一起走同一条路只需要一份时间),问所有牛都进去牛棚最少需要多少时间? 如果不能做到,则输出-1
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.
思路:最开始想用最小费用做,但是后来发现无法满足很多牛一起走同一条路只需要一份时间这个条件...于是乎,去百度,发现别人用的floyd和二分。
很奇怪,这道题floyd和后面二分Dinic居然不会TLE? low初始=0,high=两点间最大值+1,然后每次只加两点间最小值小于等于mid的边(如果加了大于mid的边,那么无法保证mid是最小时间了)
注意此题需要用long long,很坑。 最短路数组最开始赋成1<<30结果WA了好多次。 因为两点之间最短路会大于1<<30所以无法更新到,所以初始时最好把两点间最短路赋为不可能值-1
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 550
#define INF 1<<30
struct Edge
{
int to,next,cap;
} edge[N*N];
int cnt,head[N],d[N];
int now[N],can[N];
long long maxn,dp[N][N];
void init(int n)
{
maxn=-INF;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
dp[i][j]= i==j?0:-1;
}
void addedge(int from,int to,int cap)
{
edge[cnt].to=to;
edge[cnt].cap=cap;
edge[cnt].next=head[from];
head[from]=cnt++;
edge[cnt].to=from;
edge[cnt].cap=0;
edge[cnt].next=head[to];
head[to]=cnt++;
}
int bfs(int s,int t)
{
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==-1&&edge[i].cap>0)
{
d[v]=d[u]+1;
q.push(v);
}
}
}
return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
if(s==t||f==0) return f;
int flow=0;
for(int i=head[s]; i!=-1&&flow<f; i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==d[s]+1&&edge[i].cap>0)
{
int x=min(f-flow,edge[i].cap);
x=dfs(v,t,x);
flow+=x;
edge[i].cap-=x;
edge[i^1].cap+=x;
if(f==0) break;
}
}
if(!flow) d[s]=-2;
return flow;
}
int Dinic(int s,int t)///起点s终点t
{
int flow=0,f;
while(bfs(s,t))
{
while(f=dfs(s,t,INF))
flow+=f;
}
return flow;
}
void floyd(int n)
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(dp[i][k]==-1||dp[k][j]==-1) continue;
if(dp[i][j]==-1||dp[i][j]>dp[i][k]+dp[k][j])
{
dp[i][j]=dp[i][k]+dp[k][j];
maxn=max(maxn,dp[i][j]);
}
}
}
int main()
{
int n,m,t;
int from,to;
long long val;
while(~scanf("%d %d",&n,&m))
{
init(n);
int S=0,T=2*n+1;
long long sum=0;
for(int i=1; i<=n; i++)
{
scanf("%d %d",&now[i],&can[i]);
sum+=now[i];
}
for(int i=1; i<=m; i++)
{
scanf("%d %d %I64d",&from,&to,&val);
if(from==to) continue;
if(dp[from][to]==-1||dp[from][to]>val)
{
dp[from][to]=dp[to][from]=val;
maxn=max(maxn,val);
}
}
floyd(n);
long long low=0,high=maxn+1,mid,ans=-1;
while(low<high)
{
mid=(low+high)/2;
cnt=2;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
addedge(S,i,now[i]);
addedge(i,i+n,INF);
addedge(i+n,T,can[i]);
for(int j=1;j<=n;j++)
if(i!=j&&dp[i][j]!=-1&&dp[i][j]<=mid)
addedge(i,j+n,INF);
}
if(Dinic(S,T)==sum) ans=high=mid;
else low=mid+1;
}
printf("%I64d\n",ans);
}
return 0;
}