bzoj1497:[NOI2006]最大获利
传送门
最小割,最大权闭合子图,基本建图方法就是正权点与源点连边,负权点与汇点连边,中间容量都是inf就好了,对于这个题的利益,我们可以将所有的边变成点,然后就建成了一个二分图,之后就好解决了
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
#define min(a,b) (a<b?a:b)
#define rg register
int dis[60010],ans,sum,n,m,cnt=1,s,t,inf=1e9+7,pre[350001],nxt[350001],h[60010],v[350001];queue<int>q;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
inline void add(int x,int y,int z)
{
pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt,v[cnt]=z;
pre[++cnt]=x,nxt[cnt]=h[y],h[y]=cnt,v[cnt]=0;
}
inline bool bfs()
{
memset(dis,0,sizeof dis);
q.push(s),dis[s]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for(rg int i=h[x];i;i=nxt[i])if(!dis[pre[i]]&&v[i])dis[pre[i]]=dis[x]+1,q.push(pre[i]);
}
return dis[t];
}
inline int dfs(int x,int flow)
{
if(x==t||!flow)return flow;
int f=flow;
for(rg int i=h[x];i;i=nxt[i])
if(v[i]&&dis[pre[i]]>dis[x])
{
int y=dfs(pre[i],min(v[i],f));
f-=y,v[i]-=y,v[i^1]+=y;
if(!f)return flow;
}
if(f==flow)dis[x]=-1;
return flow-f;
}
int main()
{
read(n),read(m),s=0,t=n+m+1;
for(rg int i=1,x;i<=n;i++)read(x),add(s,i,x);
for(rg int i=1,x,y,z;i<=m;i++)read(x),read(y),read(z),add(n+i,t,z),add(x,n+i,inf),add(y,n+i,inf),sum+=z;
for(;bfs();ans+=dfs(s,inf));
printf("%d\n",sum-ans);
}