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 个人是一个集合的,给出一个最大花费 w,如果一个集合的人不能同时选的话,那么只能选集合中的其中一个或者不选,问按照这样的规则选得的花费不超过 w 的最大价值是多少。
思路:一开始搞个带权并查集出来,后来还是蒙了。之后看原来是01背包,然而已经N久没见过背包了。。。先用并查集弄出集合,然后对每个集合中的元素进行01背包(选集合中的一个或者不选的情况),在集合弄完之后还要对整体进行01背包(选集合中的全部),这里用了下滚动数组。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
int dp[][];
int fa[];
int b[], w[];
vector<int> vec[]; int Find(int x) {
if(x == fa[x]) return x;
return fa[x] = Find(fa[x]);
} int main()
{
int n, m, x;
scanf("%d%d%d", &n, &m, &x);
for(int i = ; i <= n; i++) scanf("%d", w + i);
for(int i = ; i <= n; i++) scanf("%d", b + i);
for(int i = ; i <= n; i++) fa[i] = i;
for(int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u = Find(u), v = Find(v);
if(u != v) fa[u] = v;
}
for(int i = ; i <= n; i++)
vec[Find(i)].push_back(i);
int now = ;
for(int i = ; i <= n; i++) {
now = !now;
int ww = , bb = ;
for(int j = ; j <= x; j++) dp[now][j] = dp[!now][j];
for(int j = ; j < vec[i].size(); j++) {
int id = vec[i][j];
int www = w[id], bbb = b[id];
ww += www, bb += bbb;
for(int j = www; j <= x; j++) { // 在当前集合选一个最优
dp[now][j] = max(dp[now][j], dp[!now][j-www] + bbb);
}
}
for(int j = ww; j <= x; j++) { // 如果全部能丢进去
dp[now][j] = max(dp[now][j], dp[!now][j-ww] + bb);
}
}
int ans = ;
for(int i = ; i <= x; i++) ans = max(ans, dp[now][i]);
printf("%d\n", ans);
return ;
}
Codeforces 741B:Arpa's weak amphitheater and Mehrdad's valuable Hoses(01背包+并查集)的更多相关文章
- 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个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...
- 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 ...
- D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题
http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
- Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)
题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...
- 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 ...
随机推荐
- Java基础——常用类(Date、File)以及包装类
本文要点: 基本数据类型的包装类 字符串相关类: 不可变字符序列:String 可变字符序列:StringBuffer.StringBuilder 时间处理相关类: Date DateFormat.S ...
- VMware设置共享文件夹
第一步:安装vmware-tools
- Vue.2.0.5-条件渲染
v-if 在字符串模板中,如 Handlebars ,我们得像这样写一个条件块: <!-- Handlebars 模板 --> {{#if ok}} <h1>Yes</h ...
- 装饰器、生成器,迭代器、Json & pickle 数据序列化
1. 列表生成器:代码例子 a=[i*2 for i in range(10)] print(a) 运行效果如下: D:\python35\python.exe D:/python培训/s14/day ...
- [转]【android studio】解决layout预览出现Rendering Problems Exception Unable to find the layout for Action Bar.
在android studio中打开layout文件,发现不能预览布局,提示以下错误: Rendering Problems Exception raised during rendering: Un ...
- 如何用VS调试不属于解决方案的EXE和DLL程序
如果你手里有一个现成的EXE, 以及EXE相关联PDB文件, 还有相关联的CPP文件和H文件. 你如何用VS调试? (当然你可以选择WinDbg.不过这里就讨论VS) 你或许想问我干嘛不从一开始就用V ...
- spring 定时任务标注
使用spring框架,需要定时任务只需要在方法上加@Component 就可以了 package hello; import java.text.SimpleDateFormat; import ja ...
- uiwebview加载中文URL
uiwebview加载中文URL _paperGamePath = [_paperGamePath stringByAddingPercentEscapesUsingEncoding:NSUTF8St ...
- 原来现在很多人都用SignalR来实现Chat Room
今天从一个业余开发的群里,看到有人要求这样一个项目需求: 1,)学员可以通过在线课堂找到自己喜欢的老师和课程. 2,)每个人可以建立自己课堂,每个课堂扣分多个子房间,交流群.设置管理员:有录音功能,可 ...
- Codeforce Round #211 Div2
真的是b到不行啊! 尼玛C题一个这么简单的题目没出 aabbccddee 正确的是aabccdee 我的是 aabcdee 硬是TM的不够用,想半天还以为自己的是对的... A:题... B:题. ...