标签:des style blog http color os
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 15724 | Accepted: 7023 |
Description
Input
Output
Sample Input
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6
Sample Output
7
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 8 using namespace std; 9 10 const int MAX_M = 1005; 11 const int MAX_N =105; 12 int M, N; 13 int pig[MAX_M]; 14 struct Edge {int from, to, cap, flow;}; 15 vector <int> p[MAX_M]; 16 int buy[MAX_N]; 17 vector <int> G[MAX_M]; 18 vector <Edge> edges; 19 int _max = 0; 20 int d[MAX_N]; 21 int cur[MAX_N]; 22 bool vis[MAX_N]; 23 24 void add_edge(int from, int to, int cap) { 25 edges.push_back((Edge) {from, to, cap, 0}); 26 edges.push_back((Edge) {to, from, 0, 0}); 27 int m = edges.size(); 28 G[from].push_back(m - 2); 29 G[to].push_back(m - 1); 30 } 31 32 33 34 void solve() { 35 for (int i = 1; i <= N; ++i) { 36 add_edge(i, N + 1, buy[i]); 37 } 38 39 for (int i = 1; i <= M; ++i) { 40 if (p[i].size()) { 41 add_edge(0, p[i][0], pig[i]); 42 for (int j = 0; j < p[i].size() - 1; ++j) { 43 add_edge(p[i][j], p[i][j + 1], _max); 44 } 45 } 46 } 47 48 } 49 50 bool BFS(int s, int t) { 51 memset(vis, 0, sizeof(vis)); 52 queue <int> q; 53 q.push(s); 54 d[s] = 0; 55 vis[s] = 1; 56 while (!q.empty()) { 57 int x = q.front(); q.pop(); 58 for (int i = 0; i < G[x].size(); ++i) { 59 Edge &e = edges[ G[x][i] ]; 60 if (!vis[e.to] && e.cap > e.flow) { 61 vis[ e.to ] = 1; 62 d[e.to] = d[x] + 1; 63 q.push(e.to); 64 } 65 } 66 } 67 68 return vis[t]; 69 } 70 71 int DFS(int x, int a, int t) { 72 if (x == t || a == 0) return a; 73 int flow = 0, f; 74 for (int &i = cur[x]; i < G[x].size(); ++i) { 75 Edge &e = edges[ G[x][i] ]; 76 if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow), t))> 0) { 77 e.flow += f; 78 edges[ G[x][i] ^ 1].flow -= f; 79 flow += f; 80 a -= f; 81 if (a == 0) break; 82 } 83 } 84 85 return flow; 86 } 87 88 int Maxflow(int s, int t) { 89 int flow = 0; 90 while (BFS(s, t)) { 91 memset(cur, 0, sizeof(cur)); 92 flow += DFS(s, _max, t); 93 } 94 95 return flow; 96 } 97 98 int main() 99 { 100 //freopen("sw.in", "r", stdin); 101 scanf("%d%d", &M, &N); 102 for (int i = 1; i <= M; ++i) { 103 scanf("%d", &pig[i]); 104 _max += pig[i]; 105 } 106 for (int i = 1; i <= N; ++i) { 107 int n; 108 scanf("%d", &n); 109 for (int j = 1; j <= n; ++j) { 110 int ch; 111 scanf("%d", &ch); 112 p[ch].push_back(i); 113 } 114 scanf("%d", &buy[i]); 115 } 116 117 solve(); 118 printf("%d\n", Maxflow(0, N + 1)); 119 120 121 //cout << "Hello world!" << endl; 122 return 0; 123 }
标签:des style blog http color os
原文地址:http://www.cnblogs.com/hyxsolitude/p/3848849.html