POJ 2289 Jamie's contact Groups two points + network stream

Source: Internet
Author: User

Test instructions

For n points and M points, an n can correspond to only one m, one m can correspond to multiple n, and each n gives the set of M points that he can correspond to, and m corresponds to the minimum value of n majority.

Analysis:

Network flow + two points.

Code:

POJ 2289//sep9#include <iostream> #include <queue> using namespace std;  const int maxn=2048;    const int maxm=1000000;  struct Edge {int v,f,nxt;  }E[MAXM*2+10];  Queue<int> que;  int src,sink;  int g[maxn+10];  int nume;  BOOL VIS[MAXN+10];  int dist[maxn+10]; int map[2048][2048];      int N,m;char tmp[4000];void addedge (int u,int v,int c) {e[++nume].v=v;e[nume].f=c;e[nume].nxt=g[u];g[u]=nume;  E[++nume].v=u;e[nume].f=0;e[nume].nxt=g[v];g[v]=nume;        } void Init () {memset (g,0,sizeof (g));  nume=1;      } void BFs () {while (!que.empty ()) Que.pop ();      Vis[src]=true;        Que.push (SRC);          while (!que.empty ()) {int U=que.front (); Que.pop ();                  for (int i=g[u];i;i=e[i].nxt) if (e[i].f&&!vis[e[i].v]) {Que.push (E[I].V);                  dist[e[i].v]=dist[u]+1;               Vis[e[i].v]=true; }}} int dfs (int u,int delta) {if (U==sink) RetuRN Delta;      int ret=0; for (int i=g[u];d elta&&i;i=e[i].nxt) if (e[i].f&&dist[e[i].v]==dist[u]+1) {int Dd=dfs (              E[i].v,min (E[i].f,delta));              E[I].F-=DD;              E[I^1].F+=DD;              DELTA-=DD;          RET+=DD;  } return ret;        } int dinic (int maxx) {init (); Src=0,sink=n+m+1;int i,j;for (i=1;i<=n;++i) Addedge (src,i,1); for (i=1;i<=n;++i) for (j=1;j<=m;++j) if (Map[i]      [J]==1] Addedge (i,n+j,1); for (I=1;i<=m;++i) Addedge (n+i,sink,maxx); int ret=0;          while (1) {memset (vis,0,sizeof (VIS));          memset (dist,0,sizeof (Dist));          BFS ();          if (!vis[sink]) break;      Ret+=dfs (Src,int_max);   } if (ret==n) return 1;    return 0;} int main () {while (scanf ("%d%d", &n,&m) ==2) {if (n==0&&m==0) Break;int I,j;getchar (); memset (map,0,      sizeof (map));         for (int i=1;i<=n;i++) {gets (TMP);          int Len=strlen (TMP); for (int j=0;j<len;j++) {if (tmp[j]>= ' 0 ' &&tmp[j]<= ' 9 ') {int v=0;                  while (tmp[j]>= ' 0 ' &&tmp[j]<= ' 9 ') {v=v*10+tmp[j++]-' 0 ';                } map[i][v+1]=1; }}}int Ans,l,r;l=0,r=n+1;while (l<r) {int mid= (l+r)/2;if (Dinic (mid) ==1) {R=mid;ans=mid;} elsel=mid+1;}     printf ("%d\n", ans);}     return 0;    }


POJ 2289 Jamie's contact Groups two points + network stream

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.