Xzz is playing a MMORPG "human life".

In this game, there are N different skills. Some skills may base on another skill.

Learning skill will cost Xzz some money.

And there are M different jobs. If Xzz's skills satisfy the job's requirement, then Xzz can get this job, and get some money.

But some jobs are conflict, some Xzz can't get the job at same time.

There are K pairs of jobs are conflict.

Now Xzz want to know the money he can get at the most,can you help him.

Input

First line of the input file contains an integer T(0 < T ≤ 10) that indicates how many cases of inputs are there.

The description of each case is given below:

The first line of each input case contains number N, M, K. N <= 100, M <= 50, K <= 5.

Then follow N lines. In ith line the first two number ? ,? , means learning skill i const vi , skill i base on another ni skills. The last ni number aij means before learning skill i Xzz need to learn skill aij. 0 ≤ vi ≤ 1000, 0 ≤ ni ≤ N

Then follow M lines. In ith line the first two number wi, mi , means job i earn wi, jobs i base on mi skills. The last mi number bij means get job i need to learn skill bij. 0 ≤ wi ≤ 1000, 0 ≤ mi ≤ M

Then follow K lines. In ith line the first two number ci, di, means job ci conflict with job di.

Output

The description of output for each test case is given below:

The first line of the output for each test case contains number answer, the maximum money Xzz can get.

Sample Input

2
5 2 0
1 0
1 1 1
1 0
1 0
1 1 4
10 2 2 3
8 2 3 5
5 2 1
1 0
1 1 1
1 0
1 0
1 1 4
10 2 2 3
8 2 3 5
1 2

Sample Output

13
7 今天组队训练,这个我负责的图论部分没有写出来 难受啊
今天想拿网络流 流过去结果建图不知道该怎么建图 流也流不对
结束后才知道这就是最大权闭合子图的板子题目
唉 图论的基本套路我都还没有掌握 难受啊 今天然后晚上认真的学习了最大权闭合子图 点击这里学习最大权闭合子图 博主很良心讲的非常好 然后下面这一题就稍微的改动了一下
一开始我还在想k如何处理 ,后来学长告诉我k最大为5 直接枚举所有情况就好了 然后就是基本的建图操作
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
using namespace std;
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define san(n,m) scanf("%d%d",&n,&m)
#define FIN freopen("in.txt","r",stdin)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
const int maxn = ;
typedef long long LL;
const int MX = ;
const int MXE = * MX * MX;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f;
struct MaxFlow {
struct Edge {
int v, nxt;
LL w;
} E[MXE];
int tot, num, s, t;
int head[MX];
void init() {
memset (head, -, sizeof (head) );
tot = ;
}
void add (int u, int v, LL w) {
E[tot] = (Edge) {
v, head[u], w
};
head[u] = tot++;
E[tot] = (Edge) {
u, head[v],
};
head[v] = tot++;
}
int d[MX], vis[MX], gap[MX];
void bfs() {
memset (d, , sizeof (d) );
memset (gap, , sizeof (gap) );
memset (vis, , sizeof (vis) );
queue<int>q;
q.push (t);
vis[t] = ;
while (!q.empty() ) {
int u = q.front();
q.pop();
for (int i = head[u]; ~i; i = E[i].nxt) {
int v = E[i].v;
if (!vis[v]) {
d[v] = d[u] + ;
gap[d[v]]++;
q.push (v);
vis[v] = ;
}
}
}
}
int last[MX];
LL dfs (int u, LL f) {
if (u == t) return f;
LL sap = ;
for (int i = last[u]; ~i; i = E[i].nxt) {
int v = E[i].v;
if (E[i].w > && d[u] == d[v] + ) {
last[u] = i;
LL tmp = dfs (v, min (f - sap, E[i].w) );
E[i].w -= tmp;
E[i ^ ].w += tmp;
sap += tmp;
if (sap == f) return sap;
}
}
if (d[s] >= num) return sap;
if (! (--gap[d[u]]) ) d[s] = num;
++gap[++d[u]];
last[u] = head[u];
return sap;
}
LL solve (int st, int ed, int n) {
LL flow = ;
num = n;
s = st;
t = ed;
bfs();
memcpy (last, head, sizeof (head) );
while (d[s] < num) flow += dfs (s, INFLL);
return flow;
}
} F;
int t, n, m, k, vis[];
struct node {
int val, n;
int num[];
} a[], b[];
struct node1 {
int x, y;
} p[maxn];
int main() {
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= n ; i++) {
scanf("%d%d", &a[i].val, &a[i].n);
for (int j = ; j < a[i].n ; j++) scanf("%d", &a[i].num[j]);
}
for (int i = ; i <= m ; i++) {
scanf("%d%d", &b[i].val, &b[i].n);
for (int j = ; j < b[i].n ; j++) scanf("%d", &b[i].num[j]);
}
for (int i = ; i < k ; i++) scanf("%d%d", &p[i].x, &p[i].y);
int st = , ed = n + m + ;
LL ans = ;
for (int i = ; i < ( << k) ; i++) {
LL sum = ;
F.init();
memset(vis, , sizeof(vis));
for (int j = ; j < k ; j++) {
if (vis[p[j].x] && vis[p[j].y] ) continue;
if ((i >> j) & ) vis[p[j].y] = ;
else vis[p[j].x] = ;
}
for (int j = ; j <= m ; j++) {
if (vis[j]) continue;
sum += 1LL * b[j].val;
F.add(st, j, b[j].val);
for (int p = ; p < b[j].n ; p++) F.add(j, m + b[j].num[p], INF);
}
for (int j = ; j <= n ; j++) {
F.add(m + j, ed, a[j].val);
for (int p = ; p < a[j].n ; p++) F.add(m + j, m + a[j].num[p], INF);
}
LL temp = F.solve(st, ed, n + m + );
ans = max(ans, sum - temp);
}
printf("%lld\n", ans);
}
return ;
}

