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 个人是一个 ...
随机推荐
- NLP情感分析监督学习样本打标
1). 情感打标 a). 全句 单句 标签 好吃是好吃 pos 真材实料 pos 不过感觉一人份的量就有点小贵 neg 点的是肥牛米线 neu b). 全文本 文本 标签 分量足,味道不错,味道也不错 ...
- Python爬虫学习1
#coding=utf-8 from urllib2 import urlopen from bs4 import BeautifulSoup import urllib2 url="htt ...
- windows下CMake使用图文手册 Part 3
例子3: 构建动态库(.dll) 静态库(.lib) 采用和例子2一样的文件,但删除了main.cpp E:. │ CMakeLists.txt │ ...
- 使用delphi+intraweb进行微信开发3—微信消息处理
示例代码已经放出!请移步使用delphi+intraweb进行微信开发1~4代码示例进行下载,虽为示例代码但是是从我项目中移出来的,封装很完备适于自行扩展和修改. 在第二讲使用delphi+intra ...
- web页面设计基本原则
1.对齐. 不对齐看起来糟糕极了.简直一片混乱,会给人一种很不专业的印象.对齐让页面更漂亮一些,更重要的是便于更好的传达信息. 2.去掉糟糕的边框.边框会产生一种分离感. 3.亲密性. 元素相互靠近看 ...
- Django 发布时间格式化
Django在数据库中读取的时间是这种格式: {{title.pub_date} 显示:Nov. 17, 2016, 6:31 p.m. 显然,这不符合我们的习惯,所以需要格式化: {{title.p ...
- 根据word模板(contract_templet.tld)生成并下载word合同及根据wordHTML模板(contract_templetHTML.tld)预览合同内容
1.action String templete=ConstantsAppParams.CONTRACT_TEMPLET_DOC;//contract_templet.tldString temple ...
- PHP性能优化大全(转)
PHP优化对于PHP的优化主要是对php.ini中的相关主要参数进行合理调整和设置,以下我们就来看看php.ini中的一些对性能影响较大的参数应该如何设置. # vi /etc/php.ini (1) ...
- C#读取Excel遇到无法读取的解决方法
C#读取Excel遇到无法读取的解决方法:1.在导入数据连接字符串中,将IMEX=1加入,“Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\ ...
- Javascript猜数字游戏
<!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...