poj1273
来源:互联网 发布:java之父 aws 编辑:程序博客网 时间:2024/06/06 16:32
Problem : Drainage Ditches
Description : 有n个池塘,m条水渠,每条水渠的流量是固定的,给出这m条水渠所连接的池塘和所能流过的水量,求水渠中所能流过的水的最大容量.
Solution : 最大流的入门题。需要注意的是会有重边出现,那么重边的流量会累加。
Code (C++) :
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define N 210#define inf 0x3f3f3f3fusing namespace std;int map[N][N];int pre[N],n;queue<int >q;bool Bfs(int src,int des){ int index; memset(pre,-1,sizeof(pre)); while(!q.empty()) q.pop(); pre[src]=0; q.push(src); while(!q.empty()) { index=q.front(); q.pop(); for(int i=0; i<=n+1; i++) { if(pre[i]==-1&&map[index][i]>0) { pre[i]=index; if(i==des) return true; q.push(i); } } } return false;}int MaxFlow(int src,int des){ int maxflow=0; while(Bfs(src,des)) { int minflow=inf; for(int i=des; i!=src; i=pre[i]) if(minflow>map[pre[i]][i]) minflow=map[pre[i]][i]; for(int i=des; i!=src; i=pre[i]) { map[pre[i]][i]-=minflow; map[i][pre[i]]+=minflow; } maxflow+=minflow; } return maxflow;}int main (){ //freopen("in.txt","r",stdin); int m,f,u,v; while(cin>>m>>n) { memset(map,0,sizeof(map)); for(int i=0; i<m; i++) { cin>>u>>v>>f; map[u][v]+=f;//可能会有重边出现 } int ans=MaxFlow(1,n); cout<<ans<<endl; } return 0;}
0 0
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- POJ1273
- poj1273
- POJ1273
- poj1273 EK
- POJ1273 Drainage Ditches
- POJ1273(最大流)
- poj1273 Drainage Ditches
- 【hihocoder1039】字符消除——模拟
- preg_replace的使用方法
- Windows编程 从消息窗口到基本窗口 游戏循环窗口框架的简单实现
- system ui 1
- Robi改造计划更新---框架终于搭出来了
- poj1273
- Android学习笔记--4.activity的声明周期
- Linux命令大全
- OC与swift混编
- 数列分段
- 234. Palindrome Linked List
- 5毛党
- 220. Contains Duplicate III
- 不要做浮躁的嵌入式系统工程师