Problem Link

741B Arpa’s weak amphitheater and Mehrdad’s valuable Hoses

Analysis

It’s a simple knapsack problem. Let’s solve this version of knapsack problem first: we have n sets of items, each item has value and weight, find the maximum value we can earn if we can choose at most one item from each set and sum of the chosen items must be less than or equal to W. Let dpw be the max value we can earn if sum of weights of chosen items are less than or equal to w. Now iterate on sets one by one and update dp as follows: for each item X, and for each weight w, newDpw = max(newDpw, oldDpw - X.weight + X.value).

Run dfs and find groups at first. The problem is same with above problem, each group is some set in above problem, just add whole group as an item to the set that related to this group.

Time complexity: O(n·W).

AC code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>using namespace std;
typedef long long LL;
const LL maxn = 1e3+10;int uf[maxn];
int w[maxn],b[maxn];
int ww[maxn],bb[maxn];
int dp[2][maxn];
int vis[maxn];
int find(int x){return uf[x] == x? x:uf[x] = find(uf[x]);}
void UNION(int x,int y){uf[find(x)] = find(y);
}
int n,m,W;int main()
{//freopen("H:\\c++\\file\\stdin.txt","r",stdin);//freopen("H:\\c++\\file\\stdout.txt","w",stdout);cin>>n>>m>>W;for(int i=1 ; i<=n ; ++i)cin>>w[i];for(int i = 1 ; i<=n ; ++i)cin>>b[i];for(int i=1 ; i<=n ; ++i)uf[i] = i;while(m--){int x,y;cin>>x>>y;UNION(x,y);}memset(ww,0,sizeof(ww));memset(bb,0,sizeof(bb));for(int i=1 ; i<=n ; ++i){ww[find(i)]+=w[i];bb[find(i)]+=b[i];}memset(dp,0,sizeof(dp));memset(vis,false,sizeof(vis));int t = 0;for(int i=1 ; i<=n ; ++i){int pp = find(i);if(vis[pp])continue;vis[pp] = true;t^=1;for(int j = 1; j<=W ; ++j){dp[t][j] = dp[t^1][j];if(j>=ww[pp])dp[t][j] = max(dp[t][j],dp[t^1][j-ww[pp]]+bb[pp]);}for(int j = 1 ; j<=n ; ++j){if(find(j)!=pp)continue;for(int k = 1; k<=W ; ++k)if(k>=w[j])dp[t][k] = max(dp[t][k],dp[t^1][k-w[j]]+b[j]);}}int ans =0;for(int i=1;  i<=W ; ++i)ans = max(ans,dp[t][i]);cout<<ans<<endl;return 0;
}

codeforce 741 B. Arpa's weak amphitheater and Mehrdad's valuable Hoses(背包 dp)相关推荐

  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 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 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  4. CF - 741(C. Arpa’s overnight party and Mehrdad’s silent entering) 二分图构造

    题目链接 题意 已经给出情侣的座位编号,将两种饭分配给N对情侣.满足以下两个要求: 情侣之间不能吃相同的饭 座位号连续的三个人不能吃同一种饭 问是否存在满足的解. 思路 构造二分图 对应情侣的座位建边 ...

  5. 【CodeForces】741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)

    [题意]给定n个点的树,每条边有一个小写字母a~v,求每棵子树内的最长回文路径,回文路径定义为路径上所有字母存在一种排列为回文串.n<=5*10^5. [算法]dsu on tree [题解]这 ...

  6. 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的 ...

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

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

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

  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. 滑动窗口,拥塞控制算法
  2. python pycurl_简单谈谈Python的pycurl模块_python
  3. s := time.Now().Format(20060102) go格式化
  4. CUDA系列学习(一)An Introduction to GPU and CUDA
  5. JVM堆老年代分配比例
  6. 【数据挖掘】视频版权检测优胜解决方案
  7. 微型计算机硬件技术基础答案,计算机硬件技术基础_万晓冬_习题
  8. ReactNative(API)Alert
  9. qtcreator摄像头显示时间_三星Galaxy S11 +渲染器展现出巨大的相机凹凸和四边形曲面显示...
  10. 只看影子就能还原视频,MIT新算法让摄像头无死角
  11. Flask框架 之重定向、cookie和session
  12. 20 分钟教你量化自己的资产
  13. 专网网速测试软件,CMD怎么测试网速?测网速用命令PING
  14. 金仓数据库KingbaseES之自增列
  15. 桌面cpu与服务器cpu天梯,桌面CPU性能排行 CPU天梯图2017年6月最新版
  16. 钢铁业步履蹒跚,中钢如何借助 IBM 率先实现大数据与 AI 化转型?
  17. vue 时间方法(yyyy-mmmm-dddd hh:mm:ss)
  18. QPS、TPS、并发用户数、吞吐量
  19. Win7加密访问共享文件夹
  20. Fe3O4纳米颗粒的表面接枝修饰/氨基乙酸|L-半胱氨酸(L-Cys)修饰的Fe3O4包裹TiO2(Fe3O4@TiO2/L-Cys)复合纳米粒子

热门文章

  1. Excel中的空值与数值比较
  2. 从苏宁电器到卡巴斯基(第二部)第08篇:我在卡巴的日子 VIII
  3. 为什么公司宁愿花 15k 重招一个新人,也不愿加薪 5k 留住老程序员?
  4. Python的5大就业方向,学会一个都是赚
  5. H3C UniServer R6900 G3 服务器介绍
  6. 在linux系统上安装坚果云
  7. lstm实例:基于lstm的DGA域名检测
  8. Unity 射线检测
  9. JavaIO流——文件的读取与传输
  10. linux微信登陆失败,微信登陆失败原因与解决方法