Codeforces 741B 并查集+dp
2016-12-07 12:25
351 查看
Arpa's weak amphitheater and Mehrdad's valuable Hoses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 xiand 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
output
input
output
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.
题意:给你n个人 每个人的重量和价值 现在把n个人划分成若干个朋友圈 对于每个朋友圈 你可以邀请1人 或者邀请全部 也可以不邀请人 现在问总重量不超过w 价值最大
题解:先用并查集处理出朋友圈的人的标号 然后对于每个朋友圈加一个重量为0价值为0的人 重量为sumweight价值为sumval的人 那么问题就变成了
从若干个朋友圈中每个朋友圈选一个人能组成的最大价值
定义dp[i][j]为前i个朋友圈重量不超过j的最大价值 背包dp过去即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>sp[1005];
int pre[1005],num[2005],val[2005],dp[1005][1005];
int find(int x){
int r=x;
while(pre[r]!=r)
r=pre[r];
int i=x,j;
while(i!=r){
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void join(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy)pre[fx]=fy;
}
int main(){
int n,m,k,i,j,w;
scanf("%d%d%d",&n,&m,&w);
for(i=0;i<=1004;i++)pre[i]=i;
for(i=1;i<=n;i++)scanf("%d",&num[i]);
for(i=1;i<=n;i++)scanf("%d",&val[i]);
for(i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
join(x,y);
}
int q[1005],cnt=0;
for(i=1;i<=n;i++){
int root=find(i);
sp[root].push_back(i);
q[++cnt]=root;
}
sort(q+1,q+1+cnt);
cnt=unique(q+1,q+1+cnt)-q-1;
for(i=1;i<=cnt;i++){
int nu=0,we=0;
for(j=0;j<sp[q[i]].size();j++){
we+=num[sp[q[i]][j]];
nu+=val[sp[q[i]][j]];
}
sp[q[i]].push_back(0);
sp[q[i]].push_back(++n);
num
=we;
val
=nu;
}
for(i=1;i<=cnt;i++){
for(k=0;k<sp[q[i]].size();k++) {
for(j=1;j<=w;j++){
if(j>=num[sp[q[i]][k]])dp[i][j]=max(dp[i][j],dp[i-1][j-num[sp[q[i]][k]]]+val[sp[q[i]][k]]);
}
}
}
printf("%d\n",dp[cnt][w]);
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 xiand 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 66 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.
题意:给你n个人 每个人的重量和价值 现在把n个人划分成若干个朋友圈 对于每个朋友圈 你可以邀请1人 或者邀请全部 也可以不邀请人 现在问总重量不超过w 价值最大
题解:先用并查集处理出朋友圈的人的标号 然后对于每个朋友圈加一个重量为0价值为0的人 重量为sumweight价值为sumval的人 那么问题就变成了
从若干个朋友圈中每个朋友圈选一个人能组成的最大价值
定义dp[i][j]为前i个朋友圈重量不超过j的最大价值 背包dp过去即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>sp[1005];
int pre[1005],num[2005],val[2005],dp[1005][1005];
int find(int x){
int r=x;
while(pre[r]!=r)
r=pre[r];
int i=x,j;
while(i!=r){
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void join(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy)pre[fx]=fy;
}
int main(){
int n,m,k,i,j,w;
scanf("%d%d%d",&n,&m,&w);
for(i=0;i<=1004;i++)pre[i]=i;
for(i=1;i<=n;i++)scanf("%d",&num[i]);
for(i=1;i<=n;i++)scanf("%d",&val[i]);
for(i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
join(x,y);
}
int q[1005],cnt=0;
for(i=1;i<=n;i++){
int root=find(i);
sp[root].push_back(i);
q[++cnt]=root;
}
sort(q+1,q+1+cnt);
cnt=unique(q+1,q+1+cnt)-q-1;
for(i=1;i<=cnt;i++){
int nu=0,we=0;
for(j=0;j<sp[q[i]].size();j++){
we+=num[sp[q[i]][j]];
nu+=val[sp[q[i]][j]];
}
sp[q[i]].push_back(0);
sp[q[i]].push_back(++n);
num
=we;
val
=nu;
}
for(i=1;i<=cnt;i++){
for(k=0;k<sp[q[i]].size();k++) {
for(j=1;j<=w;j++){
if(j>=num[sp[q[i]][k]])dp[i][j]=max(dp[i][j],dp[i-1][j-num[sp[q[i]][k]]]+val[sp[q[i]][k]]);
}
}
}
printf("%d\n",dp[cnt][w]);
return 0;
}
相关文章推荐
- codeforces 741B
- Codeforces 741B-Arpa's weak amphitheater and Mehrdad's valuable Hoses
- codeforces 741B
- 【CodeForces】Plate Game(博弈)
- codeforces 614 B Gena's Code
- Codeforces 702D - Road to Post Office
- Problem--118A--Codeforces--String Task
- codeforces 818E Card Game Again (尺取法)
- CodeForces 632 E.Thief in a Shop(生成函数+FFT+快速幂)
- 第二十四次codeforces竞技结束 #281 Div 2
- codeforces 577A Multiplication Table【思维】
- Codeforces 327C 乘法逆元 + 费马小定理 || 等比数列二分求和取模
- Codeforces 765D Artsem and Saunders 构造
- Codeforces 822D My pretty girl Noora - 线性筛 - 动态规划
- CodeForces 803 F.Coprime Subsequences(莫比乌斯反演+组合数学)
- Codeforces 3B. Lorry
- CodeForces 349A Cinema Line
- CodeForces 534C Polycarpus' Dice (数学)
- Codeforces 766 C Mahmoud and a Message 详解(DP)
- CodeForces 9C -Hexadecimal's Numbers-构造