USACO5.4 奶牛的電信Telecowmunication (最小割,割邊轉割點)
阿新 • • 發佈:2020-08-21
題目
思路
看題目就知道是最小割了,但是不一樣的是,我們一般求的最小割是割邊,而這題讓我們求的是割點,那麼我們在原有的網路基礎上在每個點和一個我們附加的點上連上一條邊權為1的邊限制流量就好了。
程式碼實現
#include<cstdio> #include<algorithm> #include<vector> #include<queue> #include<map> #include<iostream> #include<cstring> #include<cmath> using namespace std; #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i) #define per(i,n,a) for (int i=n;i>=a;i--) #define MT(x,i) memset(x,i,sizeof(x) ) #define rev(i,start,end) for (int i=start;i<end;i++) #define inf 0x3f3f3f3f #define mp(x,y) make_pair(x,y) #define lowbit(x) (x&-x) #define MOD 1000000007 #define exp 1e-8 #define N 1000005 #define fi first #define se second #define pb push_back typedef long long ll; typedef pair<int ,int> PII; typedef pair<int ,PII> PIII; ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; } inline int read() { char ch=getchar(); int x=0, f=1; while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while('0'<=ch&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } const int maxn=3510; int head[maxn],cnt=1; struct edge { int v,flow,next; }e[maxn]; inline void add (int u,int v,int flow) { e[++cnt]= (edge) {v,flow,head[u]}; head[u]=cnt; } inline void add_edge (int u,int v,int flow) { add (u,v,flow); add (v,u,0); } int dis[maxn],cur[maxn]; int s,t,n,m,c1,c2; int bfs () { MT (dis,0); queue <int > q; dis[s]=1; q.push (s); while (q.size ()) { int x=q.front (); q.pop (); for (int i=head[x];~i;i=e[i].next) { if (dis[e[i].v]==0&&e[i].flow) { dis[e[i].v]=dis[x]+1; q.push (e[i].v); } } } return dis[t]; } int dfs (int now,int nowflow) { if (now==t) return nowflow; for (int &i=cur[now];i!=-1;i=e[i].next) { if (dis[e[i].v]==dis[now]+1&&e[i].flow) { int canflow=dfs (e[i].v,min (e[i].flow,nowflow)); if (canflow) { e[i].flow-=canflow; e[i^1].flow+=canflow; return canflow; } } } return 0; } int ans=0; void Dinic () { while (bfs ()) { memcpy (cur,head,sizeof (cur)); while (int val=dfs (s,inf)) ans+=val; } } int main () { MT (head,-1); scanf ("%d%d%d%d",&n,&m,&s,&t); s+=n; rep (i,1,n) add_edge (i,i+n,1); rep (i,1,m) { int x,y; scanf ("%d%d",&x,&y); add_edge (x+n,y,inf); add_edge (y+n,x,inf); } Dinic (); cout<<ans<<endl; return 0; }