Hdoj 3549 Flow problem (maximum network flow)

Source: Internet
Author: User

Topic Links: http://acm.hdu.edu.cn/showproblem.php?pid=3549

Thinking Analysis: The problem is bare maximum network flow problem, the data is not large, using the EDMONDSKARP algorithm can be solved; it is important to note that there are up to 15 points for this problem, and the number of edges is up to 1000, so there are heavy edges in the graph, and multiple heavy edges need to be combined as one edge;

The code is as follows:

#include <queue>#include<vector>#include<cstdio>#include<climits>#include<cstring>#include<iostream>using namespacestd;Const intMax_n = -;intCap[max_n][max_n], flow[max_n][max_n];intA[max_n], P[max_n];inlineintMin (intAintb) {returnA < b?a:b;}intEdmondskarp (intver_num) {Queue<int>Q; intMax_flow =0; memset (Flow,0,sizeof(flow));  for (;;) {memset (A,0,sizeof(a)); a[1] =Int_max; Q.push (1);  while(!Q.empty ()) {            intU =Q.front ();            Q.pop ();  for(intv =1; V <= ver_num; ++v) {if(!a[v] && cap[u][v] >Flow[u][v]) {P[v]=u;                    Q.push (v); A[V]= Min (A[u], Cap[u][v]-Flow[u][v]); }            }        }        if(A[ver_num] = =0) Break;  for(intu = ver_num; U! =1; U =P[u]) {Flow[p[u]][u]+=A[ver_num]; Flow[u][p[u]]-=A[ver_num]; } Max_flow+=A[ver_num]; }    returnMax_flow;}intMain () {intCase_times, case_id =0; intRoad_num, Ver_num; scanf ("%d", &case_times);  while(case_times--)    {        intver_1, ver_2, CAPA; scanf ("%d%d", &ver_num, &road_num); memset (Cap,0,sizeof(CAP));  for(inti =0; i < Road_num; ++i) {scanf (" %d%d%d", &ver_1, &ver_2, &CAPA); Cap[ver_1][ver_2]+=Capa; }        intAns =Edmondskarp (Ver_num); printf ("Case %d:%d\n", ++case_id, ans); }    return 0;}

Hdoj 3549 Flow problem (maximum network 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.