bzoj1339/1163:[Baltic2008]Mafia
传送门
最小割,割点,模板。。。
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
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;
}
#define rg register
const int maxn=1e5+10,inf=1e9;queue<int>q;bool vis[maxn];
int cnt=1,n,m,s,ss,tt,t,dis[maxn],pre[maxn],nxt[maxn],h[maxn],v[maxn],used[maxn],ans;
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;
}
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(v[i]&&!dis[pre[i]])dis[pre[i]]=dis[x]+1,q.push(pre[i]);
}
return dis[t];
}
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(dis[pre[i]]>dis[x]&&v[i])
{
int y=dfs(pre[i],min(f,v[i]));
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),read(ss),read(tt),s=0,t=n*2+1,add(s,ss*2-1,inf),add(tt*2,t,inf);
for(rg int i=1,x;i<=n;i++)read(x),add(i*2-1,i*2,x);
for(rg int i=1,x,y;i<=m;i++)read(x),read(y),add(x*2,y*2-1,inf),add(y*2,x*2-1,inf);
for(;bfs();ans+=dfs(s,inf));printf("%d\n",ans);
}