bzoj1339 [Baltic2008]Mafia(最小割)

xiaoxiao2021-02-28  4

拆点,把点权变成边权,然后跑最小割就是答案了。

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; #define ll long long #define inf 0x3f3f3f3f #define N 410 inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f; } int n,m,h[N],num=1,cur[N],lev[N],S,T,ans=0; int dx[]={0,0,1,-1},dy[]={1,-1,0,0}; struct edge{ int to,next,val; }data[81000]; inline void add(int x,int y,int val){ data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val; data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].val=0; } inline bool bfs(){ queue<int>q;memset(lev,0,sizeof(lev)); q.push(S);lev[S]=1; while(!q.empty()){ int x=q.front();q.pop(); for(int i=h[x];i;i=data[i].next){ int y=data[i].to;if(lev[y]||!data[i].val) continue; lev[y]=lev[x]+1;if(y==T) return 1;q.push(y); } }return 0; } inline int dinic(int x,int low){ if(x==T) return low;int tmp=low; for(int &i=cur[x];i;i=data[i].next){ int y=data[i].to;if(lev[y]!=lev[x]+1||!data[i].val) continue; int res=dinic(y,min(tmp,data[i].val)); if(!res) lev[y]=0;else tmp-=res,data[i].val-=res,data[i^1].val+=res; if(!tmp) return low; }return low-tmp; } int main(){ // freopen("a.in","r",stdin); n=read();m=read();S=read();T=read()+n; for(int i=1;i<=n;++i) add(i,i+n,read()); while(m--){ int x=read(),y=read();add(x+n,y,inf);add(y+n,x,inf); }while(bfs()){memcpy(cur,h,sizeof(h));ans+=dinic(S,inf);} printf("%d\n",ans); return 0; }
转载请注明原文地址: https://www.6miu.com/read-1400381.html

最新回复(0)