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

Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces - 742D (分组背包)

2017-09-17 21:49 861 查看
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 xiand 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.

Example

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个hos每个hos有weight重量,beauty颜值,有些hos在一个朋友圈里,每个圈,要么请一个,要么全请,使得在W重量限制下,总颜值最大。

思路:对于每个组可以将体积由大到小更新一遍,对于每个体积,将该组成员遍历一遍如果可以更新该体积就更新,这样可行性在于,每个体积在每个组里都只是挑选了一个或者不选来更新体积。其实和0,1思想是一样的,不过是0,1每个组里只有一个元素罢啦。

#include <stdio.h>
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
using namespace std;

int w[1005];
int b[1005];
int pre[1005];
int mp[1005];
int sumw[1005];
int sumb[1005];
int dp[1005];
vector <int> vc[1005];

int Find(int x)
{
return x == pre[x] ? x : pre[x] = Find(pre[x]);
}

void Join(int x,int y)
{
int fx = Find(x);
int fy = Find(y);
if(fx != fy)
{
pre[fy] = fx;
}
}

int main()
{
int n,m,w_;
scanf("%d%d%d",&n,&m,&w_);
for(int i = 1; i <= n; i++)
{
pre[i] = i;
scanf("%d",&w[i]);
}
for(int j = 1; j <= n; j++)
{
scanf("%d",&b[j]);
}
for(int i = 0; i < m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
Join(x,y);
}
int k = 1;
for(int i = 1; i <= n; i++)
{
int tmp;
tmp = Find(i);
if(!mp[tmp]) mp[tmp] = k++;
vc[mp[tmp]].push_back(i);
sumw[mp[tmp]] += w[i];
sumb[mp[tmp]] += b[i];
}
for(int i = 1; i < k; i++)
{
for(int v = w_; v >= 1; v--)
{
for(int j = 0; j < vc[i].size(); j++)
{
int ii = vc[i][j];
if(v >= w[ii])
{
dp[v] = max(dp[v],dp[v-w[ii]] + b[ii]);
}
}
if(v >= sumw[i])
{
dp[v] = max(dp[v],dp[v-sumw[i]] + sumb[i]);
}
}
}
cout << dp[w_] <<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