codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值。
分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =。
代码如下:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
const int N = + ; int w[N],b[N];
int n,m,W;
int root[N];
vector<int> v[N];
int allw[N],allb[N];
int dp[N];
int find(int x) {return x == root[x] ? x : root[x] = find(root[x]);} int main()
{
scanf("%d%d%d",&n,&m,&W);
for(int i=;i<=n;i++) root[i] = i;
for(int i=;i<=n;i++) scanf("%d",w+i);
for(int i=;i<=n;i++) scanf("%d",b+i);
for(int i=;i<=m;i++)
{
int x,y;scanf("%d%d",&x,&y);
int rx = find(x), ry = find(y);
if(rx != ry) root[rx] = ry;
}
for(int i=;i<=n;i++)
{
int t = find(i);
v[t].push_back(i);
allw[t] += w[i];
allb[t] += b[i];
}
for(int i=;i<=n;i++)
{
if(v[i].size() == ) continue;
for(int j=W;j>=;j--)
{
for(int k=;k<v[i].size();k++)
{
if(j-w[v[i][k]] >= ) dp[j] = max(dp[j], dp[j-w[v[i][k]]] + b[v[i][k]]);
}
if(j-allw[i] >= ) dp[j] = max(dp[j], dp[j-allw[i]] + allb[i]);
}
}
printf("%d\n",dp[W]);
return ;
}
codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)的更多相关文章
- 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 ...
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses
[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
<题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...
- D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题
http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 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 个人是一个 ...
随机推荐
- React组件的分类
* 1.statelessComponent 不包含任何state的组件 例如:AntDesign的 :Button,Input组件 * 2.viewComponent 包含少量ui state的组件 ...
- 关于Spring事务回滚的问题
在spring的配置文件中,如果数据源的defaultAutoCommit设置为True了,那么方法中如果自己捕获了异常,事务是不会回滚的,如果没有自己捕获异常则事务会回滚,如下例比如配置文件里有这么 ...
- jdbc数据源配置
initCtx.lookup("java:comp/env") 在 容器的conf/ xxxconf.xml http://yuxiatongzhi.iteye.com/blog/ ...
- (Skill)238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- 大规模IP地址黑名单高性能查询实现
嗯……前阵子接了个活儿,需要做一个基于IP地址黑名单的分流网关.刚接到的时候心想iptables不就行了么,没想到一看客户给的IP黑名单规模……我擦……上亿个…… 黑名单到了这个规模,就不得不考虑下优 ...
- html基础 链接
使用图像来作链接: (链接标签中嵌套图片标签) <html> <body> <p> 您也可以使用图像来作链接: <a href="/example/ ...
- Hive On Spark概述
Hive现有支持的执行引擎有mr和tez,默认的执行引擎是mr,Hive On Spark的目的是添加一个spark的执行引擎,让hive能跑在spark之上: 在执行hive ql脚本之前指定执行引 ...
- PD脚本导出到数据库后没有注释问题
昨天PD里建了几张表,建表的时候我在NAME栏位写了中文说明,但是脚本在数据库里生成表以后,发现中文说明没有了,需要自己在“注释”栏位添加才行,如下图: 我想要达到的效果如下图: 解决方法: 1. ...
- centos 解压jdk安装包方式安装jdk 出现 java/lang/NoClassDefFoundError: java/lang/Object 错误
安装完JDK ,设定环境变量后出现这个错误: [root@localhost lib]# javacError occurred during initialization of VMjava/lan ...
- form中的GET与POST
form标签是强大的:如果没有form标签,Internet将变成一个枯燥文档的只读存储库.Web Forms没有完全利用form标签的强大功能(也可以说是Web Forms为实现自己的目标才管理和 ...