POJ 3281 Dining (Maximum network flow)

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=3281

Main topic:

There are n cows, f food, D drinks, the first cow like fi seed food and di kind of drink, each food or drink is selected by a cow, can not be selected by other cows, ask the maximum number of cows to meet the requirements?

Problem Solving Ideas:

The biggest matching problem, the key is how to build a map, can be fictitious out of a source point, a meeting point, altogether need to f+d+2*n+2 points can be, the map is: food-------------------- The cow was opened as a point to allow a cow to correspond to one drink and one food, avoiding the occurrence of a variety of drinks or foods.

Code:

1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <algorithm>5#include <iostream>6#include <cmath>7#include <queue>8 using namespacestd;9 Ten #defineMAXN 0x3f3f3f3f One #defineN 410 A intMap[n][n], Layer[n], S, e; - BOOLVisit[n]; - BOOLCountlayer (); the intdinic (); -  - intMain () - { +     intN, F, D, X, Y, M; -      while(SCANF (" %d%d%d", &n, &f, &d)! =EOF) +     { As =0, E = f + D + N + N +1; atmemset (Map,0,sizeof(map)); -  -          for(intI=1; i<=f; i++) -map[0][i] =1;//Food and source connections. -  -          for(intI=1; i<=d; i++) inmap[i+f+2*n][e] =1;//drinks and Meeting point links -  to          for(intI=1; i<=n; i++) +Map[i+f][i+f+n] =1;//corresponding cow and ox links -  the          for(intI=1; i<=n; i++) *{//establishing the relationship between cattle and food and drink $             intnum = f +i;Panax Notoginsengscanf ("%d%d", &x, &y); -              while(X--) the             { +scanf ("%d", &m); AMap[m][num] =1; the             } +              while(Y--) -             { $scanf ("%d", &m); $Map[num + n][m+f+2*n] =1; -             } -         } theprintf ("%d\n", Dinic ()); -     }Wuyi     return 0; the } -  Wu BOOLCountlayer () - { AboutDeque <int>Q; $memset (Layer,0,sizeof(Layer)); -layer[0] =1; -Q.push_back (0); -      while(!q.empty ()) A     { +         intnd =Q.front (); the Q.pop_front (); -          for(intI=s; i<=e; i++) $         { the             if(map[nd][i]>0&&!Layer[i]) the             { theLayer[i] = Layer[nd] +1; the                 if(i = =e) -                     return true; in                 Else the Q.push_back (i); the             } About         } the     } the     return false; the } +  - intDinic ()//dinic Templates the {Bayi     intMaxnflow =0, I; the      while(Countlayer ()) the     { -deque<int>Q; -memset (Visit,0,sizeof(visit)); thevisit[0] =1; theQ.push_back (0); the          while(!q.empty ()) the         { -             intnd =Q.back (); the             if(nd! =e) the             { the                  for(i=0; i<=e; i++)94                 { the                     if(map[nd][i]>0&& Layer[i] = = layer[nd]+1&&!Visit[i]) the                     { theVisit[i] =1;98 Q.push_back (i); About                          Break; -                     }101                 }102                 if(I >e)103 Q.pop_back ();104             } the             Else106             {107                 intMinflow =MAXN;108                 intMV;109                  for(i=1; I<q.size (); i++) the                 {111                     intNS = q[i-1]; the                     intNE =Q[i];113                     if(Map[ns][ne] <Minflow) the                     { theMinflow =Map[ns][ne]; theMV =NS;117                     }118                 }119Maxnflow + =Minflow; -                  for(i=1; I<q.size (); i++)121                 {122                     intNS = q[i-1];123                     intNE =Q[i];124Map[ns][ne]-=Minflow; theMap[ne][ns] + =Minflow;126                 }127                  while(! Q.empty () && q.back ()! =mv) - Q.pop_back ();129             } the         }131     } the     returnMaxnflow;133}

POJ 3281 Dining (Maximum network flow)

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.