Hihocoder Network Stream One · Ford-fulkerson algorithm

Source: Internet
Author: User

Network Flow One · Ford-fulkerson algorithm time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description

Small hi and Small ho live in P city, p City is a big big city, so also faced with a big city will encounter problems: traffic congestion.

Little ho: Every weekend comes home feeling traffic jam is a kind of suffering ah.

Small hi: Usually the traffic is also good, just one to the rush hour will be more crowded.

Little ho: If you can limit the number of cars, I do not know if there is a way to know the maximum traffic flow system, so that can be limited to a can always be a smooth quantity.

Little hi: Theoretically, there are algorithms. As early as 1955, T.E Harris raised the question of seeking maximum traffic between two points on a given network. and thus a new graph theory model is created: network flow.

Little ho: What exactly is that?

Little hi: The mathematical description of the language is given a graph g= (v,e), where each edge (U,V) has a non-negative capacity value, recorded as C (U,v) ≥0. At the same time there are two special vertices in the graph, the source point S and the meeting point T.

As an example:

Where node 1 is the source point S and Node 6 is the meeting point T.

We ask for the maximum feasible flow from the source point S to the meeting point T, which is also known as the maximum flow problem.

In this example, the maximum flow is 5, respectively: 1→2→4→6, Flow is 1;1→3→4→6, flow is 2;1→3→5→6, flow is 2.

Little ho: It looks like fun, you let me think about it first.

Hint: Ford-fulkerson algorithm

Input

Line 1th: 2 positive integer n,m. 2≤n≤500,1≤m≤20,000.

2nd.. M+1 line: 3 integers per line u,v,c (U,V), representing an edge (U,V) and its capacity C (u,v). 1≤u,v≤n,0≤c (u,v) ≤100.

The default source point in the given diagram is 1, and the meeting point is N. There may be duplicate edges.

Output

Line 1th: An integer representing the maximum stream for the given figure G.

Sample input
6 71 2 31 3 52 4 13 4 23 5 34 6 45 6 2
Sample output
5
Analysis: The key to the maximum flow is to seek the augmented road, adjacent table storage edge, easy to modify;
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineVI vector<int>#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)#definePII pair<int,int>#defineLson L, Mid, rt<<1#defineRson mid+1, R, rt<<1|1Const intmaxn=5e2+Ten;using namespacestd;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;}intN,m,k,t,h[maxn],tot,p[maxn],a[maxn],v[maxn][maxn],ans;structnode{intFr,to,nxt,cap,flow;} e[20000<<1];voidAddintXintYintz) {e[tot].fr=x; E[tot].to=y; E[TOT].NXT=H[x]; E[tot].cap=Z; H[X]=tot++; e[tot].fr=y; E[tot].to=x; E[TOT].NXT=H[y]; H[y]=tot++;}voidMax_flow (intSintt) {ans=0;  while(1) {memset (A,0,sizeofa); Queue<int>Q; Q.push (s); A[s]=inf;  while(!Q.empty ()) {            intx=Q.front ();            Q.pop ();  for(inti=h[x];i!=-1; i=e[i].nxt) {                intto=e[i].to,cap=e[i].cap,flow=E[i].flow; if(!a[to]&&cap>flow) {P[to]=i; A[to]=min (a[x],cap-flow);                Q.push (to); }            }            if(A[t]) Break; }        if(!a[t]) Break;  for(intnow=t;now!=s;now=e[p[now]].fr) {E[p[now]].flow+=A[t]; E[p[now]^1].flow-=A[t]; } ans+=A[t]; }}intMain () {inti,j; scanf ("%d%d",&n,&m); memset (H,-1,sizeofh);  while(m--)    {        intb,c,d; scanf ("%d%d%d",&b,&c,&d); V[B][C]+=D; } Rep (I,1, N) Rep (J,1, N)if(V[i][j]) Add (I,j,v[i][j]); Max_flow (1, N); printf ("%d\n", ans); //System ("Pause");    return 0;}

Hihocoder Network Stream One · Ford-fulkerson algorithm

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.