Human life FZU - 2295 最大权闭合子图(第一次遇到被教育了)的更多相关文章

  1. FZU - 2295 Human life (最大权闭合子图)

    题目链接 FZU - 2295 Human life 题目分析 题意:你在玩一个游戏,在其中你可以通过学习一些技能,但是学习某些技能之前,可能还要学习一些其他的技能,并且学习任何技能都有一定的花费: ...

  2. FZU - 2295 Human life:网络流-最大权闭合子图-二进制优化-第九届福建省大学生程序设计竞赛

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 http://acm.fzu.edu.cn/problem.php?pid=2295 htt ...

  3. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  4. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

  5. [BZOJ 1497][NOI 2006]最大获利(最大权闭合子图)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1497 分析: 这是在有向图中的问题,且边依赖于点,有向图中存在点.边之间的依赖关系可以 ...

  6. HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...

  7. HDU5855 Less Time, More profit(最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build ...

  8. HDU5772 String problem(最大权闭合子图)

    题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...

  9. SCU3109 Space flight(最大权闭合子图)

    嗯,裸的最大权闭合子图. #include<cstdio> #include<cstring> #include<queue> #include<algori ...

随机推荐

  1. (Python爬虫04)了解通用爬虫和聚焦爬虫,还是理论知识.快速入门可以略过的

    如果现在的你返回N年前去重新学习一门技能,你会咋做? 我会这么干: ...哦,原来这个本事学完可以成为恋爱大神啊, 我要掌握精髓需要这么几个要点一二三四..... 具体的学习步骤是这样的一二三.... ...

  2. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...

  3. opencv-学习笔记(4)-模糊

    opencv-学习笔记(4)-模糊 本章要点: 4种模糊方式 2d卷积 Cv2.filter2D(‘图像对象’,‘目标图像这里直接设为-1即可’,kernal,anchor(-1,-1)) 一般后一个 ...

  4. Image控件显示以byte[]字节数组形式存在的图片

    工作中遇到了这样的一个问题.起初觉得很简单,获得了图片的byte[]后,可以将其转换成内存中的图片对象(如System.Drawing.Image),而后赋给页面的Image控件.尝试后才发现这样根本 ...

  5. 1.Hadoop介绍

    1. Hadoop介绍 1.1 什么是Hadoop 开源的,可靠的,分布式的,可伸缩的 提供的功能: 利用服务器集群,根据用户的自定义业务逻辑,对海量数据进行分布式处理 1.2 处理方式 大众角度 数 ...

  6. Bad Cowtractors(最大生成树)

      Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= ...

  7. php5.4以上运行yii框架出现问题的解决方法

    Ubuntu Server 下安装 Mcrypt Php Extension http://blog.archean.me/2013/10/22/install-mcrypt-php-extensio ...

  8. js中斜杠转义

    js中“/”不需要转义. if(myPath.indexOf("/Upload/EmailFile/")!=-1){ alert("有附件!")}

  9. SQLite - Python

    SQLite - Python 安装 SQLite3 可使用 sqlite3 模块与 Python 进行集成.sqlite3 模块是由 Gerhard Haring 编写的.它提供了一个与 PEP 2 ...

  10. New API

    New API Producer >增加发送回调 >重构Partition 统一High Level API与Low Level API >从kafka.consumer和kafka ...