您的位置:首页 > 编程语言 > Lua

Codeforces Round #383 (Div. 2)D.Arpa's weak amphitheater and Mehrdad's valuable Hoses【并查集+分组背包】

2016-12-07 12:01 651 查看
D. 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
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 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个人,每个人对应有一个重量和美丽值,我们的目的是要分配一个到场方案,使得重量和在w限制范围内,美丽值最大。

对应我们已知M个朋友关系,朋友圈中的人要么都到场,要么到场一个人,当然也可以一个都不到。

思路:

1、首先处理这M个朋友关系,我们直接并查集处理即可,对应每个朋友关系,将两个人合并到同一个集合。

2、接下来套一个分组背包。

所谓分组背包,其实也是01背包,只不过因为有限制关系,要么上一个人,要么所有人都上。所以我们这里将这些情况全部都规为一个背包,我们在选取过程中Dp出最优即可。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int w[5000];
int val[5000];
int f[5000];
int dp[5000];
vector<int >mp[5000];
int find(int a)
{
int r=a;
while(f[r]!=r)
r=f[r];
int i=a;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(int a,int b)
{
int A,B;
A=find(a);
B=find(b);
if(A!=B)
f[B]=A;
}
int main()
{
int n,m,ww;
while(~scanf("%d%d%d",&n,&m,&ww))
{
for(int i=1;i<=n;i++)mp[i].clear();
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=n;i++)scanf("%d",&w[i]);
for(int i=1;i<=n;i++)scanf("%d",&val[i]);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
merge(x,y);
}
for(int i=1;i<=n;i++)
{
find(i);
mp[f[i]].push_back(i);
}
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=ww;j>=0;j--)
{
int sumw=0;
int sumval=0;
for(int kk=0;kk<mp[i].size();kk++)
{
sumw+=w[mp[i][kk]];
sumval+=val[mp[i][kk]];
if(j-w[mp[i][kk]]>=0)
dp[j]=max(dp[j],dp[j-w[mp[i][kk]]]+val[mp[i][kk]]);
}
if(j>=sumw)
{
dp[j]=max(dp[j],dp[j-sumw]+sumval);
}
}
}
printf("%d\n",dp[ww]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces#383Div. 2