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 个人是一个 ...
随机推荐
- JavaScript:彻底理解同步、异步和事件循环(Event Loop) (转)
原文出处:https://segmentfault.com/a/1190000004322358 一. 单线程 我们常说"JavaScript是单线程的". 所谓单线程,是指在JS ...
- oracle补丁升级
PSU的全称是Patch Set Update,Oracle对于其产品每个季度发行一次的补丁包,包含了bug的修复.Oracle选取被用户下载数量多,且被验证过具有较低风险的补丁放入到每个季度的PSU ...
- java写入文件之txt文本
FileWriter fw=new FileWriter("D:\\test.txt") fw.write("文本值") fw.flush() fw.close ...
- Hadoop内功修炼
IT十八掌<大数据内功修炼系列课程>强势推出!由实战派名师徐培成亲自操刀,学完做不了大数据我们负全责!2015.12.21前,优惠价:4999(名额已不多!)2015.12.31前,优惠价 ...
- C++设计模式-Mediator中介者模式
Mediator中介者模式作用:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. UML如下: Colleage抽象同事类 ...
- Modelsim-altera 仿真 顶层原理图的解决办法
解决办法:首先需要将.bdf原理图文件转换为Verilog HDL等第三方EDA工具所支持的标准描述文件.在Quartus下,保持*.bdf为活动窗口状态,运行[File]/[Create/Updat ...
- ubuntu下使用visual studio code来编译和调试C++
最近想在linux上编译c++代码,自己却一直习惯window上的IDE.以前公司要我写Linux代码的时候,我一般都是用eclipse + CDT,而eclipse这东西吧,我个人感觉因为加载组件太 ...
- RabbitMQ消息机制单人分发
static void Main(string[] args) { // More.SendMessage(); var factory = new ConnectionFactory(); fact ...
- Linux:history命令记录操作时间、操作用户、操作IP
[步骤] 1./etc/profile文件中加入以下内容 2.执行:source /etc/profile [效果]
- AC 设置DMZ口上网