题意:

给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个。

分析:

可以采用并查集将所有的集合统计出来, 然后再将集合中所有的价值总和sumb和所有的花费总和sumw也作为一个元素加入集合。然后就是分组背包的做法,目前暂时参考背包九讲。

#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int f[maxn]; //并查集
int b[maxn], w[maxn];
int n, W, m;
void init()
{
for(int i = ; i <= n; i++){
f[i] = i;
}
}
int Find(int x)
{
if( x == f[x])
return x;
else {
f[x] = Find(f[x]);
return f[x];
}
}
int dp[maxn];
void Merge(int v , int u)
{
int t1,t2;
t1 = Find(v);
t2 = Find(u);
if(t1 != t2){
f[t2] = t1;
}
}
struct s{
int w, b;
s(int _w, int _b) : w(_w), b(_b){};
};
vector<s> group[maxn];
int divgroup()
{
for(int i = ; i <= n;i++)
{
group[Find(i)].push_back(s(w[i],b[i]));
}
for(int i = ; i <= n; i++)
{
if(!group[i].empty())
{
int w_sum = , b_sum = ;
for(int j = ; j < group[i].size(); j++){
w_sum += group[i][j].w;
b_sum += group[i][j].b;
}
group[i].push_back(s(w_sum, b_sum));
}
}
}
int main()
{
#if LOCAL
freopen("1.txt","r",stdin);
#endif // LOCAL
scanf("%d %d %d", &n ,&m, &W);
for(int i = ; i <= n; i++){
scanf("%d", &w[i]);
}
for(int i = ; i<= n; i++){
scanf("%d", &b[i]);
} init();
for(int i = ; i < m; i++){
int u, v;
scanf("%d %d", &u, &v);
Merge(u,v);
}
divgroup(); for(int k = ; k <= n; k++){
if(!group[k].empty()){
for(int v = W; v >= ; v--){
for(int i = ; i < group[k].size(); i++){
if(v-group[k][i].w >= )
dp[v] = max(dp[v], dp[v-group[k][i].w]+ group[k][i].b);
}
}
}
}
printf("%d\n", dp[W]); return ;
}

Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)的更多相关文章

  1. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)

    <题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...

  2. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses

    [题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...

  3. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  4. Arpa's weak amphitheater and Mehrdad's valuable Hoses

    Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit per ...

  5. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...

  6. B. Arpa's weak amphitheater and Mehrdad's valuable Hoses

    B. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  7. Codeforces 741B:Arpa's weak amphitheater and Mehrdad's valuable Hoses(01背包+并查集)

    http://codeforces.com/contest/741/problem/B 题意:有 n 个人,每个人有一个花费 w[i] 和价值 b[i],给出 m 条边,代表第 i 和 j 个人是一个 ...

  8. 【42.86%】【codeforces 742D】Arpa's weak amphitheater and Mehrdad's valuable Hoses

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

随机推荐

  1. iOS NSDictionary <--> NSString(JSON) in Objc

    NSDictionary --> NSString + (NSString*)stringINJSONFormatForObject:(id)obj { NSData *jsonData = [ ...

  2. libcurl在centos6.4 64位操作系统上不支持https的解决方案

    rpm -e –nodeps 软件名 例如: 使用yum install nginx 安装了nginx, 这是如果直接使用yum remove nginx 的话,会把依赖的一些包也删掉, 所以要使用r ...

  3. 移动游戏By HYJ

    暴力求SG函数即可,记忆化贼方便 /*program from Wolfycz*/ #include<cmath> #include<cstdio> #include<c ...

  4. 手把手教你玩微信小程序跳一跳

    最近微信小程序火的半边天都红了,虽然不会写,但是至少也可以仿照网上大神开发外挂吧!下面手把手教妹纸们(汉纸们请自觉的眼观耳听)怎么愉快的在微信小游戏中获得高分. 废话不多说,咱们这就发车了!呸!咱们这 ...

  5. 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  6. 光盘安装ubuntu出现busybox-initramfs不能继续安装的终极解决方法

    我用的是Intel G45的主板,可能是板子太新的原因,kernel支持有问题 从光盘(官方寄来的光盘)安装ubuntu,出现提示: BusyBox V1.1.3 (Debian 1:1.1.3-5u ...

  7. B. Hongcow Solves A Puzzle

    http://codeforces.com/contest/745/problem/B 题目要求的是,给定一个图形,要求里面判断是否有矩形,且仅有一个 就是 XXX.... XXX...X 是不行的, ...

  8. Oracle本地动态 SQL

    本地动态 SQL 首先我们应该了解什么是动态 SQL,在 Oracle数据库开发 PL/SQL块中我们使用的 SQL 分为:静态 SQL语句和动态 SQL语句.所谓静态 SQL指在 PL/SQL块中使 ...

  9. Linux之测试服务器和端口连通

    目录 wget工具 telnet工具 ssh工具 wget工具: 该工具是网络自动下载工具,如果linux或centos中不存在,需要先安装,支持http.https.ftp协议,wget名称的由来是 ...

  10. [BZOJ2456]mode 其它

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2456 这道题有着神奇的内存限制1MB也就是说我们是没办法把读入的数字存下来的. 由于答案求 ...