#383 Div1 Problem B Arpa's weak amphitheater.... (分组背包 && 并查集)
题意 : 有n个人,每个人都有颜值bi与体重wi。剧场的容量为W。有m条关系,xi与yi表示xi和yi是好朋友,在一个小组。 每个小组要么全部参加舞会,要么参加人数不能超过1人。 问保证总重量不超过W,剧场中的颜值最大能到多少?
分析 : 很显然的分组背包题目, 不过有所不同, 先来回顾一下普通的分组背包的描述 给出一个背包,背包有容量C,再给出N组物品,每组物品有ki种,每种物品有对应的体积Vi,价值Pi,每组物品至多选一种,且最多取一件。求用背包装物品,能获得的最大总价值是多少。可以发现和上面的不同之处就是多了一个可以全取的操作, 其实只要再在每一组里面加上所有的物品和就行了, 便成了一道普通的模板题了!在分组的时候使用简单的并查集即可!
瞎搞 : 在实现的时候很慢, 还老是犯小错误, 归其原因就是分组背包接触太少, 即使是模板题也写的有点不流利!
#include<bits/stdc++.h>
using namespace std;
struct st
{
int w, v, id;
};
st arr[];
][], w[][], dp[], p[];///p数组表示各个组里面的物品个数
], sumw[];
];
map<int, int> M;
int findset(int x)
{
int root = x;
while(c[root] != root) root = c[root];
int j;
while(c[x] != root){
j = c[x];
c[x] = root;
x = j;
}
return root;
}
inline int join(int a, int b)
{
int f = findset(a), ff = findset(b);
if(f != ff){
c[f] = ff;
}
}
#define IN 0
#define OUT 0
int main(void)
{
#if IN
freopen("in.txt", "r", stdin);
#endif
#if OUT
freopen("out.txt", "w", stdout);
#endif
int n, m, C;
scanf("%d%d%d", &n, &m, &C);
M.clear();
; i<=n; i++){
memset(v[i], , sizeof(v[i]));
memset(w[i], , sizeof(w[i]));
memset(p, , sizeof(p));
memset(dp, , sizeof(dp));
memset(sumw, , sizeof(sumw));
memset(sumv, , sizeof(sumv));
arr[i].id = i;//忽略这个id, 没有用, 懒得改了.....
c[i] = i;
}
; i<=n; i++) scanf("%d", &arr[i].w);
; i<=n; i++) scanf("%d", &arr[i].v);
; i<m; i++){
int a, b;
scanf("%d%d", &a, &b);
join(a, b);
}
;///表示组数
];
memset(vis, true, sizeof(vis));
; i<=n; i++){
int temp = findset(i);
if(vis[temp]){///如果还没有出现过这个在并查集里面的“大佬”
M[temp] = top;//记录一下老大所在的组的下标元素
vis[temp] = false;
v[top][p[top]] = arr[i].v;
w[top][p[top]] = arr[i].w;
sumv[top] += arr[i].v;
sumw[top] += arr[i].w;
p[top]++, top++;
}else{
int Top = M[temp];
v[Top][p[Top]] = arr[i].v;
w[Top][p[Top]] = arr[i].w;
sumv[Top] += arr[i].v;
sumw[Top] += arr[i].w;
p[Top]++;
}
}
; i<top; i++){
) continue;//这里需要注意, 只有一个元素的时候, 不应该再加所有元素的和, 会重复的!
v[i][p[i]] = sumv[i];
w[i][p[i]] = sumw[i];
p[i]++;
}
///Debug begin
// printf("top = %d\n", top);
// for(int i=0; i<top; i++){
// printf("In group %d\n", i);
// printf("v");puts("");
// for(int j=0; j<p[i]; j++){
// printf("ord=%d, v=%d\n", j, v[i][j]);
// }
// printf("w");puts("");
// for(int j=0; j<p[i]; j++){
// printf("ord=%d, w=%d\n", j, w[i][j]);
// }
// puts("");
// }
// puts("");
///Debug end
; i<top; i++){
; j--){
; k<p[i]; k++){
if(w[i][k] <= j)
dp[j] = max(dp[j], dp[j-w[i][k]]+v[i][k]);
}
}
///Debug begin
// for(int ii=1; ii<=C; ii++){
// printf("%d ", dp[ii]);
// }
// puts("");
///Debug end
}
printf("%d\n", dp[C]);
;
}
#383 Div1 Problem B Arpa's weak amphitheater.... (分组背包 && 并查集)的更多相关文章
- 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 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 ...
- Codeforces Round #383 (Div. 2)D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(dp背包+并查集)
题目链接 :http://codeforces.com/contest/742/problem/D 题意:给你n个女人的信息重量w和美丽度b,再给你m个关系,要求邀请的女人总重量不超过w 而且如果邀请 ...
- 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 ...
- 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 个人是一个 ...
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses
[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...
- D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题
http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...
- 【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 ...
随机推荐
- mysql——多表——外连接查询——左连接、右连接、复合条件查询
), d_id ), name ), age ), sex ), homeadd ) ); ,,,'nan','beijing'); ,,,'nv','hunan'); ,,,'nan','jiang ...
- python 并发编程 多线程 GIL全局解释器锁基本概念
首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念. 就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可执行代码. ...
- Mac OS X 11中的/usr/bin 的“Operation not permitted”
更新了 Mac OS X 11后发现,MacVim 不再能够通过Terminal用命令打开了. mvim hello.txt 于是尝试将 mvim 重新复制到/usr/bin/中去 sudo cp - ...
- openvswitch安装与使用
wget http://openvswitch.org/releases/openvswitch-2.4.1.tar.gz tar -xvf openvswitch-2.4.1.tar.gz cd o ...
- 微服务理论之二:面向微服务架构与传统架构、SOA对比,以及云化对比
一.Monolith 网上对Microservice进行介绍的文章常常以Monolith作为开头,我也不会例外.原因是,知道了Monolith的不便之后才能更容易地理解Microservice架构模式 ...
- Luogu P1450 [HAOI2008]硬币购物
题目 一个很自然的想法是容斥. 假如只有一种硬币,那么答案就是没有限制的情况下买\(s\)的方案数减去强制用了\(d+1\)枚情况下买\(s\)的方案数即没有限制的情况下买\(s-c(d+1)\)的方 ...
- php中use关键词使用场景
php中use关键词使用场景,主要使用在函数内部使用外包得变量才使用得 1,这种函数使用不到外包变量 $messge="96net.com.cn"; $exam=function ...
- Python类函数调用:missing 1 required positional argument
在Python中,应该先对类进行实例化,然后在应用类.注意,实例化的过程是应该加括号的.
- MySQL数据库入门多实例配置
MySQL数据库入门——多实例配置 前面介绍了相关的基础命令操作,所有的操作都是基于单实例的,mysql多实例在实际生产环境也是非常实用的,因为必须要掌握 1.什么是多实例 多实例就是一台服务器上开启 ...
- 【vue】canvas验证码组件--数字/数字加字母
基于canvas的数字/数字+字符验证码 SIdentify.vue 组件 <!-- 基于canvas的数字/数字+字符验证码 --> <!-- 调用格式 <s-ident ...