FZU 1980 AbOr's story
阅读原文时间:2023年07月13日阅读:2

Time Limit: 1000ms

Memory Limit: 32768KB

This problem will be judged on FZU. Original ID: 1980
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

In the first line there is an integer T (T≤10), indicating the number of cases. Each case begins with a line containing two integers N and M (1≤N, M≤10,000), the number of the girls in the palace and the number of the kinds of gemstones AbOr can get. The following N lines, each line contains an integer Li (0≤Li≤M, 1≤i≤N) first, the number of the kinds of gemstones the i-th girl wants. We guarantee that the sum of Li (1≤i≤N) is no more than 100,000. Then following Li integers Gj (1≤Gj≤M, 1≤j≤Li), the index of the gemstone the i-th girl wants. We guarantee that no replication index of the gemstone will appear in these Li integers. Then following an integer P (1≤P≤1,000,000,000), the money AbOr could make after playing with the i-th girl. The next line contains M integers Sk (1≤Sk≤1,000,000,000, 1≤k≤M), the price of the k-th gemstone is Sk. The index of the gemstone is labeled from 1 to M.

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

The 35th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest

解题:最大权闭合图

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define pii pair
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
LL flow;
arc(int x = ,LL y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[];
int head[maxn],d[maxn],cur[maxn];
int tot,S,T,n,m;
void add(int u,int v,LL flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
memset(d,-,sizeof(d));
d[S] = ;
queueq;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
LL dfs(int u,LL low){
if(u == T) return low;
LL tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == d[u] + &&(a=dfs(e[i].to,min(e[i].flow,low)))){
e[i].flow -= a;
e[i^].flow += a;
tmp += a;
low -= a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
LL dinic(){
LL ans = ;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans += dfs(S,INF);
}
return ans;
}
int main() {
int csn = ,cs,ks,u,v,w;
scanf("%d",&cs);
while(cs--){
scanf("%d %d",&n,&m);
memset(head,-,sizeof(head));
S = tot = ;
T = n+m+;
LL ans = ;
for(int i = ; i <= n; ++i){
scanf("%d",&ks);
while(ks--){
scanf("%d",&u);
add(i,u+n,INF);
}
scanf("%d",&w);
add(S,i,w);
ans += w;
}
for(int i = ; i <= m; ++i){
scanf("%d",&w);
add(i+n,T,w);
}
printf("Case %d: %I64d\n",csn++,ans - dinic());
}
return ;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章