【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 output 
Just to remind, girls in Arpa’s land are really nice.
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.
Examples 
input 
3 1 5 
3 2 5 
2 4 2 
1 2 
output 
6 
input 
4 2 11 
2 4 6 6 
6 4 2 1 
1 2 
2 3 
output 
7 
Note 
In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.
In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can’t invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
【题目链接】:http://codeforces.com/contest/742/problem/D
【题解】 
 
把原本的n个人看成是n个物品. 
特殊的,在同一个朋友组里面的所有人她们的漂亮值和体重和也作为一个新的物品. 
重量作为代价、漂亮值作为收益.求最大收益. 
对这n+x个物品做01背包就可以了. 
 
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXW = 1e3+100;
const int MAXN = 1e3+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int f[MAXW],fa[MAXN];
int n,m,W;
int c[MAXN],w[MAXN];
int bag[MAXN];
int ff(int x)
{
    if (fa[x]==x) return x;
    else
        fa[x] = ff(fa[x]);
    return fa[x];
}
int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    rei(n);rei(m);rei(W);
    rep1(i,1,n) rei(w[i]),fa[i] = i;
    rep1(i,1,n) rei(c[i]);
    rep1(i,1,m)
    {
        int x,y;
        rei(x);rei(y);
        int r1 = ff(x),r2 = ff(y);
        if (r1!=r2)
            fa[r1] = r2;
    }
    rep1(i,1,n)
        if (fa[i]==i)
        {
            int cnt = 0,tw = 0,tc = 0;
            rep1(j,1,n)
                if (ff(j)==i)
                {
                    bag[++cnt] = j;
                    tw+= w[j],tc+= c[j];
                }
            rep2(j,W,0)
            {
                rep1(k,1,cnt)
                    if (j >= w[bag[k]])
                        f[j] = max(f[j],f[j-w[bag[k]]]+c[bag[k]]);
                if (j>=tw)
                    f[j] = max(f[j],f[j-tw]+tc);
            }
        }
    cout << f[W];
    return 0;
}
【42.86%】【codeforces 742D】Arpa's weak amphitheater and Mehrdad's valuable Hoses的更多相关文章
- 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 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 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses
		
[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...
 - 并查集+背包 【CF741B】 Arpa's weak amphitheater and Mehrdad's valuable Hoses
		
Descirption 有n个人,每个人都有颜值bi与体重wi.剧场的容量为W.有m条关系,xi与yi表示xi和yi是好朋友,在一个小组. 每个小组要么全部参加舞会,要么参加人数不能超过1人. 问保证 ...
 - codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
		
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
 - 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 而且如果邀请 ...
 - Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
		
<题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...
 - 【42.86%】【Codeforces Round #380D】Sea Battle
		
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
 
随机推荐
- BZOJ1576: [Usaco2009 Jan]安全路经Travel(树链剖分)
			
Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...
 - Linux 服务器初始配置步骤
			
1.IP配置 vim /etc/sysconfig/network-scripts/ifcfg- BOOTPROTO=dhcp 改成 static ONBOOT=no 改成 yes 添加 IPADDR ...
 - Vue 学习记录<1>
			
1.环境搭建:(前提node.js搭建) # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue i ...
 - select下拉列表选中后,跳转新链接
			
1.在当前页面打开新链接 <select onchange="location.href=this.options[this.selectedIndex].value" na ...
 - Spring MVC基础了解
			
参考网址:https://www.yiibai.com/spring_mvc/springmvc_overview.html Spring框架相关 Spring Security 一个灵活强大的身份验 ...
 - 【Codeforces Round #432 (Div. 2) A】 Arpa and a research in Mexican wave
			
[链接]h在这里写链接 [题意] 在这里写题意 [题解] t<=k,输出t t>=n,输出k-t+n 其他情况都是k [错的次数] 0 [反思] 在这了写反思 [代码] /* */ #in ...
 - elementUI upload 对图片的宽高做校验
			
很开心今天中午没有吃饭!原因是一直没有解决掉一个小问题,于是一直试错,最后看了下源码才有了点头绪,历时四五个小时才解决掉,有点怀疑自己的能力了,所以写下此文,记录一下今天的囧况!一般情况下遇到问题,自 ...
 - Javascript和jquery事件--事件监听器
			
之前看完了js和jq的冒泡捕获和事件对象event,这里看看同时使用js和jq后我最容易混淆的监听器的绑定. (1) js的监听器绑定解绑 绑定监听器有两种方式: a.on-事件type,比如oncl ...
 - Day1:注释
			
一.注释方法 1.单行注释用#,本行#号后的内容为注释内容,不执行 2.多行用三个单引号或三个双引号标注,中间内容为注释,不执行 二.其他相关内容 三个引号中的内容还可以当作字符串赋值给变量,可以同时 ...
 - 【习题 3-9 UVA - 10340】All in All
			
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 相当于让你判断s1是不是s2的子序列. for一遍就好 [代码] #include <bits/stdc++.h> us ...