题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值。

  分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =。

  代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <vector>
 5 using namespace std;
 6 const int N = 1000 + 5;
 7
 8 int w[N],b[N];
 9 int n,m,W;
10 int root[N];
11 vector<int> v[N];
12 int allw[N],allb[N];
13 int dp[N];
14 int find(int x) {return x == root[x] ? x : root[x] = find(root[x]);}
15
16 int main()
17 {
18     scanf("%d%d%d",&n,&m,&W);
19     for(int i=1;i<=n;i++) root[i] = i;
20     for(int i=1;i<=n;i++) scanf("%d",w+i);
21     for(int i=1;i<=n;i++) scanf("%d",b+i);
22     for(int i=1;i<=m;i++)
23     {
24         int x,y;scanf("%d%d",&x,&y);
25         int rx = find(x), ry = find(y);
26         if(rx != ry) root[rx] = ry;
27     }
28     for(int i=1;i<=n;i++)
29     {
30         int t = find(i);
31         v[t].push_back(i);
32         allw[t] += w[i];
33         allb[t] += b[i];
34     }
35     for(int i=1;i<=n;i++)
36     {
37         if(v[i].size() == 0) continue;
38         for(int j=W;j>=0;j--)
39         {
40             for(int k=0;k<v[i].size();k++)
41             {
42                 if(j-w[v[i][k]] >= 0) dp[j] = max(dp[j], dp[j-w[v[i][k]]] + b[v[i][k]]);
43             }
44             if(j-allw[i] >= 0) dp[j] = max(dp[j], dp[j-allw[i]] + allb[i]);
45         }
46     }
47     printf("%d\n",dp[W]);
48     return 0;
49 }

转载于:https://www.cnblogs.com/zzyDS/p/6190671.html

codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)相关推荐

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

  2. 【Codeforces 741 B. Arpa's weak amphitheater and Mehrdad's 】+ 并查集 + 01背包

    B. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  3. Codeforces 742B Arpa’s obvious problem and Mehrdad’s terrible solution

    http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible solutio ...

  4. Codeforces 742A Arpa’s hard exam and Mehrdad’s naive cheat 打表+水题

    A. Arpa's hard exam and Mehrdad's naive cheat time limit per test 1 second memory limit per test 256 ...

  5. Codeforces 741 D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    D - Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  6. Codeforces 742B B. Arpa’s obvious problem and Mehrdad’s terrible solution

    B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...

  7. CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    CF741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 好像这个题只能Dsu On Tree? 有根树点分治 统计子树过x的 ...

  8. codeforces 688 E. The Values You Can Make(01背包+思维)

    题目链接:http://codeforces.com/contest/688/problem/E 题解:设dp[s1][s2]表示s1状态下出现s2是否合理.那么s1显然可以更具01背包来得到状态.首 ...

  9. Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或)

    Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或) 题意: 3种操作: 1 插入一个数 2 删除一个数 3 给出一个数 ...

  10. 【codeforces 742A】Arpa’s hard exam and Mehrdad’s naive cheat

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

最新文章

  1. 使用ubuntu(18.04) 作为软路由器连接互联网
  2. Big Data Security Part One: Introducing PacketPig
  3. 打印给定字符串中字符的所有排列
  4. 一周一论文(翻译 总结)—— [DSN 18] RDMC A Reliable RDMA Multicast for Large Objects :一个面向大型对象的可靠的RDMA广播框架
  5. AI:Algorithmia《2021 enterprise trends in machine learning 2021年机器学习的企业趋势》翻译与解读
  6. 每天一道LeetCode-----将字符串切分,使每个子串都是回文串,计算所有可能结果和最小切分次数
  7. vue13过滤器 debounce延迟、limitBy、filterBy、orderBy
  8. 如何评价一个推荐系统的好坏?
  9. python迭代器举例_如何最简单、通俗地理解Python的迭代器?
  10. 镜头像差之二——色差
  11. 2020-11-18
  12. python下载整个网站_python 下载整个站点
  13. Mysql闪退解决方法
  14. Windows网上邻居 授权访问设置
  15. Dubbo——服务暴露的实现原理
  16. StringBuilder
  17. 赫耳墨斯与忒瑞西阿斯
  18. 12款多媒体能力SDK与开发平台盘点
  19. html思维导图word版,(完整word版)非谓语动词练习及思维导图
  20. ReactDOM.render(...) 渲染方法

热门文章

  1. 字符串(后缀自动机):COGS 2399. 循环同构
  2. HTTP协议(转自:小坦克博客)
  3. Solr -- 实时搜索
  4. 提升网络销售转化率的10种方法
  5. iOS -[JYBDScanCardManage configIDScan] in JYBDScanCardManage.o等静态库问题
  6. Xcode 自定义代码块及代码块迁移
  7. HDU 4335 What is N?(指数循环节)题解
  8. 20154322 杨钦涵 Exp2 后门原理与实践
  9. 三维坐标绕轴坐标旋转公式
  10. hdu 3339 In Action