博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【42.86%】【codeforces 742D】Arpa's weak amphitheater and Mehrdad's valuable Hoses
阅读量:4560 次
发布时间:2019-06-08

本文共 4229 字,大约阅读时间需要 14 分钟。

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.

【题目链接】:

【题解】

把原本的n个人看成是n个物品.
特殊的,在同一个朋友组里面的所有人她们的漂亮值和体重和也作为一个新的物品.
重量作为代价、漂亮值作为收益.求最大收益.
对这n+x个物品做01背包就可以了.
【完整代码】

#include 
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
pii;typedef pair
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;}

转载于:https://www.cnblogs.com/AWCXV/p/7626827.html

你可能感兴趣的文章
字符集
查看>>
《Linux命令行与shell脚本编程大全 第3版》Linux命令行---51
查看>>
《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---27
查看>>
jQuery最佳实践
查看>>
DelphiXE2 DataSnap开发技巧收集
查看>>
HoloLens开发手记 - Unity之Keyboard input 键盘输入
查看>>
关于日历
查看>>
荣品KING3288开发板升级啦!
查看>>
系统产生死锁的四个必要条件
查看>>
初心易得,始终难守
查看>>
北京集训DAY4
查看>>
编译 ACE
查看>>
JDBC(1)
查看>>
《程序是怎样跑起来的》第五章
查看>>
配置SSH单向无密码访问
查看>>
深入浅出Docker(五):基于Fig搭建开发环境
查看>>
ubuntu apt 代理设置
查看>>
第四章—变量,作用域和内存问题(二)
查看>>
MySQL日期处理函数_20160922
查看>>
Mysql存储引擎以及锁机制
查看>>