http://codeforces.com/contest/742/problem/D

题目大意:有n个人,每个人有重量wi和魅力值bi。然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和c是朋友,那么a和c就是朋友。现在,把所有能作为朋友的人放在一个集合里面。你现在要开一个party,这个party的容量为W,现在,你每次只能选择一个集合里面的一个人或者选择集合里面的所有人进入这个party。在满足总w <= W的情况下,总魅力值b最大,问魅力值最大是多少?

思路:

定义dp(i, j)表示目前是第i个集合,party里面的重量为j的最大魅力值。

dp(i, j) = max(dp[i - 1][j], dp[i-1][j - 集合的总w(或集合的某个w)]) + 集合的总b(或集合中的某一个b)

复杂度O(n*n)

  1. //看看会不会爆int!数组会不会少了一维!
  2. //取物问题一定要小心先手胜利的条件
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #pragma comment(linker,"/STACK:102400000,102400000")
  6. #define LL long long
  7. #define ALL(a) a.begin(), a.end()
  8. #define pb push_back
  9. #define mk make_pair
  10. #define fi first
  11. #define se second
  12. #define haha printf("haha\n")
  13. const int maxn = + ;
  14. int dp[maxn][maxn];
  15. vector<pair<int, int> > subset[maxn];
  16. pair<int, int> f[maxn];
  17. int par[maxn];
  18. int n, m, w;
  19.  
  20. int pfind(int x){
  21. if (par[x] == x) return x;
  22. return par[x] = pfind(par[x]);
  23. }
  24.  
  25. map<int, int> id;
  26. int cnt = ;
  27. int get_id(int x){
  28. if (id.count(x) == ) id[x] = ++cnt;
  29. return id[x];
  30. }
  31.  
  32. int main(){
  33. cin >> n >> m >> w;
  34. for (int i = ; i <= n; i++) par[i] = i;
  35. for (int i = ; i <= n; i++)
  36. scanf("%d", &f[i].fi);
  37. for (int i = ; i <= n; i++)
  38. scanf("%d", &f[i].se);
  39. for (int i = ; i <= m; i++){
  40. int u, v; scanf("%d%d", &u, &v);
  41. int pu = pfind(u), pv = pfind(v);
  42. par[pv] = pu;
  43. }
  44.  
  45. for (int i = ; i <= n; i++){
  46. int myid = get_id(pfind(i));
  47. subset[myid].pb(f[i]);
  48. }
  49.  
  50. for (int i = ; i <= cnt; i++){
  51. for (int j = w; j >= ; j--){
  52. int totw = , totb = ;
  53. dp[i][j] = dp[i - ][j];
  54. for (int k = ; k < subset[i].size(); k++){
  55. pair<int, int> p = subset[i][k];
  56. totw += p.fi, totb += p.se;
  57. if (p.fi > j) continue;
  58. dp[i][j] = max(dp[i][j], dp[i - ][j - p.fi] + p.se);
  59. }
  60. if (totw <= j){
  61. dp[i][j] = max(dp[i][j], dp[i - ][j - totw] + totb);
  62. }
  63. }
  64. }
  65.  
  66. int ans = ;
  67. for (int i = ; i <= w; i++)
  68. ans = max(ans, dp[cnt][i]);
  69. printf("%d\n", ans);
  70. return ;
  71. }

01背包dp+并查集 Codeforces Round #383 (Div. 2)的更多相关文章

  1. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  2. 贪心+bfs 或者 并查集 Codeforces Round #268 (Div. 2) D

    http://codeforces.com/contest/469/problem/D 题目大意: 给你一个长度为n数组,给你两个集合A.B,再给你两个数字a和b.A集合中的每一个数字x都也能在a集合 ...

  3. DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

    题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...

  4. Codeforces Round #383 (Div. 2) 题解【ABCDE】

    Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...

  5. Codeforces Round #383 (Div. 2)D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(dp背包+并查集)

    题目链接 :http://codeforces.com/contest/742/problem/D 题意:给你n个女人的信息重量w和美丽度b,再给你m个关系,要求邀请的女人总重量不超过w 而且如果邀请 ...

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

  7. Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包

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

  8. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  9. Codeforces Round #383 (Div. 1)

    A: 题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x. 题解: 首先每个点应该都在一个环上,否则无解. 对 ...

随机推荐

  1. Mysql开机启动,CentOS6.5

    使用chkconfig命令,步骤如下: 将服务文件拷贝到init.d下,并重命名为mysql cp /usr/local/mysql/support-files/mysql.server /etc/i ...

  2. C#模板打印excel

    using Microsoft.Office.Interop.Excel; //引用   public void PrintPriviewExcelFile(string filePath) {    ...

  3. 定时帧:NSTimer和CADisplayLink

    学习参考了:http://www.jianshu.com/p/c35a81c3b9ebhttps://zsisme.gitbooks.io/ios-/content/chapter11/frame-t ...

  4. Provably Delay Efficient Data Retrieving in Storage Clouds---INFOCOM 2015

    [标题] [作者] [来源] [对本文评价] [why] 存在的问题 [how] [不足] assumption future work [相关方法或论文] [重点提示] [其它]

  5. C++线程安全的单例模式

    1.在GCC4.0之后的环境下: #include <iostream> using namespace std;template <typename T>class Sing ...

  6. Android编程获取手机的IMEI

    手机在生产时,每部手机均有一个唯一的标识(ID),国际上采用国际移动设备身份码(IMEI, International Mobile Equipment Identity).IMEI是由15位数字组成 ...

  7. 【转】母函数(Generating function)详解 — TankyWoo(红色字体为批注)

    母函数(Generating function)详解 - Tanky Woo 在数学中,某个序列的母函数(Generating function,又称生成函数)是一种形式幂级数,其每一项的系数可以提供 ...

  8. ActionBar之style出现Cannot resolve symbol 'Theme' 错误

    今天 2014/03/08 00:49 刚刚升级 android studio 到了 0.5.0 版本,修复了许多 bug,包含当前这个问题,之前一直困扰我很久,莫名奇妙的提示主题样式找不到,无法解析 ...

  9. python中的pip安装

    windows下安装PIP 当前环境(windows 7,python安装路径为c:\Python) 1.首先到官网下载(https://pypi.python.org/pypi/setuptools ...

  10. bootstrap复习:组件

    一.下拉菜单 1.实例:将下拉菜单触发器和下拉菜单都包裹在 .dropdown 里,或者另一个声明了 position: relative; 的元素.然后加入组成菜单的 HTML 代码.为下拉菜单的父 ...