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 个人是一个 ...
随机推荐
- C++调用JAVA方法详解
C++调用JAVA方法详解 博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...
- git和github使用方式
git 和github github是远程管理代码的服务器的名称 git代码管理系统 (git既然是一个系统,所以说git也有一些命令) git操作过程 首先在本地建立一个仓库,用来把代码提交到git ...
- 转mysql 多表 update sql语句总结
mysql 多表 update 有几种不同的写法. 假定我们有两张表,一张表为Product表存放产品信息,其中有产品价格列Price:另外一张表是ProductPrice表,我们要将ProductP ...
- [AS3.0] HTMLLoader与js交互
HtmlLoader 的属性window是加载到 HTML 控件中的内容的全局 JavaScript 对象,通过这个对象能够方便的和页面js通讯. AS代码: import flash.html.HT ...
- twitter点赞动画详解
今天在微博上看到@过气网红一丝 的一篇微博,codepen上贴出了twitter点赞那个动画效果的源码,地址 http://codepen.io/yisi/pen/LpXVJb .我看了下效果很好看, ...
- [转]Amazon DynamoDB – a Fast and Scalable NoSQL Database Service Designed for Internet Scale Applications
This article is from blog of Amazon CTO Werner Vogels. -------------------- Today is a very exciting ...
- spring+ibatis整合
一.pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...
- SQL语句学习手册实例版
SQL语句学习手册实例版 表操作 例1 对于表的教学管理数据库中的表 STUDENTS ,可以定义如下: CREATE TABLE STUDENTS (SNO NUMERIC (6, ...
- 关于android 5.0对开发带来的影响
由于google推出了android5.0的系统,在app的安装方式,权限的管理方面与之前的系统有较大的区别.自己做的app也有体会.现在记录一些要注意的东西,防止忘记: 1.5.0改变了对自定义权限 ...
- Xcode7打包,iOS9真机闪退,如何解决?
问:有些项目用xcode7打开运行,打包安装到iOS9设备上程序会闪退. 如果用xcode7以下编译,然后打包到iOS9的设备上就是正常的.这是为什么,关键是,怎么解决? 答:iOS9发布之后,有些a ...