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. C# 属性和字段的区别

    属性和字段的区别 在C#中,我们可以非常自由的.毫无限制的访问公有字段, 但在一些场合中,我们可能希望限制只能给字段赋于某个范围的值.或是要求字段只能读或只能写, 或是在改变字段时能改变对象的其他一些 ...

  2. nopi excel 导入

    #region 从Excel导入 /// <summary> /// 读取excel ,默认第一行为标头 /// </summary> /// <param name=& ...

  3. linux查看一条命令的执行结果是1还是0

    echo $? 0为成功 其他为失败

  4. Android SDK Manager中不显示未下载的api解决方案

    Android SDK 在线更新镜像服务器资源用户评价:  / 14 Android SDK 在线更新镜像服务器资源:大连东软信息学院镜像服务器地址:http://mirrors.neusoft.ed ...

  5. 0x00到0xFF二进制数值中1的的个数

    似乎有点无聊耶~~~~~(>_<)~~~~ #include <stdio.h> , , , , , , , , , , , , , , , }; ] = { , , , , ...

  6. shell命令

    1:操作系统:人--使用-->shell(或应用程序)--呼叫-->kernel(核心)--->硬件2:查看shell的种类:# cat /etc/shells3:而这个登入系统的 ...

  7. 图片在ie8浏览器打不开,其他浏览器都可以打开的问题

    问题描述: 1.图片在IE8浏览器打不开,但是IE8以上及其他浏览器均可以打开: 2.同一网站,其他图片可以在IE8浏览器打开 解决办法: 1.图片的颜色模式是CMYK模式,应改为RGB模式 2.修改 ...

  8. 【python】PIL 批量绘制图片矩形框工具

    工具采用PIL:Python Imaging Library,图像处理标准库.PIL功能非常强大,但API却非常简单易用. 安装PIL 在Debian/Ubuntu Linux下直接通过apt安装 $ ...

  9. 前端学习 第三弹: JavaScript语言的特性与发展

    前端学习 第三弹: JavaScript语言的特性与发展 javascript的缺点 1.没有命名空间,没有多文件的规范,同名函数相互覆盖 导致js的模块化很差 2.标准库很小 3.null和unde ...

  10. 在MyEclipse和Eclipse中添加Hibernate开发工具

    一.插件准备 MyEclipse需要的插件:HibernateTools-3.2.4.zip Eclipse需要的插件:jbosstools-4.2.3.Final_2015-03-26_22-41- ...