Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集+分组背包)
来源:互联网 发布:邮件传输协议端口号 编辑:程序博客网 时间:2024/04/29 15:22
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.
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 xiand yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.
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.
3 1 53 2 52 4 21 2
6
4 2 112 4 6 66 4 2 11 22 3
7
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.
题目大意:有一个能够承受w重量的剧场,有n个人,每个人有wi的体重和bi的颜值,现在有m组关系,每组的人要么全部都参加要不不参加或者只参加一个人,求出这个剧场颜值最高多少
解题思路:对于每组这种情况,需要做的是利用并查集分组,然后对于每组记下相应的下标,之后用分组背包去做
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;typedef long long LL;struct people{LL weight,beautiful;}a[5005];LL dp[1005];vector<int> tu[1005];int check[1005];int n,m,w;int find(int x){if(check[x]==x)return x;check[x]=find(check[x]);return check[x];}void join(int x,int y){int t1=find(x);int t2=find(y);if(t1!=t2)check[t1]=t2;}int main(){int i,j,k,x,y;cin>>n>>m>>w;for(i=1;i<=n;i++){check[i]=i;cin>>a[i].weight;}for(i=1;i<=n;i++)cin>>a[i].beautiful;for(i=1;i<=m;i++){cin>>x>>y;join(x,y);}for(i=1;i<=n;i++)tu[find(check[i])].push_back(i);memset(dp,0,sizeof(dp));for(i=1;i<=n;i++){if(check[i]!=i)continue;for(j=w;j>=0;j--){LL cntw=0,cntb=0;for(k=0;k<tu[i].size();k++){cntw+=a[tu[i][k]].weight;cntb+=a[tu[i][k]].beautiful;if(j>=a[tu[i][k]].weight){dp[j]=max(dp[j],dp[j-a[tu[i][k]].weight]+a[tu[i][k]].beautiful);}}if(j>=cntw)dp[j]=max(dp[j],dp[j-cntw]+cntb);}}LL MAX=0;for(i=1;i<=w;i++)MAX=max(MAX,dp[i]);cout<<MAX<<endl;return 0;}
- Codeforces Round #383 (Div. 2)D.Arpa's weak amphitheater and Mehrdad's valuable Hoses【并查集+分组背包】
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集+分组背包)
- Codeforces#383D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集+分组背包)
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 并查集+双重01背包
- Codeforces Round #383 (Div. 2)-D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
- Codeforces742D-Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+并查集)
- Round 3 E-Arpa's weak amphitheater and Mehrdad's valuable Hoses( 并查集 分组背包)
- codeforce 742 D. Arpa's weak amphitheater and Mehrdad's valuable Hoses (分组背包+并查集))
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集+分组背包)
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(DP)
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)
- codefores741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集+背包)
- Codeforces724D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集+背包)
- Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces
- Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces
- Codeforces Round #383 (Div. 1) Problem B.Arpa's weak amphitheater and Mehrdad's valuable Hoses 解题报告
- codeforces 742d D - Arpa's weak amphitheater and Mehrdad's valuable Hoses
- CodeForces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses
- extjs日期的转换
- c++中stringstream 类的用法及字符串与数字之间转化
- extern关键字
- Mac node已安装 但是报-bash: npm: command not found
- SpringMVC使用Ajax使用JSON传递数据
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集+分组背包)
- 使用Toolbar与DrawerLayout实现侧滑菜单
- NOIP普及组2003
- Kafka SASL/PLAINTEXT 简单验证测试
- 证件照换底,红白蓝底随机换(附:一寸二寸证件照制作)
- POJ 1039 Pipe【计算几何+直线相交】
- C#datagridview动态添加行和列
- POJ 2406 Power Strings (kmp next数组)
- JAVA方法