B. 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 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.

思路:并查集+背包;

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<math.h>
6 #include<queue>
7 #include<stdlib.h>
8 #include<set>
9 #include<vector>
10
11 typedef long long LL;
12 using namespace std;
13 vector<int>vec[1005];
14 int bin[1005];
15 int du[1005];
16 int ans[1005];
17 int bns[1005];
18 int aa[1005];
19 int bb[1005];
20 bool flag[1005];
21 int dp[1005][2000];
22 int main(void)
23 {
24 int n,m,k;
25 while(scanf("%d %d %d",&n,&m,&k)!=EOF)
26 {
27 int i,j;
28 memset(dp,0,sizeof(dp));
29 memset(flag,0,sizeof(flag));
30 for(i = 0; i<=1000; i++)
31 {
32 vec[i].clear();
33 du[i] = 1;
34 bin[i] = i;
35 }
36 for(i = 1; i <= n; i++)
37 {
38 scanf("%d",&ans[i]);
39 aa[i] = ans[i];
40 }
41 for(i = 1; i <= n; i++)
42 {
43 scanf("%d",&bns[i]);
44 bb[i] = bns[i];
45 }
46 while(m--)
47 {
48 int x,y;
49 scanf("%d %d",&x,&y);
50 int xx,yy;
51 for(xx = x; xx!=bin[xx];)
52 xx = bin[xx];
53 for(yy = y; yy!=bin[yy];)
54 yy =bin[yy];
55 if(xx !=yy)
56 {
57 if(du[xx] > du[yy])
58 {
59 du[xx]+=du[yy];
60 bin[yy] = xx;
61 }
62 else
63 {
64 du[yy]+=du[xx];
65 bin[xx] = yy;
66 }
67 }
68 }
69 for(i = 1; i <= n; i++)
70 {
71 int xx ;
72 for(xx = i ; xx!=bin[xx];)
73 xx = bin[xx];
74 if(xx!=i)
75 {
76 flag[i] = true;
77 ans[xx]+=ans[i];
78 bns[xx]+=bns[i];
79 vec[xx].push_back(i);
80 }
81 vec[i].push_back(i);
82
83 }
84 for(i = 1; i <= n; i++)
85 {
86 if(!flag[i])
87 {
88 for(j = 0; j <=k; j++)
89 {
90 dp[i][j] = dp[i-1][j];
91 }
92 for(j = ans[i];j <= k;j++)
93 {
94 dp[i][j] = max(dp[i][j],dp[i-1][j-ans[i]]+bns[i]);
95 }
96 for(j = 0;j < vec[i].size();j++)
97 {
98 int s;int kk = vec[i][j];
99 for(s = 0;s <=k;s++)
100 {
101 dp[i][s] = max(dp[i][s],dp[i-1][s]);
102 }
103 for(s = aa[kk];s <= k;s++)
104 {
105 dp[i][s] = max(dp[i][s],dp[i-1][s-aa[kk]]+bb[kk]);
106 }
107 }
108 }
109 else
110 {
111 for(j = 0;j <= k;j++)
112 dp[i][j] = dp[i-1][j];
113 }
114 }
115
116 printf("%d\n",dp[n][k]);
117 }
118 return 0;
119 }

最新文章

  1. shell note
  2. Hadoop 数据库 - HBase
  3. 封装insertAfter、addClass、格式化时间
  4. Cookie和Session专题
  5. C#使用oledb方式将excel数据导入到datagridview后数据被截断为 255 个字符
  6. 那天有个小孩跟我说LINQ(四)转载
  7. 利用Linux命令行进行文本按行去重并按重复次数排序
  8. 在Activity之间传递数据—简单数据/Bundle
  9. 【LCT】一步步地解释Link-cut Tree
  10. vb.net 代码建立控件,并显示在窗体上
  11. Spark MLlib FPGrowth关联规则算法
  12. SQL server 导出平面文件时出错: The code page on Destination - 3_txt.Inputs[Flat File Destination Input].Columns[UserId] is 936 and is required to be 1252.
  13. 谈谈 git 撤销操作
  14. NRF51822之RNG
  15. ListView嵌套 EditText的光标不显示
  16. SDN交换机迁移2
  17. linux nohup screen注解
  18. Android 中的广播(Broadcast)
  19. ArcGIS ArcMap 问题(ArcMap闪退、cx_oracle安装不上)
  20. H5新手教程,小白来看看。

热门文章

  1. Oracle-trunc函数、round 函数、ceil函数和floor函数---处理数字函数使用
  2. Linux-普通用户和root用户任意切换
  3. linux 实用指令压缩和解压类
  4. How To Call An Ambulance
  5. CentOS7 搭建maven私服Nexus
  6. 什么是 IP 地址 – 定义和解释
  7. Android权限级别(protectionLevel)
  8. KMP算法中的next函数
  9. @Value(&quot;#{}&quot;)与@Value(&quot;${}&quot;)
  10. Linux基础命令---htdigest建立和更新apache服务器摘要