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 ...
随机推荐
- Android --ListView模板
调整了近一上午的模板 ListView表头 <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- 手动内存管理与ARC互相转换问题
- play 之定时器job
play定时任务之job 要创建一个Job,只需要简单地继承play.jobs.Job类就可以. 一些常用的注解: @Every("1h") 每隔1小时执行一次 @ ...
- IIS URL Rewrite redirect from one Domain to another
IIS URL Rewrite enables Web administrators to create powerful rules to implement URLs that are easie ...
- Javascript中Number()、parseIn()和parseFloat()的区别
有3个函数可以把非数值转化成数值:Number().parseInt()和parseFloat().第一个函数,即转型函数Number()可以用于任何数据类型,而另两个函数则专门用于把字符串转换成数值 ...
- SQLSERVER:通过sys.tables实现批量删表、快速统计多表记录和
SQLSERVER:通过sys.tables实现批量删表,或者回滚表 begin try drop table #temp10 end try begin catch end catch select ...
- 5.POST中文乱码解决方案
Spring Web MVC提供了Filter用于解决POST方式造成的中文乱码问题,具体配置如下: <filter> <filter-name>CharacterEncodi ...
- PostgreSQL configuration file postgresql.conf recommanded settings and how it works
1 Set max_connections to three times the number of processor cores on the server. Include vir ...
- G面经prepare: Straight Partition of A Deck of Cards
Define “Straight” as 5 cards with consecutive numbers. Determine if the deck can be fully divided in ...
- java一般要点
1.String是引用类型. 2.char, short, byte在进行运算的时候会自动转换成int类型数据., 3.数据A 异或同一个数两次,得到的还是A 4.java的for循环,可以在前面加一 ...