依旧是水题
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.
Solution
再见USACO
先跑最短路。
然后二分答案,对于(i,j),如果dis[i][j]<=s则add_edge(i,j+n,INF)
src和牛的个数连边,sink和牛棚容量连边。(也就是对于i把它拆成两个点)
跑一遍Dinic就好了。
[源代码]
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int M=405;
const int N=5e4+5;
const int INF=1<<30;
struct node{
int to;ll v;
inline bool operator < (const node &tmp)const{
return v>tmp.v;
}
};
#define pb push_back
#define vec vector<node>
#define pq priority_queue<node>
vec G[M];
pq pque;
ll dis[M][M];
int n,m,A[M],B[M];
inline void Djstl(int s){
memset(dis[s],127,sizeof(dis[s]));
dis[s][s]=0;
for(;!pque.empty();)pque.pop();
pque.push((node){s,0});
for(;!pque.empty();){
int v=pque.top().to;pque.pop();
for(int i=0;i<G[v].size();++i){
node nxt=G[v][i];
if(dis[s][nxt.to]>dis[s][v]+nxt.v){
dis[s][nxt.to]=dis[s][v]+nxt.v;
pque.push((node){nxt.to,dis[s][nxt.to]});
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
struct Edge{
int to,cap,nxt;
}edge[N<<1];
int head[M],work[M],tot_edge,src,sink,dist[M];
bool mark[M];
inline void add_edge(int from,int to,int c){
edge[tot_edge]=(Edge){to,c,head[from]};
head[from]=tot_edge++;
edge[tot_edge]=(Edge){from,0,head[to]};
head[to]=tot_edge++;
}
queue<int>que;
inline bool bfs(){
for(int i=0;i<=sink;++i)
dist[i]=-1;
dist[src]=0;
que.push(src);
for(;!que.empty();){
int v=que.front();que.pop();
for(int i=head[v];~i;i=edge[i].nxt){
int to=edge[i].to;
if(dist[to]==-1&&edge[i].cap){
dist[to]=dist[v]+1;
que.push(to);
}
}
}
return dist[sink]!=-1;
}
inline int dfs(int v,int f){
if(v==sink)return f;
mark[v]=1;
for(int &i=work[v];~i;i=edge[i].nxt){
int to=edge[i].to;
if(!mark[to]&&dist[to]==dist[v]+1&&edge[i].cap){
int d=dfs(to,min(f,edge[i].cap));
if(d){
edge[i].cap-=d;
edge[i^1].cap+=d;
return d;
}
}
}
return 0;
}
inline int Dinic(){
int res=0;
for(;bfs();){
for(int i=0;i<=sink;++i)
work[i]=head[i];
for(;;){
for(int i=0;i<=sink;++i)
mark[i]=0;
int f=dfs(src,INF);
if(!f)break;
res+=f;
}
}
return res;
}
int sum;
inline int gao(ll s){
memset(head,-1,sizeof(head));
tot_edge=0;
for(int i=1;i<=n;++i){
add_edge(src,i,A[i]);
add_edge(i+n,sink,B[i]);
}
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(dis[i][j]<=s)
add_edge(i,j+n,INF);
int res=Dinic();
return res==sum;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;++i){
scanf("%d %d",&A[i],&B[i]);
sum+=A[i];
}
ll l=0,r=0,ans=-1;
for(int i=1,a,b,c;i<=m;++i){
scanf("%d %d %d",&a,&b,&c);
G[a].pb((node){b,c});
G[b].pb((node){a,c});
r+=c;
}
src=0,sink=n<<1|1;
for(int i=1;i<=n;++i)Djstl(i);
for(;l<=r;){
ll mid=l+r>>1;
if(gao(mid))ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans<<endl;
return 0;
}