HDU 1532 Drainage Ditches(最大流 EK算法)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532

思路:

网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替换“=”。

对网络流不熟悉的,给一篇讲解:http://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html。 ?(? ? ??)我是看这篇博客才入门的。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 const int inf=10000005;
 7 int n,m;
 8 int mp[205][205];
 9 int vis[205];
10 int pre[205];
11 void update(int s,int t,int Min){
12     int k=t;
13     while (pre[k]!=-1) {
14         mp[k][pre[k]]+=Min;
15         mp[pre[k]][k]-=Min;
16         k=pre[k];
17     }
18 }
19 int bfs(int s,int t){
20     int Min=inf;
21     memset(vis, 0, sizeof(vis));
22     memset(pre, -1, sizeof(pre));
23     queue<int>q;
24     q.push(s);
25     vis[s]=1;
26     while (!q.empty()) {
27         int x=q.front();q.pop();
28         for (int i=1; i<=n; i++) {
29             if (!vis[i] && mp[x][i]>0) {
30                 pre[i]=x;
31                 Min=min(mp[x][i], Min);
32                 q.push(i);
33                 vis[i]=1;
34             }
35         }
36         if(pre[t]!=-1)  return Min;
37     }
38     return 0;
39 }
40 int edmonds_karp(int s,int t){
41     int Min=-1;
42     int Max=0;
43     while(Min!=0){
44         Min=bfs(s, t);
45         update(s,t,Min);
46         Max+=Min;
47     }
48     return Max;
49 }
50 int main(){
51     while (scanf("%d%d",&n,&m)!=EOF) {
52         memset(mp, 0, sizeof(mp));
53         for (int i=0; i<n; i++) {
54             int x,y,z;
55             scanf("%d%d%d",&x,&y,&z);
56             mp[x][y]+=z;//注意!!
57         }
58         printf("%d\n",edmonds_karp(1, m));
59     }
60     return 0;
61 }
时间: 2024-10-07 04:17:26

HDU 1532 Drainage Ditches(最大流 EK算法)的相关文章

hdu 1532 Drainage Ditches(最大流)

Drainage Ditches 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 water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drai

hdu - 1532 Drainage Ditches (最大流)

http://acm.hdu.edu.cn/showproblem.php?pid=1532 求最大的流量,用dinic算法就好. 1 // Rujia Liu 2 // 因为图较大,所以采用Dinic而不是EdmondsKarp 3 // 得益于接口一致性,读者无须理解Dinic就能使用它. 4 #include<cstdio> 5 #include<cstring> 6 #include<queue> 7 #include<algorithm> 8 us

HDU 1532 Drainage Ditches 最大排水量 网络最大流 Edmonds_Karp算法

题目链接:HDU 1532 Drainage Ditches 最大排水量 Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9641    Accepted Submission(s): 4577 Problem Description Every time it rains on Farmer John

HDU 3549 Flow Problem ( 最大流 -EK 算法)

C++,G++的读取速度差距也太大了 Flow Problem 题意:n,m表示n个点m条有向带权边 问:从1-n最大流多少 裸最大流,拿来练手,挺不错的 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; #define

HDU 1532 Drainage Ditches (网络流)

A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

HDU - 1532 - Drainage Ditches &amp;&amp; 3549 - Flow Problem (网络流初步)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10875    Accepted Submission(s): 5131 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie'

HDU 1532 Drainage Ditches (最大网络流)

Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Every time it rains on

hdu 1532 Drainage Ditches(edmond-karp最大流算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 #include <stdio.h> #include <queue> #include <string.h> #include <algorithm> #define INT_MAX (int)1e9 using namespace std; const int N = 207; int network[N][N], pre[N], used[N], f

HDU 1532 Drainage Ditches EK算法

题意:输入m n, m是边数,n是点数. 接下来m行: 起点,终点,容量.求以 1 为源点, n为汇点的最大流. #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int INF = 0xfffffff; const int MAXN = 200 + 10; //邻接矩阵存放图. int flow[MAXN][