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 invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y.
Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.
Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.
Input:
The first line contains integers n, m and w (1 ≤ n ≤ 1000, , 1 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses.
The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses.
The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.
Output:
Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.
Sample Input:
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
Sample Output:
7
这是一个分组背包的问题,仔细想想其实也不难 但一定要用二维dp数组才能写出来!
【题目链接】D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
【题目类型】分组背包+dsu
&题意:
你有\(n\)个\(Hos\)要邀请,每个\(Hos\)都有2个属性,\(w_i\)和\(b_i\)并且这些\(Hos\)会组成一些朋友组,对于每个朋友组,你只能全部选择,或者只选一个,问不超过\(W\)时,最大的\(b\)能是多少?
&题解:
这是分组背包,这题的思路就是优先选择全部的那个,之后去判断,是否是一个朋友组有多个人?
如果是,那么继续细分dp,这时的dp就代表只选一个的情况.
如果不是,那就和01背包一样了
【时间复杂度】\(O(n^2)\)
&代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
#define PI(A) cout<<(A)<<endl;
const int maxn = (int)1e3 + 9;
#define fastio ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int w[maxn],b[maxn],ww[maxn],bb[maxn],n,m,W;
int dp[maxn][maxn];
bool vis[maxn];
int dsu[maxn];
int FIND(int x) {return x==dsu[x]?x:dsu[x]=FIND(dsu[x]);}
int UNION(int x,int y) {dsu[FIND(x)]=FIND(y);}
int main() {
while(cin>>n>>m>>W) {
rep(i,1,n) cin>>w[i];
rep(i,1,n) cin>>b[i];
rep(i,1,n) dsu[i]=i;
while(m--) {
int t1,t2;
cin>>t1>>t2;
UNION(t1,t2);
}
rep(i,1,n) {
int pp=FIND(i);
ww[pp]+=w[i];
bb[pp]+=b[i];
}
int cc=1;
cle(vis,0);
rep(i,1,n) {
int pp=FIND(i);
if (vis[pp]) continue;
vis[pp]=true;
rep(j,1,W) {
dp[cc][j]=dp[cc-1][j];
if (ww[pp]<=j)
dp[cc][j]=max(dp[cc][j],dp[cc-1][j-ww[pp]]+bb[pp]);
}
rep(t,1,n) if(FIND(t)==pp) {
rep(j,1,W) {
if (w[t]<=j)
dp[cc][j]=max(dp[cc][j],dp[cc-1][j-w[t]]+b[t]);
}
}
cc++;
}
int ans=0;
for(int i=0; i<=W; i++) ans=max(ans,dp[cc-1][i]);
PI(ans)
}
return 0;
}
Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)的更多相关文章
- 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 而且如果邀请 ...
- D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题
http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...
- 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 Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan
C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或
题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution
B. Arpa’s obvious problem and Mehrdad’s terrible solution time limit per test 1 second memory limit ...
随机推荐
- C# 处理应用程序减少内存占用
SetProcessWorkingSetSize减少内存占用 系统启动起来以后,内存占用越来越大,使用析构函数.GC.Collect什么的也不见效果,后来查了好久,找到了个办法,就是使用 SetPro ...
- Win10(win7) 安装vs2015(2012)出现ASP.NET 4.0/4.5 尚未在 Web 服务器上注册 下载这个补丁安装就可以了
url:https://www.microsoft.com/zh-cn/download/details.aspx?id=44907&a03ffa40-ca8b-4f73-0358-c191d ...
- Go http共享
package main import( "net/http" "fmt" ) func main(){ h := http.FileServer(http.D ...
- Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- caffe 安装资料整理
最近在安装caffe,因为过程繁琐,而且不同的作者给出了不同的安装教程,鱼龙混杂,所以做了个简单的整理. 基本安装方法在下面博客上面都有详细介绍,不过不同版本的硬件适配不同版本的软件,所以安装的时候一 ...
- [翻译]Primer on Cognitive Computing(认知计算入门)
Source Kelly J., Primer on Cognitive Computing 20150216. 侵删,联系方式:zhoujiagen\@gmail.com. 按A candidate ...
- java虚拟机之回收方法区
在java虚拟机中并没有规范规定需要对方法区即是新生代进行垃圾回收, 主要是这些区域的回收性价比极低, 一般在新生代中一般垃圾回收中可以达到70%到95%. 其中永久代中的垃圾回收主要回收的是两个 ...
- Windows下Nginx的安装与配置
Nginx ("engine x") 是一款高性能的,轻量级的HTTP Web 服务器 和 反向代理服务器及电子邮件 IMAP/POP3/SMTP 代理服务器. Nginx 是由俄 ...
- webpack使用tree shaking的问题。及关于UglifyJs不支持ES6的解决方案。
webpack: plugins:[ new webpack.optimize.UglifyJsPlugin({ compress:{warning:true} }) ] 是的,一些dead code ...
- socket reuse
int k = 1; if( SUCCESS != m_socketServer.setSockOptSocket( SO_REUSEADDR, (char*)&k, sizeo ...