HDU ACM 1532 Drainage ditches-> Network max flow template problem (EK algorithm practice)

Source: Internet
Author: User

Analysis: The maximum flow of the entry problem, BFS looking for augmented road, found to update traffic until the augmentation path can not be found.

#include <iostream> #include <queue>using namespace std; #define N 202#define MAX 0x7fffffff;class max_flow_ Ek{public:max_flow_ek () {}~max_flow_ek () {}int Run (int n,int m);p rivate:void Init (); void Read (int n); int maxflow (int s,   int d,int m); int Bfs (int s,int d,int m); int m_icap[n][n];     Record the flow of residual network int m_iflow[n];      The current traffic from the source point to the current node is available in int m_ipre[n]; The precursor of the node in the augmented path queue<int> m_q;}; int max_flow_ek::run (int n,int m) {Init (); Read (n); return Maxflow (1,m,m);}  int max_flow_ek::bfs (int s,int d,int m) {int i,index;while (!m_q.empty ()) M_q.pop (); Empty queue for (i=0;i<=m;i++)//Initialize precursor M_ipre[i]=-1;m_ipre[s]=0;m_iflow[s]=max;m_q.push (s); while (!m_q.empty ()) {in  Dex=m_q.front (); Take out the team head node M_q.pop (); if (Index==d)//Find the Grace Road Break;for (i=1;i<=m;i++) if (i!=s && m_icap[index][i]>0 && Amp M_ipre[i]==-1) {M_ipre[i]=index;m_iflow[i]=m_iflow[index]<m_icap[index][i]?m_iflow[index]:m_icap[index][i];m _q.push (i);}} if (m_ipre[d]==-1) Return-1;else return m_iflow[d];} InchT Max_flow_ek::maxflow (int s,int d,int m) {int increasement;int sumflow;int k,last;sumflow=0;while ((Increasement=Bfs (s  , d,m))!=-1) {K=d;while (k!=s) {last=m_ipre[k];m_icap[last][k]-=increasement;  Change the forward edge capacity m_icap[k][last]+=increasement; Change reverse edge capacity k=last;} Sumflow+=increasement;} return sumflow;} void Max_flow_ek::init () {memset (m_icap,0,sizeof (M_icap)); Memset (M_iflow,0,sizeof (M_iflow));} void Max_flow_ek::read (int n) {int i;int si,ei,ci;for (i=0;i<n;i++) {scanf ("%d%d%d", &si,&ei,&ci); if (Si      ==ei) continue;       The starting point may be the same as the end M_icap[si][ei]+=ci; Consider repeating input}} int main () {int n,m; Max_flow_ek Max_flow;while (scanf ("%d%d", &n,&m) ==2) {printf ("%d\n", Max_flow.    Run (N,m));}      return 0; }


HDU ACM 1532 Drainage ditches-> Network max flow template problem (EK algorithm practice)

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.