做这道题目做的想砸电脑。。dinic的过了。。。。
今天ISAP也过了,不明白昨天晚上为什么一直WA
Ombrophobic Bovines
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9810 | Accepted: 2202 |
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.
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.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
#define MAXN 800
const int inf=1<<30;
#define INF 10000000000000
struct edge
{
int to,next;
int c;
};
edge e[999999];
int que[MAXN*100];
int dis[MAXN];
int pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed;
int en;
int n,m;
long long a[MAXN][MAXN];
int aa[MAXN],bb[MAXN];
long long maxflow;
void add(int a,int b,int c)
{
e[en].to=b;
e[en].c=c;
e[en].next=head[a];
head[a]=en++;
e[en].to=a;
e[en].c=0;
e[en].next=head[b];
head[b]=en++;
}
bool bfs()
{
memset(dis,-1,sizeof(dis));
que[0]=st,dis[st]=1;
int t=1,f=0;
while(f<t)
{
int j=que[f++];
for(int k=head[j];k!=-1;k=e[k].next)
{
int i=e[k].to;
if(dis[i]==-1 && e[k].c)
{
que[t++]=i;
dis[i]=dis[j]+1;
if(i==ed) return true;
}
}
}
return false;
}
int update()
{
int p,flow=inf;
for (int i=pre[ed];i!=-1;i=pre[i])
if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;
for (int i=pre[ed];i!=-1;i=pre[i])
e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;
maxflow+=flow;
return p;
}
void dfs()
{
memset(pre,-1,sizeof(pre));
memcpy(head2,head,sizeof(head2));
for(int i=st,j;i!=-1;)
{
int flag=false;
for(int k=head[i];k!=-1;k=e[k].next)
if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )
{
pre[j]=i;
head2[i]=k;
i=j;
flag=true;
if(i==ed)
i=update();
if(flag)
break;
}
if (!flag) dis[i]=-1,i=pre[i];
}
}
void build(long long maxs)
{
en=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
add(st,i,aa[i]);
add(i+n,ed,bb[i]);
add(i,i+n,inf);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i][j]!=INF && a[i][j]<=maxs)
{
add(i,j+n,inf);
}
}
void solve()
{
int x,y,z;
long long l=0,r=INF;
long long total=0;
st=0,ed=n*2+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
a[i][j]=INF;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&aa[i],&bb[i]);
total+=aa[i];
}
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
a[x][y]=a[y][x]=min(a[x][y],(long long)z);
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
{
if( a[i][k]==INF )
continue;
for(int j=1;j<=n;j++)
if(i!=j &&i!=k && j!=k && a[i][k]!=INF && a[k][j]!=INF)
if(a[i][k]+a[k][j]<a[i][j])
a[i][j]=a[i][k]+a[k][j];
}
bool flag=false;
long long ans=-1;
long long mid;
while(l<=r)
{
mid=(l+r)>>1LL;
build(mid);
maxflow=0;
while(bfs())
dfs();
if(maxflow==total)
{
r=mid-1;
ans=mid;
}
else
l=mid+1;
}
cout<<ans<<endl;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
solve();
return 0;
}
isap
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
#define MAXN 800
const int inf=1<<30;
#define INF 10000000000000
struct edge
{
int to,next;
int c;
};
edge e[999999];
int g[MAXN],en,dis[MAXN],gap[MAXN];
int que[MAXN*100];
int pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed;
int n,m;
long long a[MAXN][MAXN];
int aa[MAXN],bb[MAXN];
long long maxflow;
void add(int a,int b,int c)
{
e[en].to=b;
e[en].c=c;
e[en].next=g[a];
g[a]=en++;
e[en].to=a;
e[en].c=0;
e[en].next=g[b];
g[b]=en++;
}
int sap(int u,int flow)
{
if(u==ed)
return flow;
int ans=0,i,t;
for(i=g[u];i!=-1;i=e[i].next)
{
if( e[i].c && dis[u]==dis[e[i].to]+1)
{
t=sap(e[i].to,min(flow-ans,e[i].c));
e[i].c-=t,e[i^1].c+=t,ans+=t;
if(ans==flow)
return ans;
}
}
if(dis[st]>=n*2+2)
return ans;
if(!--gap[dis[u]]) //gap优化
dis[st]=n*2+2;
++gap[++dis[u]];
return ans;
}
void build(long long maxs)
{
en=0;
memset(g,-1,sizeof(g));
for(int i=1;i<=n;i++)
{
add(st,i,aa[i]);
add(i+n,ed,bb[i]);
add(i,i+n,inf);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i][j]!=INF && a[i][j]<=maxs)
{
add(i,j+n,inf);
}
}
void solve()
{
int x,y,z;
long long l=0,r=INF;
long long total=0;
st=0,ed=n*2+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
a[i][j]=INF;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&aa[i],&bb[i]);
total+=aa[i];
}
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
a[x][y]=a[y][x]=min(a[x][y],(long long)z);
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
{
if( a[i][k]==INF )
continue;
for(int j=1;j<=n;j++)
if(i!=j &&i!=k && j!=k && a[i][k]!=INF && a[k][j]!=INF)
if(a[i][k]+a[k][j]<a[i][j])
a[i][j]=a[i][k]+a[k][j];
}
long long ans=-1;
long long mid;
while(l<=r)
{
mid=(l+r)>>1LL;
build(mid);
maxflow=0;
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
for(gap[0]=n*2+2;dis[st]<n*2+2;)
maxflow+=sap(st,inf);
if(maxflow==total)
{
r=mid-1;
ans=mid;
}
else
l=mid+1;
}
cout<<ans<<endl;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
solve();
return 0;
}