poj2391Ombrophobic Bovines網路流最短路加二分
Language:Default Ombrophobic Bovines
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. Input * Line 1: Two space-separated integers: F and P 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 Sample Output Hint OUTPUT DETAILS: Source |
給定一個有n個頂點和m條邊的無向圖,點i 處有Ai頭牛,點i 處的牛棚能容納Bi頭牛,每條邊有一個時間花費ti(表示從一個端點走到另一個端點所需要的時間),求一個最短時間T使得在T時間內所有的牛都能進到某一牛棚裡去。
將每個點i 拆成兩個點i’, i’’,連 邊(s, i’, Ai), (i’’, t, Bi)。二分最短時間T,若d[i][j]<=T(d[i][j]表示點i, j 之間的最短時間花費)則加邊(i’, j’’, ∞)。每次根據最大流調整二分的上下界即可。
這裡拆點是因為i點有兩個屬性,一個是現在有多少牛,其次是一共能容納多少牛,加邊時i和i''之間加無窮,因為一個棚可以放多隻奶牛
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define inf 1e9
#define infLL 1LL<<60
using namespace std;
const int maxn=500+10;
struct Edge
{
int from,to,cap,flow;
Edge(){}
Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};
struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
int d[maxn];
bool vis[maxn];
int cur[maxn];
void init(int n,int s,int t)
{
this->n=n,this->s=s,this->t=t;
edges.clear();
for(int i=0;i<n;i++) G[i].clear();
}
void addedge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,0) );
edges.push_back( Edge(to,from,0,0) );
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
queue<int> Q;
memset(vis,0,sizeof(vis));
vis[s]=true;
d[s]=0;
Q.push(s);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=0;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if(x==t || a==0) return a;
int flow=0,f;
for(int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[e.to]==d[x]+1 && (f=DFS(e.to,min(a,e.cap-e.flow) ) ) >0)
{
e.flow+=f;
edges[G[x][i]^1].flow -=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int maxflow()
{
int ans=0;
while(BFS())
{
memset(cur,0,sizeof(cur));
ans +=DFS(s,inf);
}
return ans;
}
}Dc;
int n,m;
long long dis[maxn][maxn];
int now[maxn];
int can[maxn];
int fullflow;
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(dis[i][k]<infLL&&dis[k][j]<infLL)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
bool solve(long long limit)
{
Dc.init(2*n+2,0,2*n+1);
for(int i=1;i<=n;i++)
{
Dc.addedge(0,i,now[i]);
Dc.addedge(i+n,2*n+1,can[i]);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(dis[i][j]<=limit)
Dc.addedge(i,j+n,inf);
return Dc.maxflow()==fullflow;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{fullflow=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dis[i][j]=i==j?0:infLL;
for(int i=1;i<=n;i++)
{scanf("%d%d",&now[i],&can[i]);
fullflow+=now[i];
}
int u,v;
long long w;
while(m--)
{
scanf("%d%d%lld",&u,&v,&w);
dis[u][v]=dis[v][u]=min(w,dis[u][v]);
}
floyd(n);
long long l=0,r=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(dis[i][j]<infLL)
r=max(r,dis[i][j]);
if(!solve(r))
printf("-1\n");
else
{
while(r>l)
{
long long mid=l+(r-l)/2;
if(solve(mid))
r=mid;
else
l=mid+1;
}
printf("%lld\n",r);
}
}
return 0;
}