luogu1345 奶牛的电信 (最小割)
虽然割点不好搞,但是可以变成割边呀
拆点,拆出来的边权给1,原图中的边权给inf,然后跑dinic就行了
1 #include<bits/stdc++.h> 2 #define pa pair<int,int> 3 #define CLR(a,x) memset(a,x,sizeof(a)) 4 using namespace std; 5 typedef long long ll; 6 const int maxn=220,maxm=600*10,inf=1e9; 7 8 inline ll rd(){ 9 ll x=0;char c=getchar();int neg=1; 10 while(c<'0'||c>'9'){if(c=='-') neg=-1;c=getchar();} 11 while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); 12 return x*neg; 13 } 14 15 struct Edge{ 16 int b,l,ne; 17 }eg[maxm]; 18 int egh[maxn],ect=1; 19 int S,T,N,M; 20 int cur[maxn],dep[maxn]; 21 queue<int> q; 22 23 inline void adeg(int a,int b,int c){ 24 eg[++ect].b=b,eg[ect].l=c,eg[ect].ne=egh[a];egh[a]=ect; 25 eg[++ect].b=a,eg[ect].l=0,eg[ect].ne=egh[b];egh[b]=ect; 26 } 27 28 inline bool bfs(){ 29 CLR(dep,0);CLR(cur,-1); 30 dep[S]=1,q.push(S); 31 while(!q.empty()){ 32 int p=q.front();q.pop(); 33 for(int i=egh[p];i;i=eg[i].ne){ 34 int b=eg[i].b; 35 if(dep[b]||!eg[i].l) continue; 36 dep[b]=dep[p]+1,q.push(b); 37 } 38 } 39 return dep[T]; 40 } 41 42 int dinic(int x,int y){ 43 if(x==T) return y; 44 int tmp=y; 45 if(cur[x]==-1) cur[x]=egh[x]; 46 for(int &i=cur[x];i;i=eg[i].ne){ 47 int b=eg[i].b; 48 if(dep[b]!=dep[x]+1||!eg[i].l) continue; 49 int re=dinic(b,min(eg[i].l,tmp)); 50 tmp-=re,eg[i].l-=re,eg[i^1].l-=re; 51 if(!tmp) break; 52 }return y-tmp; 53 } 54 55 int main(){ 56 //freopen("","r",stdin); 57 int i,j,k; 58 N=rd(),M=rd(),S=rd()+N,T=rd(); 59 for(i=1;i<=N;i++) 60 adeg(i,i+N,1); 61 for(i=1;i<=M;i++){ 62 int a=rd(),b=rd(); 63 adeg(a+N,b,inf),adeg(b+N,a,inf); 64 } 65 int ans=0; 66 while(bfs()) ans+=dinic(S,inf); 67 printf("%d\n",ans); 68 return 0; 69 }