"POJ1815" Friendship network flow minimum cut

Source: Internet
Author: User

Test instructions: Several people, and then give the S, T, and the edge matrix, ask the least how many people to kill can let S and t these two (can not be killed) the person is not connected. (also the Minimum Dictionary order scheme)


Puzzle: Minimum cut, first we split, two parts of the flow to meet the nature, and then even the INF Unicom edge.

And then it's going to take a couple of people to run.

If it is INF, no answer


Then we from small to large enumeration which people in the smallest cut set inside, that is, the person deleted and then run, see Maxflow.

Then AC.


Note that in test instructions, if it is necessary to kill, the second line will output these people.

But do not say no need to do.


After detection, the output is not empty line, it is inferred that there is no such situation.

Code:

#include <cmath> #include <queue> #include <cstdio> #include <cstring> #include <iostream > #include <algorithm> #define N 2000//Network Diagram Midpoint # # G 1000000//Network Diagram Edge # define INF 0x3f3f3f3fusing namespace Std;s Truct ksd{int V,next,len;} E[g];int head[n],cnt;void Add (int u,int v,int len) {cnt++;e[cnt].v=v;e[cnt].len=len;e[cnt].next=head[u];head[u]=cnt;} Queue<int>q;int D[n],s,t;bool BFs () {memset (d,0,sizeof (d)); int I,u,v;while (!q.empty ()) Q.pop (); Q.push (s), D[s] =1;while (!q.empty ()) {U=q.front (), Q.pop (); for (I=head[u];i;i=e[i].next) if (E[i].len) {v=e[i].v;if (!d[v]) {D[v]=d[u] +1;if (v==t) return 1;q.push (v);}} return 0;} int dinic (int x,int flow) {if (x==t) return flow;int remain=flow,k;int i,v;for (i=head[x];i&&remain;i=e[i].next) {v=e[i].v;if (e[i].len&&d[v]==d[x]+1) {k=dinic (V,min (Remain,e[i].len)), if (!k) d[v]=0;e[i^1].len+=k,e[i]. Len-=k;remain-=k;}} return flow-remain;} int N,m,p,maxflow;int s,t,ans;int Map[1005][1005];bool vis[n];void build () {int I,j;cnt=1;memseT (head,0,sizeof (head)), for (i=1;i<=n;i++) if (!vis[i]) Add (i,i+n,1), add (i+n,i,0), for (i=1;i<=n;i++) if (!vis[i] ) for (j=1;j<=n;j++) if (!vis[j]) {if (i==j) continue;if (Map[i][j]) Add (I+n,j,inf), add (j,i+n,0);}} BOOL Work () {if (scanf ("%d%d%d", &n,&s,&t) ==eof) return 0;int i,j,k,flag=0;s=s+n,t=t;for (i=1;i<=n;i++) for (j=1;j<=n;j++) scanf ("%d", &map[i][j]), build (), Ans=0;while (BFS ()) {k=dinic (S,inf), if (K==inf) {puts ("NO Answer! "); return 1;} else ans+=k;} printf ("%d\n", ans), if (ans) flag=1;for (i=1;i<=n;i++) if (i!=s&&i!=t) {vis[i]=1;build (); Maxflow=0;while ( BFS ()) Maxflow+=dinic (S,inf), if (Maxflow<ans) printf ("%d", i), Ans=maxflow;else vis[i]=0;} Puts (""); return 1;} int main () {freopen ("test.in", "R", stdin), while (work ());}


"POJ1815" Friendship network flow minimum cut

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.