poj3469 Dual Core CPU
Posted 王宜鸣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3469 Dual Core CPU相关的知识,希望对你有一定的参考价值。
思路:
最小割。
实现:
1 #include <iostream> 2 #include <vector> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 const int MAXV = 20005, INF = 0x3f3f3f3f; 8 struct edge 9 { 10 int to, cap, rev; 11 edge(int t, int c, int r) { to = t; cap = c; rev = r; } 12 }; 13 int level[MAXV]; 14 int iter[MAXV]; 15 vector<edge> G[MAXV]; 16 void add_edge(int from, int to, int cap) 17 { 18 edge e(to, cap, G[to].size()); 19 G[from].push_back(e); 20 edge er(from, 0, G[from].size() - 1); 21 G[to].push_back(er); 22 } 23 void bfs(int s) 24 { 25 memset(level, -1, sizeof level); 26 queue<int> q; 27 level[s] = 0; 28 q.push(s); 29 while (!q.empty()) 30 { 31 int v = q.front(); q.pop(); 32 for (int i = 0; i < G[v].size(); i++) 33 { 34 edge& e = G[v][i]; 35 if (e.cap > 0 && level[e.to] < 0) 36 { 37 level[e.to] = level[v] + 1; 38 q.push(e.to); 39 } 40 } 41 } 42 } 43 int dfs(int v, int t, int f) 44 { 45 if (v == t) return f; 46 for (int& i = iter[v]; i < G[v].size(); i++) 47 { 48 edge& e = G[v][i]; 49 if (e.cap > 0 && level[v] < level[e.to]) 50 { 51 int d = dfs(e.to, t, min(f, e.cap)); 52 if (d > 0) 53 { 54 e.cap -= d; 55 G[e.to][e.rev].cap += d; 56 return d; 57 } 58 } 59 } 60 return 0; 61 } 62 int max_flow(int s, int t) 63 { 64 int flow = 0; 65 for (;;) 66 { 67 bfs(s); 68 if (level[t] < 0) return flow; 69 memset(iter, 0, sizeof iter); 70 int f = 0; 71 while ((f = dfs(s, t, INF)) > 0) 72 flow += f; 73 } 74 } 75 76 int main() 77 { 78 ios::sync_with_stdio(false); 79 int n, m, x, y, w; 80 cin >> n >> m; 81 for (int i = 1; i <= n; i++) 82 { 83 cin >> x >> y; 84 add_edge(0, i, x); 85 add_edge(i, n + 1, y); 86 } 87 for (int i = 1; i <= m; i++) 88 { 89 cin >> x >> y >> w; 90 add_edge(x, y, w); 91 add_edge(y, x, w); 92 } 93 cout << max_flow(0, n + 1) << endl; 94 return 0; 95 }
以上是关于poj3469 Dual Core CPU的主要内容,如果未能解决你的问题,请参考以下文章