"UVA 753" A Plug for UNIX (graph theory-Maximum flow dinic)

Source: Internet
Author: User

Test Instructions: there are n plugs, m devices and K-type converters. Require the device to plug as much as possible, ask for a minimum number of mismatched devices.

Solution: to the reading of the various plug coding, source point to equipment, equipment through the converter to plug, plug to the meeting point each building a capacity of 1 side. The maximum number of matching devices can be obtained by running the Max stream at one time.

Note that this code in the magic of the place of the dead loop-to determine the queue is empty statement ... Σ (° °| | |) ︴ so we mainly look at the "line of thought"? :-)

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5#include <iostream>6#include <queue>7 using namespacestd;8 9 Const intn= the, m= the, k= the;Ten Const intnn=310, mm=310, inf= (int) 1e9; One intN,m,k,ts,st,ed,len; A intD[nn],last[nn]; - Chars[n],ss[n],str[3*n][n];//, sb[m][n]; - structedge{intX,y,fl,next;} A[MM]; thequeue<int>Q; -  - intMmin (intXintY) {returnX<y?x:y;} - intget_id () + { -      for(intI=1; i<=ts;i++) +       if(!STRCMP (S,str[i]))returni; Astrcpy (str[++ts],s); at     returnts; - } - voidInsintXintYintFL) - { -A[++len].x=x,a[len].y=y,a[len].fl=FL; -a[len].next=last[x],last[x]=Len; inA[++len].x=y,a[len].y=x,a[len].fl=0; -a[len].next=last[y],last[y]=Len; to } + BOOLBFS () - { thememset (d,-1,sizeof(d)); *      while(!q.empty ()) Q.pop ();//The dead circle here??? -_-||| $Q.push (ST); d[st]=1;Panax Notoginseng      while(!q.empty ()) -     { the       intx=Q.front (); Q.pop (); +        for(intI=last[x];i;i=a[i].next) A       { the         inty=a[i].y; +         if(d[y]!=-1||! A[I].FL)Continue;//FL -d[y]=d[x]+1, Q.push (y); $         //if (y==ed) break;//No, this is for the 61st line of optimization. $       } -     } -     if(d[ed]==-1)return false; the     return true; - }Wuyi intDfsintXintflow) the { -     if(x==ed)returnflow; Wu     intsum=0; -      for(intI=last[x];i;i=a[i].next) About     { $       inty=a[i].y; -       if(d[y]!=d[x]+1||! A[I].FL)Continue;// -       intT=dfs (Y, Mmin (a[i].fl,flow-sum)); -A[i].fl-=t, a[i^1].fl+=T; Asum+=T; +       if(Sum==flow) Break; the     } -     if(sum==0) d[x]=-1;// $     returnsum; the } the intdinic () the { the     intsum=0; -      while(BFS ()) inSum+=dfs (St,inf);//The capacity at this point is INF the     returnsum; the } About intMain () the { the     intt,x,y; thescanf"%d",&T); +      while(t--) -     { thest=1, ed=2, len=1, ts=2;Bayiscanf"%d",&n); theMemset (Last,0,sizeof(last)); the        for(intI=1; i<=n;i++) -       { -scanf"%s", s); thex=get_id (); theIns (x,ed,1); the       } thescanf"%d",&m); -        for(intI=1; i<=m;i++) the       { thescanf"%s%s", s,s); thex=get_id ();94Ins (st,x,1); the       } thescanf"%d",&k); the        for(intI=1; i<=k;i++)98       { Aboutscanf"%s", s); -x=get_id ();101scanf"%s", s);102y=get_id ();103Ins (x, Y,1);104       } the       intans=dinic ();106       if(T) printf ("\ n");107printf"%d\n", M-ans);108     }109     return 0; the}

"UVA 753" A Plug for UNIX (graph theory-Maximum flow dinic)

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.