标签:
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2374 Accepted Submission(s): 514
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 }
标签:
原文地址:http://www.cnblogs.com/titicia/p/4700303.html