POJ 1273 最水最水的网络最大流
2014-03-22 19:05
513 查看
直接最大流就可以了。没有任何困难,就是要注意数据范围,每组数据之后清零就可以了。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <vector> #define INF 0x7fffffff #define maxn 205 #define maxm 300000 using namespace std; int vis[maxn],cur[maxn],d[maxn],s,m,n,t; struct Edge{ int from,to,cap,flow; }; vector<int>G[maxn]; vector<Edge>edges; void init() { for(int i=1;i<=n;i++)G[i].clear(); edges.clear(); } void AddEdge(int from,int to,int cap) { edges.push_back((Edge){from,to,cap,0}); edges.push_back((Edge){to,from,0,0}); int m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BFS() { memset(vis,0,sizeof vis); queue<int>Q; Q.push(s); d[s]=0; vis[s]=1; while(!Q.empty()) { int x = Q.front();Q.pop(); int len = G[x].size(); for(int i=0;i<len;i++) { Edge &e = edges[G[x][i]]; if(!vis[e.to]&&e.cap>e.flow) { vis[e.to]=1; d[e.to]=d[x]+1; Q.push(e.to); } } } return vis[t]; } int DFS(int x ,int a) { if(x==t||a==0)return a; int flow = 0,f; int len = G[x].size(); for(int &i = cur[x];i<len ;i++) { Edge &e = edges[G[x][i]]; if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0) { e.flow+=f; edges[G[x][i]^1].flow-=f; flow+=f; a-=f; if(a==0)break; } } return flow; } int Maxflow(int s ,int t) { int flow=0; while(BFS()) { memset(cur,0,sizeof cur); flow+=DFS(s,INF); } return flow; } int main() { // freopen("in.txt","r",stdin); while(~scanf("%d%d",&m,&n)) { init(); int u,v,w; for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); AddEdge(u,v,w); } s=1;t=n; printf("%d\n",Maxflow(s,t)); } return 0; }
相关文章推荐
- POJ-1273-Drainage Ditches(网络最大流 标号法)
- POJ-1273-Drainage Ditches(网络最大流)
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
- POJ 1273 Drainage Ditches (网络最大流)
- POJ 1273~Drainage Ditches(网络最大流ek,dinic)
- POJ:1273 Drainage Ditches(网络最大流模版题)
- 【POJ】1273 Drainage Ditches 网络最大流
- poj 1273 网络最大流(ek)
- isap算法模板poj 1273gap+弧优化 最大流
- poj 1273Drainage Ditches 最大流
- POJ1273--Drainage Ditches(最大流)
- POJ 1273 最大流
- POJ 1273 Drainage Ditches——最大流
- poj - 1273 - Drainage Ditches(最大流)
- POJ 1149 PIGS (网络最大流 Dinic 建对图你就赢了)
- POJ 1698 Alice's Chance 二分图多重匹配 网络最大流
- POJ-1273 Drainage Ditches【最大流】
- POJ 1273-Drainage Ditches(网络流_最大流_ISAP()算法和EK()算法)
- poj(1273)(最大流)
- POJ - 1273 Drainage Ditches (最大流)