POJ 1273(EK)

题目大概意思是,有N条水沟和M个水池,问从第一个水池到最后一个水池在同一时间内能够流过多少水
第一行有两个整数N,M
接下来N行,每行有3个整数,a,b,c,代表从a到b能够流c单位的水
超级模板题,一个有向图,源点为1,汇点为M,套最大流模板就行了

我就通过这水题理解EK的...


 1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<queue>
5 #include<climits>
6 using namespace std;
7 #define N 210
8 #define clr(a,b) memset(a,b,sizeof(a))
9 int cap[N][N];///容量
10 int n,m;
11 int a,b,c;
12 int EK(int s,int t)///s代表源点,t代表汇点
13 {
14 queue<int> q;
15 int flow[N][N];///流量
16 int low[N];///low[v]表示s-v的最小残量
17 int pre[N];///用于BFS寻找父亲节点
18 int u,v;
19 int maxflow = 0;///最大流
20 clr(flow,0);///初始时残量网络为0
21 while(1)
22 {
23 q.push(s);///把源点压入队列中
24 clr(low,0);
25 low[s] = INT_MAX;
26 while(!q.empty())
27 {
28 u = q.front();
29 q.pop();
30 for(v = 0; v <= t; v ++)
31 {
32 if(!low[v] && cap[u][v] > flow[u][v])///找到新节点v
33 {
34 q.push(v);
35 low[v] = min(low[u], cap[u][v] - flow[u][v]);
36 pre[v] = u;
37 }
38 }
39 }
40 if(low[t] == 0) break;///找不到,则当前流已经是最大
41 for(u = t; u != s; u = pre[u])
42 {
43 flow[pre[u]][u] += low[t];
44 flow[u][pre[u]] -= low[t];
45 }
46 maxflow += low[t];
47 }
48 return maxflow;
49 }
50 int main()
51 {
52 int ans;
53 while(~scanf("%d%d",&n,&m))
54 {
55 clr(cap,0);
56
57 for(int i=0; i<n; i++)
58 {
59 scanf("%d%d%d",&a,&b,&c);
60 cap[a][b] += c;
61 }
62 ans = EK(1,m);
63 printf("%d\n",ans);
64 }
65 return 0;
66 }

时间: 2024-08-06 11:36:00

POJ 1273(EK)的相关文章

POJ 1273 Drainage Ditches(我的EK算法模板)

题意:给你n条边,目标位置t:接下来是每条边,包括起点,终点,容量: 感想:第一道最大流的代码,这道题是我更深地理解了Ek算法,不过这道题有个超坑的情况,那就是出现重边的情况==! 思路:EK算法 AC代码: #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; #define INF 100000000 #define N

UVA 820 --- POJ 1273 最大流

找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的时候注意一下. 而且我居然犯了更愚蠢的错误,以为重边的时候需要选最大的,正解应该是累加.... 1 #include<stdio.h> 2 #include<queue> 3 #include<string.h> 4 #define INF 999999 5 using n

POJ 1273 Drainage Ditches(初识网络流)

开始研究网络流了,看了两个晚上吧,今天总算动手实践一下,有了更深的理解 总结一下:在最大流中,容量与实际流量满足3点: 1.实际流量<=容量 2.任意两点之间   : 流量(a->b)==流量(b->a) 3.流量守恒原则   :从s流出的流量 == t流入的流量 一.为什么叫增广路,因为在所有的流量网络中,会存在一个残量,所以在整个残量网络中,找到一个最小值,加到所有的流量线路里,便叫增广. 二.为什么要修改反向流量,因为在更新流量网时,当前选择的并不一定就是最优解,比如u->v

hdu 1532 poj 1273 Drainage Ditches (最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55276   Accepted: 21122 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55235   Accepted: 21104 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

poj 1273 Drainage Ditches (最大流入门)

1 /****************************************************************** 2 题目: Drainage Ditches(POJ 1273) 3 链接: http://poj.org/problem?id=1273 4 题意: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条 5 水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水 6 渠中所能流过的水的最大容量.水流是单向的. 7 算法: 最大流之增广路(入门)

POJ 1273 最大流 Dinic

Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56802 Accepted: 21824 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by wate

POJ 1273 Drainage Ditches 最大流

很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include