ZOJ2532_Internship

时间:2023-03-09 03:56:20
ZOJ2532_Internship

一个单源多汇的有向图,求增大那些边的容量可以使得网络的最大流增加。

很简单,直接跑最大流,保留残余网络,然后枚举所有余量为0的边,使其容量增加一个1,看看是否出现新的增广路即可。

召唤代码君:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#define maxn 555
#define maxm 55555
using namespace std; int to[maxm],c[maxm],next[maxm],first[maxn],edge;
int d[maxn],tag[maxn],TAG=;
bool can[maxn];
int Q[maxm],bot,top;
int n,m,l,s,t; void _init()
{
edge=-;
for (int i=; i<=n+m+; i++) first[i]=-;
} void addedge(int U,int V,int W)
{
edge++;
to[edge]=V,c[edge]=W,next[edge]=first[U],first[U]=edge;
edge++;
to[edge]=U,c[edge]=,next[edge]=first[V],first[V]=edge;
} bool bfs()
{
Q[bot=top=]=t,tag[t]=++TAG,d[t]=,can[t]=false;
while (bot<=top)
{
int cur=Q[bot++];
for (int i=first[cur]; i!=-; i=next[i])
if (c[i^] && tag[to[i]]!=TAG)
{
tag[to[i]]=TAG,d[to[i]]=d[cur]+;
can[to[i]]=false,Q[++top]=to[i];
if (to[i]==s) return true;
}
}
return false;
} int dfs(int cur,int num)
{
if (cur==t) return num;
int tmp=num,k;
for (int i=first[cur]; i!=-; i=next[i])
if (c[i] && d[to[i]]==d[cur]- && tag[to[i]]==TAG && !can[to[i]])
{
k=dfs(to[i],min(c[i],num));
if (k) num-=k,c[i]-=k,c[i^]+=k;
if (!num) break;
}
if (num) can[cur]=true;
return tmp-num;
} int main()
{
int U,V,W,Flow=;
vector<int> ans;
while (scanf("%d%d%d",&n,&m,&l) && (n|m|l))
{
_init();
for (int i=; i<=l; i++)
{
scanf("%d%d%d",&U,&V,&W);
addedge(V,U,W);
}
s=,t=n+m+;
for (int i=; i<=n; i++) addedge(i,t,~0U>>);
while (bfs()) Flow+=dfs(s,~0U>>);
ans.clear();
for (int i=; i<=l; i++)
{
if (c[i+i-]) continue;
c[i+i-]++;
if (bfs()) ans.push_back(i);
c[i+i-]--;
}
if (ans.size()>)
{
printf("%d",ans[]);
for (unsigned i=; i<ans.size(); i++) printf(" %d",ans[i]);
}
printf("\n");
}
return ;
}