题意:

给定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. B - Crossword solving

    Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he ...

  2. _bzoj1012 [JSOI2008]最大数maxnumber【Fenwick Tree】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 裸的树状数组. #include <cstdio> #include &l ...

  3. magento getMessage 不显示或者显示html标签解决方案

    在模板页面不显示getMessage的解决方案是,在对应的控制器里加上如下代码: $this->_initLayoutMessages('customer/session'); 如果加入后出现如 ...

  4. Environment中有大量访问目录的函数

    public class Environment { /** * Return root of the "system" partition holding the core An ...

  5. js中toFixed重写

    在测试原生的toFixed发现,它在个浏览器上表现不一致,并且有些值在保留小数时得到的结果并不是想要,如在chrome下测试: 所以针对toFixed方法不准的问题,我们进行方法改造: 主要思路是:对 ...

  6. No rule to make target ...

    在编译一个Android上的jni的时候出现了如下的问题 make[3]: *** No rule to make target `/home/zhang/android1/src/androidpk ...

  7. Collection接口框架图

    Java集合大致可分为Set.List和Map三种体系,其中Set代表无序.不可重复的集合:List代表有序.重复的集合:而Map则代表具有映射关系的集合.Java 5之后,增加了Queue体系集合, ...

  8. mysql 判断null 和 空字符串

    1.在mysql中null 不能使用任何运算符与其他字段或者变量(函数.存储过程)进行运算.若使用运算数据就可能会有问题. 2.对null 的判断: 创建一个user表:id 主健 name 可以为空 ...

  9. object -c OOP , 源码组织 ,Foundation 框架 详解1

     object -c  OOP ,  源码组织  ,Foundation 框架 详解1 1.1 So what is OOP? OOP is a way of constructing softwar ...

  10. 强大的云存储与应用管理工具DzzOffice1.0 Beta(大桌子办公)发布下载

    之前在9月份我们发布了一份内测版,得到了1000多位朋友参与下载测试.经过2个月,结合测试后朋友们反馈的问题,和开发建议.终于完成了这次Beta版的开发.感谢这两个月中参与测试,和帮助我们完善程序的朋 ...