HDU3549 flow problem "Maximum Flow" "Edmond-karp"

Source: Internet
Author: User

Topic Links:

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


Main topic:

Give you a weighted graph of N-node m-bars, the source point is 1, the meeting point is N, and the maximum flow of this graph is obtained.


Ideas:

This problem is the bare topic of the network flow to find the maximum flow. it can be done directly with the EDMOND-KARP algorithm.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < queue>using namespace Std;const int maxn = 20;const int maxm = 2020;int map[maxn][maxn],pre[maxn],n,m;bool EkBFS (int st    Art,int end) {queue<int> Q;    BOOL VIS[MAXN];    memset (vis,false,sizeof (VIS));    memset (pre,-1,sizeof (pre));    Q.push (start);    Vis[start] = true; while (!        Q.empty ()) {int u = q.front ();        if (U = = end) return true;        Q.pop (); for (int i = 0; I <= N; ++i) {if (Map[u][i] &&!vis[i]) {Vis[i] = t                Rue                Pre[i] = u;            Q.push (i); }}} return false;}    int ekmaxflow (int start,int end) {int v,ans = 0,minn;        while (EKBFS (Start,end)) {minn = 0xffffff0;        v = end;            while (pre[v]! =-1) {minn = min (Minn,map[pre[v]][v]);        v = pre[v];       } Ans + = Minn; v = end;            while (pre[v]! =-1) {MAP[PRE[V]][V]-= Minn;            MAP[V][PRE[V]] + = Minn;        v = pre[v]; }} return Ans;    int main () {int t,u,v,w, Kase = 0;    scanf ("%d", &t);        while (t--) {memset (map,0,sizeof (MAP));        scanf ("%d%d", &n,&m);            for (int i = 0; i < M; ++i) {scanf ("%d%d%d", &u,&v,&w);        MAP[U][V] + = W;    } printf ("Case%d:%d\n", ++kase, Ekmaxflow (1,n)); } return 0;}


HDU3549 flow problem "Maximum Flow" "Edmond-karp"

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.