HDU 1532 Drainage Ditches

Posted zaq19970105

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1532 Drainage Ditches相关的知识,希望对你有一定的参考价值。

题目链接:https://vjudge.net/problem/HDU-1532

题目大意

  给定 m 个点,n 条边,以及每条边的容量 c,求从原点 1 到汇点 m 的最大流。

分析:

  网络流模板题。

代码如下

技术图片
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3  
  4 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 12  
 13 #define pr(x) cout << #x << " = " << x << "  "
 14 #define prln(x) cout << #x << " = " << x << endl
 15  
 16 #define LOWBIT(x) ((x)&(-x))
 17  
 18 #define ALL(x) x.begin(),x.end()
 19 #define INS(x) inserter(x,x.begin())
 20  
 21 #define ms0(a) memset(a,0,sizeof(a))
 22 #define msI(a) memset(a,inf,sizeof(a))
 23 #define msM(a) memset(a,-1,sizeof(a))
 24 #define mcy(d,s) memcpy(d, s, sizeof(s))
 25 
 26 #define MP make_pair
 27 #define PB push_back
 28 #define ft first
 29 #define sd second
 30  
 31 template<typename T1, typename T2>
 32 istream &operator>>(istream &in, pair<T1, T2> &p) {
 33     in >> p.first >> p.second;
 34     return in;
 35 }
 36  
 37 template<typename T>
 38 istream &operator>>(istream &in, vector<T> &v) {
 39     for (auto &x: v)
 40         in >> x;
 41     return in;
 42 }
 43  
 44 template<typename T1, typename T2>
 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 46     out << "[" << p.first << ", " << p.second << "]" << "\n";
 47     return out;
 48 }
 49 
 50 inline int gc(){
 51     static const int BUF = 1e7;
 52     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 53     
 54     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 55     return *bg++;
 56 } 
 57 
 58 inline int ri(){
 59     int x = 0, f = 1, c = gc();
 60     for(; c<48||c>57; f = c==-?-1:f, c=gc());
 61     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 62     return x*f;
 63 }
 64  
 65 typedef long long LL;
 66 typedef unsigned long long uLL;
 67 typedef pair< double, double > PDD;
 68 typedef pair< int, int > PII;
 69 typedef pair< string, int > PSI;
 70 typedef set< int > SI;
 71 typedef vector< int > VI;
 72 typedef map< int, int > MII;
 73 typedef pair< LL, LL > PLL;
 74 typedef vector< LL > VL;
 75 typedef vector< VL > VVL;
 76 const double EPS = 1e-10;
 77 const LL inf = 0x7fffffff;
 78 const LL infLL = 0x7fffffffffffffffLL;
 79 const LL mod = 1e9 + 7;
 80 const int maxN = 2e2 + 7;
 81 const LL ONE = 1;
 82 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
 83 const LL oddBits = 0x5555555555555555;
 84 
 85 struct Vertex{
 86     VI edges;
 87 };
 88 
 89 struct Edge{
 90     int from, to;
 91     int r; // 残余容量
 92 };
 93 
 94 Edge e[maxN]; 
 95 Vertex v[maxN];
 96 int elen;
 97 int n, m;
 98 
 99 int pre[maxN]; //  // 从 s - t 中的一个可行流中, Pre[i]表示指向节点 i 的边
100 bool vis[maxN]; // 标记一个点是否被访问过
101 
102 // bfs寻找增广,如果找到就返回true,没找到就返回false
103 bool bfs(int S, int T) {
104     queue< int > Q;
105     msM(pre);
106     ms0(vis);
107     vis[S] = 1;
108     Q.push(S);
109     
110     while(!Q.empty()) {
111         int tmpQ = Q.front();
112         Q.pop();
113         
114         foreach(i, v[tmpQ].edges) {
115             int to = e[*i].to;
116             if(e[*i].r == 0 || vis[to]) continue;
117             vis[to] = true;
118             pre[to] = *i;
119             if(to == T) return true;
120             Q.push(to);
121         }
122     }
123     return false;
124 }
125 
126 // EdmondsKarp算法求最大流
127 // S 源点 
128 // T 汇点 
129 int maxFlow(int S, int T) {
130     int ret = 0;
131     
132     // 不断寻找增广路 
133     while(bfs(S, T)) {
134         // 找可行流中残余流量最小的边 
135         int t = T;
136         int mi = inf;
137         while(pre[t] != -1) {
138             mi = min(mi, e[pre[t]].r);
139             t = e[pre[t]].from;
140         }
141         
142         // 更新可行流的所有边,减去最小残余流量 
143         ret += mi;
144         t = T;
145         while(pre[t] != -1) {
146             e[pre[t]].r -= mi;
147             t = e[pre[t]].from;
148         }
149     }
150     return ret;
151 }
152 
153 int main(){
154     INIT();
155     while(cin >> n >> m) {
156         elen = 0;
157         For(i, 1, m) v[i].edges.clear();
158         
159         Rep(i, n) {
160             int S, E, C;
161             cin >> S >> E >> C;
162             e[elen].from = S;
163             e[elen].to = E;
164             e[elen].r = C;
165             v[S].edges.PB(elen++);
166         }
167         
168         cout << maxFlow(1, m) << endl;
169     }
170     return 0;
171 }
View Code

 

以上是关于HDU 1532 Drainage Ditches的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1532 Drainage Ditches

HDU-1532-Drainage Ditches

HDU 1532 Drainage Ditches

hdu1532 Drainage Ditches(最大流)

HDU1532 Drainage Ditches (网络流)

hdu 1532 Drainage Ditches(最大流)