hdu 3996 Gold Mine 最大权闭合子图
Gold Mine
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2374 Accepted Submission(s): 514
Problem Description
Long long ago, there is a gold mine.The mine consist of many layout, so some area is easy to dig, but some is very hard to dig.To dig one gold, we should cost some value and then gain some value. There are many area that have gold, because of the layout, if one people want to dig one gold in some layout, he must dig some gold on some layout that above this gold's layout. A gold seeker come here to dig gold.The question is how much value the gold he can dig, suppose he have infinite money in the begin.
Input
First line the case number.(<=10)
Then for every case:
one line for layout number.(<=100)
for every layout
first line gold number(<=25)
then one line for the dig cost and the gold value(32bit integer), the related gold number that must be digged first(<=50)
then w lines descripte the related gold followed, each line two number, one layout num, one for the order in that layout
see sample for details
Then for every case:
one line for layout number.(<=100)
for every layout
first line gold number(<=25)
then one line for the dig cost and the gold value(32bit integer), the related gold number that must be digged first(<=50)
then w lines descripte the related gold followed, each line two number, one layout num, one for the order in that layout
see sample for details
Output
Case #x: y.
x for case number, count from 1.
y for the answer.
x for case number, count from 1.
y for the answer.
Sample Input
1
2
1
10 100 0
2
10 100 1
1 1
10 100 1
1 1
Sample Output
Case #1: 270
题意:有一个矿,其中分为很多个区域。每个区域里面有黄金。每个黄金有价值,挖黄金需要一定的花费。但是挖其中的一些黄金必须要先挖另一些黄金。
思路:挖其中的一些黄金需要先挖另一些黄金。显示了必要条件。所以可以用最大权闭合子图来解决。
建图:增加源点s和汇点t。每个点和源点连一条边,容量为黄金的价值。每个点和汇点连一条边,容量为所需要的花费。
如果挖i个黄金需要先挖j黄金,那么从i到j连一条有向边,容量为正无穷。
要注意的是最后的结果可能会超int,要用long long。
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define maxn 3200 4 const long long inf = 1ll<<60; 5 struct Edge 6 { 7 int from, to; 8 long long cap, flow; 9 Edge(int f, int t, long long c, long long fl) 10 { 11 from = f; to = t; cap = c; flow = fl; 12 } 13 }; 14 long long min(long long a, long long b) 15 { 16 return a<b?a:b; 17 }; 18 vector <Edge> edges; 19 vector <int> G[maxn]; 20 int n, m, s, t; 21 int cur[maxn], vis[maxn], d[maxn]; 22 void AddEdge(int from, int to, long long cap) 23 { 24 edges.push_back(Edge(from, to, cap, 0)); 25 edges.push_back(Edge(to, from, 0, 0)); 26 m = edges.size(); 27 G[from].push_back(m-2); 28 G[to].push_back(m-1); 29 } 30 bool bfs() 31 { 32 memset(vis, 0, sizeof(vis)); 33 d[s] = 0; 34 vis[s] = 1; 35 queue <int> q; 36 q.push(s); 37 while(!q.empty()) 38 { 39 int u = q.front(); q.pop(); 40 for(int i = 0; i < G[u].size(); i++) 41 { 42 Edge &e = edges[G[u][i]]; 43 if(!vis[e.to] && e.cap > e.flow) 44 { 45 d[e.to] = d[u]+1; 46 vis[e.to] = 1; 47 q.push(e.to); 48 } 49 } 50 } 51 return vis[t]; 52 } 53 long long dfs(int x, long long a) 54 { 55 if(x == t || a == 0) return a; 56 long long flow = 0, f; 57 for(int &i = cur[x]; i < G[x].size(); i++) 58 { 59 Edge &e = edges[G[x][i]]; 60 if(d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) 61 { 62 e.flow += f; 63 edges[G[x][i]^1].flow -= f; 64 flow += f; 65 a -= f; 66 if(a == 0) break; 67 } 68 } 69 return flow; 70 } 71 long long Maxflow() 72 { 73 long long flow = 0; 74 while(bfs()) 75 { 76 memset(cur, 0, sizeof(cur)); 77 flow += dfs(s, inf); 78 } 79 return flow; 80 } 81 int T, N; 82 int main() 83 { 84 scanf("%d", &T); 85 int cast = 0; 86 while(T--) 87 { 88 cast++; 89 edges.clear(); 90 for(int i = 0; i < maxn; i++) G[i].clear(); 91 scanf("%d", &N); 92 s = 0; t = N*25+1; n = N; 93 long long sum = 0; 94 for(int i = 1; i <= N; i++) 95 { 96 int cnt; scanf("%d", &cnt); 97 for(int j = 1; j <= cnt; j++) 98 { 99 long long value, cost; 100 scanf("%lld%lld",&cost, &value); 101 sum += value; 102 AddEdge(s, (i-1)*25+j, value); 103 AddEdge((i-1)*25+j, t, cost); 104 int w; scanf("%d", &w); 105 while(w--) 106 { 107 int a, b; scanf("%d%d", &a, &b); 108 AddEdge((i-1)*25+j, (a-1)*25+b, inf); 109 } 110 } 111 } 112 long long flow = Maxflow(); 113 printf("Case #%d: %lld\n", cast, sum-flow); 114 } 115 return 0; 116 }