Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)
题意:
给定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(并查集&分组背包)的更多相关文章
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
<题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses
[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 个人是一个 ...
- 【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 ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
随机推荐
- Python圈中的符号计算库-Sympy(转载)
<本文来自公众号“大邓带你玩python”,转载> import math math.sqrt(8) 2.8284271247461903 我们看看Python中结果 math.sqrt( ...
- Django中的cookie和session实现
cookie from django.shortcuts import render, HttpResponse, redirect # 此装饰器的作用就是讲所有没有cookie验证的页面都需要验证后 ...
- flask框架基础入门一
首先:flask是一个基于Werkzeug,Jinja2的一个python的微服务框架. 安装flask框架: pip install flask 一个最小的最简单的flask应用: from fla ...
- python爬取网页图片(二)
从一个网页爬取图片已经解决,现在想要把这个用户发的图片全部爬取. 首先:先找到这个用户的发帖页面: http://www.acfun.cn/u/1094623.aspx#page=1 然后从这个页面中 ...
- QString:常用成员函数总结
QString是Qt中使用频率最高的几种数据类型之一,主要在于其提供了大量功能强大的成员函数,这里重点介绍一些常用的成员函数: 一.字符串处理相关 1.1 split() (拆分字符串) split( ...
- hdu 4565 So Easy! (共轭构造+矩阵快速幂)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...
- KMP HDOJ 4300 Clairewd's message
题目传送门 题意:完全不懂,最后还是看题解才理解了.第一行字符串是密文变成明文的规则,比如第二个样例:“qwertyuiopasdfghjklzxcvbnm”,‘q'对应的明文为’a','w'对应'b ...
- Android SQLite(2)如何判断表是否已经存在
在sql语句中用 if not exists void create_table(){ SQLiteDatabase dbWireter = dbhelper.getWritableDatabase( ...
- Android偏好设置(5)偏好设置界面显示多个分组,每个分组也有一个界面
1.Using Preference Headers In rare cases, you might want to design your settings such that the first ...
- ambari-server启动报错500 status code received on GET method for API:/api/v1/stacks/HDP/versions/2.4/recommendations Error message : Server Error解决办法(图文详解)
问题详情 来源是,我在Ambari集群里,安装Hue. 给Ambari集群里安装可视化分析利器工具Hue步骤(图文详解 所遇到的这个问题. 然后,去ambari-server的log日志,查看,如下 ...