Cogs 14. [Network flow 24 questions] paired with pilots

Source: Internet
Author: User

This problem is actually very good thinking, because divided into positive, deputy pilots. So the pilot as a boy, the copilot as a girl. Then do hungry can.
1#include <bits/stdc++.h>2 using namespacestd;3 intg[131][131],vis[131],girl[131],n,n1;4 intHungry (intBoy )5 {6     inti;7      for(i=n1+1; i<=n;i++)8     {9         if(g[boy][i]==1&&vis[i]==0)Ten         { Onevis[i]=1; A             if(Hungry (girl[i]) = =1|| girl[i]==0) -             { -girl[i]=Boy ; the                 return 1; -             } -         } -     } +     return 0; - } + intMain () A { at     inta,b,sum,i; -scanf"%d%d",&n,&N1); -      while(SCANF ("%d%d", &a,&b)!=eof) g[a][b]=1; -Memset (Girl,0,sizeof(Girl)); -sum=0; -      for(i=1; i<=n1;i++) in     { -memset (Vis,0,sizeof(Vis)); to         if(Hungry (i) = =1) sum++; +     } -printf"%d", sum);  the     return 0; *}
View Code But this problem is "network flow 24 questions", so still want to practice a network flow. So the maximum flow can be done directly with Dinic.
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <cstdlib>5#include <algorithm>6 using namespacestd;7 intn,s,t,q[131],dis[131],g[131][131];8 intMin (intAaaintBBB) {if(AAA&GT;BBB)returnbbbElse returnAAA;}9 intBfs ()Ten { One     inthead,tail,u,v; Amemset (Q,0,sizeof(q)), head=0, tail=1, q[tail]=s; -memset (dis,-1,sizeof(dis)), dis[s]=0; -      while(head<=tail) the     { -u=q[++Head]; -          for(v=1; v<=n;v++) -         { +             if(g[u][v]>0&&dis[v]<0) -             { +dis[v]=dis[u]+1; Aq[++tail]=v; at             } -         } -     } -     if(dis[n]<=0)return 0; -     Else return 1; - } in intDfs (intUintMinflow) - { to     intV,ans; +     if(u==n)returnMinflow; -      for(v=1; v<=n;v++) the     { *         if(g[u][v]>0&&dis[v]==dis[u]+1) $         {Panax Notoginsengans=Dfs (V,min (minflow,g[u][v)); -             if(ans!=0) the             { +g[u][v]-=ans; Ag[v][u]+=ans; the                 returnans; +             } -         }  $     } $     return 0; - } - intdinic () the { -     intmaxflow=0, ans;Wuyi      while(BFS ()! =0) the     { -Ans=dfs (s),0x7fffffff);if(ans==0) Break; maxflow+=ans; Wu     } -     returnMaxflow; About } $ intMain () - { -     intn1,a,b,i; -scanf"%d%d",&n,&N1); A      while(SCANF ("%d%d", &a,&b)!=eof) g[a][b]=1; +n+=2; s=n-1; t=N; the      for(i=1; i<=n1;i++) g[s][i]=1; -      for(i=n1+1; i<=n-2; i++) g[i][t]=1; $printf"%d", Dinic ()); the     return 0;  the}
View Code



Cogs 14. [Network flow 24 questions] paired with pilots

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.