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)的更多相关文章

  1. 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 ...

  2. 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 而且如果邀请 ...

  3. D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题

    http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Python 基础之在ubuntu系统下安装双版本python

    前言:随着python升级更新,新版本较于老版本功能点也有不同地方,作为一个初学者应该了解旧版本的规则,也要继续学习新版本的知识.为了能更好去学习python,我在ubuntu安装python2和py ...

  2. Guid与id区别

    一.产生Guid的方法 1.SqlServer中使用系统自带的NEWID函数: select NEWID() 2.C#中,使用Guid类型的NewGuid方法: Guid gid;           ...

  3. CSS元素定位6-10课

    <精通CSS.DIV网页样式与布局>视频6-10课总结图: 元素定位 (1)float:left/right; 左浮动:脱离普通文档流向左浮动(即向左对齐):float属性必须应用在块级元 ...

  4. CoInitialize浅析二

    最近工作比较忙,在粗略分析了CoInitialize之后我们一直没有再深入研究,下面言归正传.前面我们初步了解到了CoInitialize其实是通过调用CoInitializeEx来实现功能的,而后者 ...

  5. redis 数据结构一 之t_string

    简介 REDIS有非常丰富的数据结构 以及建立在这数据结构上的操作,在源文件中主要集中在 T_hash.c /T_list.c /T_string.c/T_zset.c 可以说读懂了这4个源文件  大 ...

  6. Oracle expdp按分区导出生成参数文件

    使用dba_tab_partitions视图获得每个分区的参数文件内容,使用chr(10)分行 select 'content=data_only'||chr(10)|| 'directory=dat ...

  7. Android之ListView&ViewPager模拟新闻界面

    模拟新闻 APP 的界面 1)写 ListView 之前先写布局: 这里有两种 Item 的布局: <?xml version="1.0" encoding="ut ...

  8. 课堂笔记-background属性

    一,background-position:(图片定位)   三种写法:   1):按%比,左上角最小(0%,0%),右下角最大(100%,%100):   2):(x,y)左上角最小(0,0),右下 ...

  9. Flask 的扩展

    1. Flask-Script,为Flask程序提供了一个命令行解析器: (venv) $ pip install flask-script 2. Bootstrap(http://getbootst ...

  10. 我是如何社工TDbank获取朋友隐私的

    原创 ziwen@beebeeto 转载请保留本行 个人感觉 国外的安全方面对社工的了解和防范并不是很好 即使他们使用社工的时间比我们要长很多 比如 他们的visa在pos机上使用是不需要密码的 而且 ...