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 using namespace std;
  9
 10 const int maxn = 50*50+10;
 11
 12 const int INF = 1000000000;
 13
 14 struct Edge
 15 {
 16   int from, to, cap, flow;
 17 };
 18
 19 bool operator < (const Edge& a, const Edge& b)
 20 {
 21   return a.from < b.from || (a.from == b.from && a.to < b.to);
 22 }
 23
 24 struct Dinic
 25 {
 26     int n, m, s, t;
 27     vector<Edge> edges;    // 边数的两倍
 28     vector<int> G[maxn];   // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
 29     bool vis[maxn];        // BFS使用
 30     int d[maxn];           // 从起点到i的距离
 31     int cur[maxn];         // 当前弧指针
 32
 33     void init(int n)
 34     {
 35         for(int i = 0; i < n; i++) G[i].clear();
 36         edges.clear();
 37     }
 38
 39     void AddEdge(int from, int to, int cap)
 40     {
 41         edges.push_back((Edge){from, to, cap, 0});
 42         edges.push_back((Edge){to, from, 0, 0});
 43         m = edges.size();
 44         G[from].push_back(m-2);
 45         G[to].push_back(m-1);
 46     }
 47
 48     bool BFS()
 49     {
 50         memset(vis, 0, sizeof(vis));
 51         queue<int> Q;
 52         Q.push(s);
 53         vis[s] = 1;
 54         d[s] = 0;
 55         while(!Q.empty())
 56         {
 57             int x = Q.front(); Q.pop();
 58             for(int i = 0; i < G[x].size(); i++)
 59             {
 60                 Edge& e = edges[G[x][i]];
 61                 if(!vis[e.to] && e.cap > e.flow)
 62                 {
 63                     vis[e.to] = 1;
 64                     d[e.to] = d[x] + 1;
 65                     Q.push(e.to);
 66                 }
 67             }
 68         }
 69         return vis[t];
 70     }
 71
 72     int DFS(int x, int a)
 73     {
 74         if(x == t || a == 0) return a;
 75         int flow = 0, f;
 76         for(int& i = cur[x]; i < G[x].size(); i++)
 77         {
 78             Edge& e = edges[G[x][i]];
 79             if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap-e.flow))) > 0)
 80             {
 81                 e.flow += f;
 82                 edges[G[x][i]^1].flow -= f;
 83                 flow += f;
 84                 a -= f;
 85                 if(a == 0) break;
 86             }
 87         }
 88         return flow;
 89     }
 90
 91     int Maxflow(int s, int t)
 92     {
 93         this->s = s; this->t = t;
 94         int flow = 0;
 95         while(BFS())
 96         {
 97             memset(cur, 0, sizeof(cur));
 98             flow += DFS(s, INF);
 99         }
100         return flow;
101     }
102 };
103
104 Dinic g;
105
106 int main()
107 {
108   //  freopen("a.txt","r",stdin);
109     int n,m,a,b,c;
110     while(~scanf("%d%d",&m,&n))
111     {
112         g.init(n);
113         for(int i=0;i<m;i++)
114         {
115             scanf("%d%d%d",&a,&b,&c);
116             g.AddEdge(a,b,c);
117         }
118         printf("%d\n",g.Maxflow(1, n));
119     }
120     return 0;
121 }
时间: 2024-10-27 06:51:36

hdu - 1532 Drainage Ditches (最大流)的相关文章

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(最大流 EK算法)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用"+="替换"=". 对网络流不熟悉的,给一篇讲解:http://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html. ?(? ? ??)我是看这篇博客才入门的. 代码: 1 #include <cstdio> 2 #includ

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 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+BFS解决.每次搜到一条到达终点的路径,就立刻退出,更新ans,然后再回头修改图中的当前flow状况(这就得靠记录路径了).当当前图没有到达终点的路径图,流已经饱和,可以结束程序了. 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define pii pair<int,int> 4 #define INF 0x7f7f7

HDU 1532 Drainage Ditches

http://acm.hdu.edu.cn/showproblem.php?pid=1532 基础题. 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 8 int n, m, flow; 9 int vis[205]; 10 //路径记录 11 i