标签:
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3549
题目大意:
给你一个N个节点M条边的加权有向图,源点为1,汇点为N,求此图的最大流。
思路:
这道题就是网络流求最大流的裸题。直接用Edmond-Karp算法来做就可以了。
AC代码:
#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 start,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] = true; 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【最大流】【Edmond-Karp】
标签:
原文地址:http://blog.csdn.net/lianai911/article/details/43956529