码迷,mamicode.com
首页 > 其他好文 > 详细

Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces - 742D

时间:2017-05-23 00:24:49      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:pre   i++   possible   width   .com   tle   nice   contains   ati   

 

   

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.

Example

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.并查集管理集合。

2.把集合看成一件物品,因为要么从集合中取一个元素,要么取整个集合,那么就对这个集合中的每件物品背一次,然后对这件物品背一次。最后背完所有的物品就是答案。。。

易懂的的代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<vector>
 5 using namespace std;
 6 
 7 const int INF=0x3f3f3f3f;
 8 const int N=100010;
 9 
10 int dp[1010];
11 int fa[1010],c[1010],w[1010];
12 
13 vector<int> vec[1010];
14 
15 int Find(int x){
16     if(x==fa[x]) return x;
17     return fa[x]=Find(fa[x]);
18 }
19 
20 int main()
21 {   int n,m,V;
22     scanf("%d%d%d",&n,&m,&V);
23     for(int i=1;i<=n;i++) scanf("%d",&c[i]);
24     for(int i=1;i<=n;i++) scanf("%d",&w[i]);
25     for(int i=1;i<=n;i++) fa[i]=i;
26     for(int i=0;i<m;i++){
27         int u,v;
28         scanf("%d%d",&u,&v);
29         u=Find(u),v=Find(v);
30         if(u!=v) fa[u]=v;
31     }
32     for(int i=1;i<=n;i++) vec[Find(i)].push_back(i);
33     
34     for(int i=1;i<=n;i++){
35         if(fa[i]==i){
36             for(int j=V;j>=0;j--){
37                  int sumc=0,sumw=0;
38                  for(int k=0;k<vec[i].size();k++){
39                      sumc+=c[vec[i][k]];
40                      sumw+=w[vec[i][k]];
41                      if(j>=c[vec[i][k]]) dp[j]=max(dp[j],dp[j-c[vec[i][k]]]+w[vec[i][k]]);
42                     }
43                  if(j>=sumc) dp[j]=max(dp[j],dp[j-sumc]+sumw);
44             }
45         }
46     }
47     cout<<dp[V]<<endl;
48     return 0;
49 }

 看不懂的代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<vector>
 5 using namespace std;
 6 
 7 const int INF=0x3f3f3f3f;
 8 const int N=100010;
 9 
10 int dp[2][1010];
11 int fa[1010],b[1010],w[1010];
12 
13 vector<int> vec[1010];
14 
15 int Find(int x){
16     if(x==fa[x]) return x;
17     return fa[x]=Find(fa[x]);
18 }
19 
20 int main()
21 {   int n,m,x;
22     scanf("%d%d%d",&n,&m,&x);
23     for(int i=1;i<=n;i++) scanf("%d",&w[i]);
24     for(int i=1;i<=n;i++) scanf("%d",&b[i]);
25     for(int i=1;i<=n;i++) fa[i]=i;
26     for(int i=0;i<m;i++){
27         int u,v;
28         scanf("%d%d",&u,&v);
29         u=Find(u),v=Find(v);
30         if(u!=v) fa[u]=v;
31     }
32     for(int i=1;i<=n;i++)
33         vec[Find(i)].push_back(i);
34     int now=0;
35     for(int i=1;i<=n;i++){
36         now=!now;
37         int ww=0,bb=0;
38         for(int j=0;j<=x;j++) dp[now][j]=dp[!now][j];
39         for(int j=0;j<vec[i].size();j++){
40             int id=vec[i][j];
41             int www=w[id],bbb=b[id];
42             ww+=www,bb+=bbb;
43             for(int j=www;j<=x;j++)
44                dp[now][j]=max(dp[now][j],dp[!now][j-www]+bbb); 
45         }
46         for(int j=ww;j<=x;j++)
47             dp[now][j]=max(dp[now][j],dp[!now][j-ww]+bb);
48     }
49     int ans=0;
50     for(int i=0;i<=x;i++) ans=max(ans,dp[now][i]);
51     cout<<ans<<endl;
52     return 0;
53 }

 

 

Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces - 742D

标签:pre   i++   possible   width   .com   tle   nice   contains   ati   

原文地址:http://www.cnblogs.com/zgglj-com/p/6891862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!