"luogup3376" "template" Network maximum flow

Source: Internet
Author: User

Title Description

Title, a network diagram is given, along with its source and sink points, to find out its maximum network flow.

Input/output format Input Format:

The first line contains four positive integers n, M, S, T, which represent the number of points, the number of forward edges, the number of source points, and the number of sink points.

The next M line contains three positive integer UI, VI, WI, indicating that article I has a forward edge from the UI, reaching VI, the edge is WI (that is, the edge of the maximum traffic is WI)

output Format:

A row that contains a positive integer, which is the maximum stream for the network.

Input/Output sample Input Sample # #:Copy
4 5 4 34 2 304 3 202 3 202 1 301 3 40
Sample # # of output:Copy
50
Description

Time limit: 1000ms,128m

Data size:

For 30% data: n<=10,m<=25

For 70% data: n<=200,m<=1000

For 100% data: n<=10000,m<=100000

Sample Description:

There are 3 paths in the title:

4-->2-->3, the route is available through 20 of the flow

4-->3, up to 20 of the flow

4-->2-->1-->3, up to 10 of the flow (20 of the flow before the edge 4-->2)

So the total flow 20+20+10=50. Output 50.

Exercises

is a template problem Ah! Qaq

Actually, it's a board 2333.

1 /*2 Qwerta3 P3376 "template" Network maximum flow4 Accepted5  -6 Code c++,1.68kb7 Submission Time 2018-07-12 17:31:298 time consuming/memory9 140ms, 4847KBTen */ One#include <cmath> A#include <queue> -#include <cstdio> -#include <cstring> the#include <iostream> - using namespacestd; - structemm{ -     inte,f,v; +}a[200007];//contiguous linked list storage edge - inth[10007],cur[10007];//cur: Current arc optimization (no use + intn,m,s,t; A inttot=1; atInlineintRead ()//Quick Read - { -     CharCh=GetChar (); -     ints=1, x=0; -      while(ch<'0'|| Ch>'9'){if(ch=='-') s=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {x=x*Ten+ch- -; ch=GetChar ();} in     returns*x; - } toInlinevoidConintLintRintW//Add Edge + { -a[++tot].f=H[l]; theh[l]=tot; *     //Cur[l]=h[l]; $A[tot].e=R;Panax Notoginsenga[tot].v=W; -     return; the } +InlinevoidScan ()//read in, build a map A { theN=read (), M=read (), S=read (), t=read (); +      for(intI=1; i<=m;++i) -     { $         intZ=read (), Y=read (), l=read (); $ con (z,y,l); -Con (y,z,0); -     } the     return; - }Wuyi //Dinic thequeue<int>q;//BFS's Queue - intd[100007];//depth of a hierarchical graph WuInlineBOOLBFS () - { Aboutmemset (D,0,sizeof(d));//Initialize $d[s]=1;//Mark Source Point depth - Q.push (s); -      for(intI=1; i<=n;++i) Cur[i]=h[i];//Recovering cur Arrays -      while(!q.empty ()) A     { +         intnow=Q.front (); the Q.pop (); -         //Expansion $          for(intI=h[now];i;i=a[i].f) the         if(!D[A[I].E]&AMP;&AMP;A[I].V)//if it is not marked and the edge is in the residual network the         { thed[a[i].e]=d[now]+1; the Q.push (A[I].E); -         } in     } the     returnD[T];//back to s,t whether Unicom the } About intDfsintXintal) the { the     if(X==t| |! Alreturnal; the     inttot=0; +      for(intI=cur[x];i;i=a[i].f) -     { theCur[x]=i;//Current ARC OptimizationBayi         if(d[a[i].e]==d[x]+1&&a[i].v) the         { the             intF=dfs (A[i].e,min (AL,A[I].V));//look down . -             if(f)//unless 0 -             { thea[i].v-=F; thea[i^1].v+=F; thetot+=F; theal-=F; -                 if(!al) Break; the             } the         } the     }94     if(!tot) d[x]=-1;//The most useful optimizations! (Knocking on the blackboard the     returnTot//Return flow value the } theInlinevoidRun ()//Run98 { About     Long Longans=0; -      while(BFS ())) Ans+=dfs (S,2147483647);101cout<<ans;102     return;103 }104 intMain ()//Super Short main function (the year the Code wind is so strange orz the {106 Scan ();107 run ();108     return 0;109}

"luogup3376" "template" Network maximum 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.