FZU 1980 AbOr's story
AbOr's story
64-bit integer IO format: %I64d Java class name: Main
As we all known, AbOr has a lots of girls in a palace called “Hou” (About 30 millions).
The girls in the palace love gemstones, such as cat’s eye, garnet, amethyst and so on. If AbOr wants to play with one girl, he must take all the gemstones the girl wants. But the girl would not take any gemstones away. Though the gemstone is expensive, but AbOr is a businessman and he has infinity money. If AbOr plays with one girl, he will feel happy and she will help AbOr to make some money. AbOr likes the new and loathes the old, so if he plays with one girl he used to play with, he will not feel happy any more, of course make no money. Initially AbOr has no gemstones.
The evil AbOr want to make as much money as possible. But he is very busy, so your task is to help him to calculate the maximum money he can make.
Input
Output
For each test case, print a line containing the test case number (beginning with 1) and the maximum money AbOr can make.
Sample Input
3 1 2 2 1 2 100 3 7 1 2 1 1 100 1000 10000 2 1 1 1 100 1 1 16 17
Sample Output
Case 1: 90 Case 2: 0 Case 3: 99
Source
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 50010; 18 struct arc{ 19 int to,next; 20 LL flow; 21 arc(int x = 0,LL y = 0,int z = -1){ 22 to = x; 23 flow = y; 24 next = z; 25 } 26 }; 27 arc e[600000]; 28 int head[maxn],d[maxn],cur[maxn]; 29 int tot,S,T,n,m; 30 void add(int u,int v,LL flow){ 31 e[tot] = arc(v,flow,head[u]); 32 head[u] = tot++; 33 e[tot] = arc(u,0,head[v]); 34 head[v] = tot++; 35 } 36 bool bfs(){ 37 memset(d,-1,sizeof(d)); 38 d[S] = 1; 39 queue<int>q; 40 q.push(S); 41 while(!q.empty()){ 42 int u = q.front(); 43 q.pop(); 44 for(int i = head[u]; ~i; i = e[i].next){ 45 if(e[i].flow && d[e[i].to] == -1){ 46 d[e[i].to] = d[u] + 1; 47 q.push(e[i].to); 48 } 49 } 50 } 51 return d[T] > -1; 52 } 53 LL dfs(int u,LL low){ 54 if(u == T) return low; 55 LL tmp = 0,a; 56 for(int &i = cur[u]; ~i; i = e[i].next){ 57 if(e[i].flow && d[e[i].to] == d[u] + 1 &&(a=dfs(e[i].to,min(e[i].flow,low)))){ 58 e[i].flow -= a; 59 e[i^1].flow += a; 60 tmp += a; 61 low -= a; 62 if(!low) break; 63 } 64 } 65 if(!tmp) d[u] = -1; 66 return tmp; 67 } 68 LL dinic(){ 69 LL ans = 0; 70 while(bfs()){ 71 memcpy(cur,head,sizeof(head)); 72 ans += dfs(S,INF); 73 } 74 return ans; 75 } 76 int main() { 77 int csn = 1,cs,ks,u,v,w; 78 scanf("%d",&cs); 79 while(cs--){ 80 scanf("%d %d",&n,&m); 81 memset(head,-1,sizeof(head)); 82 S = tot = 0; 83 T = n+m+1; 84 LL ans = 0; 85 for(int i = 1; i <= n; ++i){ 86 scanf("%d",&ks); 87 while(ks--){ 88 scanf("%d",&u); 89 add(i,u+n,INF); 90 } 91 scanf("%d",&w); 92 add(S,i,w); 93 ans += w; 94 } 95 for(int i = 1; i <= m; ++i){ 96 scanf("%d",&w); 97 add(i+n,T,w); 98 } 99 printf("Case %d: %I64d\n",csn++,ans - dinic()); 100 } 101 return 0; 102 }