Drainage Ditches(POJ1273+網絡流+Dinic+EK)
阿新 • • 發佈:2018-08-03
ace update con txt lib sig 技術 lse 實現
題目鏈接:poj.org/problem?id=1273
題目:
題意:求最大流。
思路:測板子題,分別用Dinic和EK實現(我的板子跑得時間均為0ms)。
Dinic代碼實現如下:
1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <stack> 5 #include <cmath> 6 #include <bitset> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstdlib> 11 #include <cstring> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 typedef long long ll; 17 typedef pair<ll, ll> pll; 18 typedef pair<int, ll> pil;; 19 typedef pair<int, int> pii; 20 typedef unsigned long long ull; 21 22 #define lson i<<1 23 #define rson i<<1|1 24 #define bug printf("*********\n"); 25 #define FIN freopen("D://code//in.txt", "r", stdin); 26 #define debug(x) cout<<"["<<x<<"]" <<endl; 27 #define IO ios::sync_with_stdio(false),cin.tie(0); 2829 const double eps = 1e-8; 30 const int mod = 10007; 31 const int maxn = 200 + 7; 32 const double pi = acos(-1); 33 const int inf = 0x3f3f3f3f; 34 const ll INF = 0x3f3f3f3f3f3f3f; 35 36 37 int n, m, tot, maxflow, s, t, u, v, w; 38 int head[maxn<<1], d[maxn<<1]; 39 40 queue<int> q; 41 42 struct edge { 43 int v, w, next; 44 }ed[maxn<<1]; 45 46 void addedge(int u, int v, int w) { 47 ed[tot].v = v; 48 ed[tot].w = w; 49 ed[tot].next = head[u]; 50 head[u] = tot++; 51 ed[tot].v = u; 52 ed[tot].w = 0; 53 ed[tot].next = head[v]; 54 head[v] = tot++; 55 } 56 57 bool bfs() { 58 memset(d, 0, sizeof(d)); 59 while(!q.empty()) q.pop(); 60 q.push(s); 61 d[s] = 1; 62 while(!q.empty()) { 63 int x = q.front(); q.pop(); 64 for(int i = head[x]; ~i; i = ed[i].next) { 65 if(ed[i].w && !d[ed[i].v]) { 66 q.push(ed[i].v); 67 d[ed[i].v] = d[x] + 1; 68 if(ed[i].v == t) return 1; 69 } 70 } 71 } 72 return 0; 73 } 74 75 int dinic(int x, int flow) { 76 if(x == t) return flow; 77 int rest = flow, k; 78 for(int i = head[x]; ~i && rest; i = ed[i].next) { 79 if(ed[i].w && d[ed[i].v] == d[x] + 1) { 80 k = dinic(ed[i].v, min(rest, ed[i].w)); 81 if(!k) d[ed[i].v] = 0; 82 ed[i].w -= k; 83 ed[i^1].w += k; 84 rest -= k; 85 } 86 } 87 return flow - rest; 88 } 89 90 int main() { 91 //FIN; 92 while(~scanf("%d%d", &m, &n)) { 93 s = 1, t = n; 94 tot = 0, maxflow = 0; 95 memset(head, -1, sizeof(head)); 96 for(int i = 1; i <= m; i++) { 97 scanf("%d%d%d", &u, &v, &w); 98 addedge(u, v, w); 99 } 100 int flow = 0; 101 while(bfs()) { 102 while(flow = dinic(s, inf)) { 103 maxflow += flow; 104 } 105 } 106 printf("%d\n", maxflow); 107 } 108 return 0; 109 }
EK實現如下:
1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <stack> 5 #include <cmath> 6 #include <bitset> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstdlib> 11 #include <cstring> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 typedef long long ll; 17 typedef pair<ll, ll> pll; 18 typedef pair<int, ll> pil;; 19 typedef pair<int, int> pii; 20 typedef unsigned long long ull; 21 22 #define lson i<<1 23 #define rson i<<1|1 24 #define bug printf("*********\n"); 25 #define FIN freopen("D://code//in.txt", "r", stdin); 26 #define debug(x) cout<<"["<<x<<"]" <<endl; 27 #define IO ios::sync_with_stdio(false),cin.tie(0); 28 29 const double eps = 1e-8; 30 const int mod = 10007; 31 const int maxn = 200 + 7; 32 const double pi = acos(-1); 33 const int inf = 0x3f3f3f3f; 34 const ll INF = 0x3f3f3f3f3f3f3f; 35 36 int n, m, u, v, w, tot, maxflow, s, t; 37 int head[maxn<<1], vis[maxn], incf[maxn], pre[maxn]; 38 39 struct edge { 40 int v, w, next; 41 }ed[maxn<<1]; 42 43 void addedge(int u, int v, int w) { 44 ed[tot].v = v; 45 ed[tot].w = w; 46 ed[tot].next = head[u]; 47 head[u] = tot++; 48 ed[tot].v = u; 49 ed[tot].w = 0; 50 ed[tot].next = head[v]; 51 head[v] = tot++; 52 } 53 54 bool bfs() { 55 memset(vis, 0, sizeof(vis)); 56 queue<int> q; 57 q.push(s); 58 vis[s] = 1; 59 incf[s] = inf; 60 while(!q.empty()) { 61 int x = q.front(); 62 q.pop(); 63 for(int i = head[x]; i != -1; i = ed[i].next) { 64 if(ed[i].w) { 65 int v = ed[i].v; 66 if(vis[v]) continue; 67 incf[v] = min(incf[x], ed[i].w); 68 pre[v] = i; 69 q.push(v); 70 vis[v] = 1; 71 if(v == t) return 1; 72 } 73 } 74 } 75 return 0; 76 } 77 78 void update() { 79 int x = t; 80 while(x != s) { 81 int i = pre[x]; 82 ed[i].w -= incf[t]; 83 ed[i^1].w += incf[t]; 84 x = ed[i^1].v; 85 } 86 maxflow += incf[t]; 87 } 88 89 int main() { 90 //FIN; 91 while(~scanf("%d%d", &m, &n)) { 92 s = 1, t = n; 93 tot = 0, maxflow = 0; 94 memset(head, -1, sizeof(head)); 95 memset(pre, -1, sizeof(pre)); 96 memset(incf, 0, sizeof(incf)); 97 for(int i = 1; i <= m; i++) { 98 scanf("%d%d%d", &u, &v, &w); 99 addedge(u, v, w); 100 } 101 while(bfs()) update(); 102 printf("%d\n", maxflow); 103 } 104 return 0; 105 }
Drainage Ditches(POJ1273+網絡流+Dinic+EK)