zoukankan      html  css  js  c++  java
  • Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D


    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 nm and w (1  ≤  n  ≤  10001 ≤ 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 ≤ nxi ≠ yi), meaning that Hoses xiand 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.通过并查集,得出每一组有哪些人,并且将这些人的信息重新归类。

    2.对于每一组,要么全选,要么只选一人,要么都不选。那么将全选的又看成一个“人 ”,并放入这个集合中,所以就变成了01背包了。

    3.代码中,cnt为集合的个数,c[i]为集合i的元素个数,x[i][j]、y[i][j]为i集合中第j个元素的体重和颜值。

    类似的题:http://blog.csdn.net/dolfamingo/article/details/73438052


    写法一:

     1 #include<bits/stdc++.h>
     2 //#define LOCAL
     3 using namespace std;
     4 typedef long long LL;
     5 const double eps = 1e-6;
     6 const int INF = 2e9;
     7 const LL LNF = 9e18;
     8 const int mod = 1e9+7;
     9 const int maxn = 1e3+10;
    10 
    11 int n, m, w, a[maxn], b[maxn];
    12 int fa[maxn], x[maxn][maxn], y[maxn][maxn], c[maxn], cnt, dp[maxn][maxn], vis[maxn];
    13 
    14 int find(int x) { return (x==fa[x])?x:x=find(fa[x]); }
    15 
    16 void init()
    17 {
    18     scanf("%d%d%d",&n,&m,&w);
    19     for(int i = 1; i<=n; i++)
    20         scanf("%d",&a[i]);
    21     for(int i = 1; i<=n; i++)
    22         scanf("%d",&b[i]);
    23     for(int i = 1; i<=n; i++)
    24         fa[i] = i;
    25 
    26     for(int i = 1; i<=m; i++)
    27     {
    28         int u, v;
    29         scanf("%d%d",&u, &v);
    30         u = find(u);
    31         v = find(v);
    32         if(u!=v)
    33             fa[u] = v;
    34     }
    35 
    36     for(int i = 1; i<=n; i++)
    37     {
    38         int f = find(i);
    39         if(!vis[f]) vis[f] = ++cnt;
    40         f = vis[f]; //将f赋值为集合的序号
    41         ++c[f]; //集合内元素的个数
    42         x[f][c[f]] = a[i];
    43         y[f][c[f]] = b[i];
    44     }
    45 
    46     for(int i = 1; i<=cnt; i++) //全部都取,相当于每个集合增加一个元素
    47     {
    48         int X = 0, Y = 0;
    49         for(int j = 1; j<=c[i]; j++)
    50         {
    51             X += x[i][j];
    52             Y += y[i][j];
    53         }
    54         ++c[i];
    55         x[i][c[i]] = X;
    56         y[i][c[i]] = Y;
    57     }
    58 }
    59 
    60 void solve()
    61 {
    62     for(int i = 1; i<=cnt; i++)
    63     for(int j = 1; j<=w; j++)
    64     {
    65         for(int k = 1; k<=c[i]; k++)    //Remember!!!
    66             dp[i][j] = dp[i-1][j];
    67 
    68         for(int k = 1; k<=c[i]; k++)
    69         if(j>=x[i][k])
    70             dp[i][j] = max(dp[i][j], dp[i-1][j-x[i][k]]+y[i][k]);
    71     }
    72 
    73     cout<< dp[cnt][w]<<endl;
    74 }
    75 
    76 int main()
    77 {
    78 #ifdef LOCAL
    79     freopen("input.txt", "r", stdin);
    80 //      freopen("output.txt", "w", stdout);
    81 #endif
    82     init();
    83     solve();
    84 }
    View Code


    写法二:

     1 #include<bits/stdc++.h>
     2 //#define LOCAL
     3 using namespace std;
     4 typedef long long LL;
     5 const double eps = 1e-6;
     6 const int INF = 2e9;
     7 const LL LNF = 9e18;
     8 const int mod = 1e9+7;
     9 const int maxn = 1e3+10;
    10 
    11 int n, m, w, a[maxn], b[maxn];
    12 int fa[maxn], x[maxn][maxn], y[maxn][maxn], c[maxn], cnt, dp[maxn], vis[maxn];
    13 
    14 int find(int x) { return (x==fa[x])?x:x=find(fa[x]); }
    15 
    16 void init()
    17 {
    18     scanf("%d%d%d",&n,&m,&w);
    19     for(int i = 1; i<=n; i++)
    20         scanf("%d",&a[i]);
    21     for(int i = 1; i<=n; i++)
    22         scanf("%d",&b[i]);
    23     for(int i = 1; i<=n; i++)
    24         fa[i] = i;
    25 
    26     for(int i = 1; i<=m; i++)
    27     {
    28         int u, v;
    29         scanf("%d%d",&u, &v);
    30         u = find(u);
    31         v = find(v);
    32         if(u!=v)
    33             fa[u] = v;
    34     }
    35 
    36     for(int i = 1; i<=n; i++)
    37     {
    38         int f = find(i);
    39         if(!vis[f]) vis[f] = ++cnt;
    40         f = vis[f]; //将f赋值为集合的序号
    41         ++c[f]; //集合内元素的个数
    42         x[f][c[f]] = a[i];
    43         y[f][c[f]] = b[i];
    44     }
    45 
    46     for(int i = 1; i<=cnt; i++) //全部都取,相当于每个集合增加一个元素
    47     {
    48         int X = 0, Y = 0;
    49         for(int j = 1; j<=c[i]; j++)
    50         {
    51             X += x[i][j];
    52             Y += y[i][j];
    53         }
    54         ++c[i];
    55         x[i][c[i]] = X;
    56         y[i][c[i]] = Y;
    57     }
    58 }
    59 
    60 void solve()
    61 {
    62     for(int i = 1; i<=cnt; i++) //01背包
    63     for(int j = w; j>=1; j--)
    64     for(int k = 1; k<=c[i]; k++)
    65         if(j>=x[i][k])
    66             dp[j] = max(dp[j], dp[j-x[i][k]]+y[i][k]);
    67 
    68     cout<< dp[w]<<endl;
    69 }
    70 
    71 int main()
    72 {
    73 #ifdef LOCAL
    74     freopen("input.txt", "r", stdin);
    75 //      freopen("output.txt", "w", stdout);
    76 #endif
    77     init();
    78     solve();
    79 }
    View Code


  • 相关阅读:
    java中static的用法
    java策略设计模式
    java模板设计模式
    Spring 学习笔记 8. 尚硅谷_佟刚_Spring_使用外部属性文件
    Spring 学习笔记 7. 尚硅谷_佟刚_Spring_Bean 的作用域
    Spring学习笔记 6. 尚硅谷_佟刚_Spring_Bean 之间的关系
    Spring学习笔记 5. 尚硅谷_佟刚_Spring_自动装配
    Spring 学习笔记 4. 尚硅谷_佟刚_Spring_属性配置细节
    Spring 学习笔记 3. 尚硅谷_佟刚_Spring_配置 Bean
    Spring 学习笔记 2. 尚硅谷_佟刚_Spring_IOC&DI概述
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538649.html
Copyright © 2011-2022 走看看