http://codeforces.com/contest/741/problem/B

题意:有 n 个人,每个人有一个花费 w[i] 和价值 b[i],给出 m 条边,代表第 i 和 j 个人是一个集合的,给出一个最大花费 w,如果一个集合的人不能同时选的话,那么只能选集合中的其中一个或者不选,问按照这样的规则选得的花费不超过 w 的最大价值是多少。

思路:一开始搞个带权并查集出来,后来还是蒙了。之后看原来是01背包,然而已经N久没见过背包了。。。先用并查集弄出集合,然后对每个集合中的元素进行01背包(选集合中的一个或者不选的情况),在集合弄完之后还要对整体进行01背包(选集合中的全部),这里用了下滚动数组。

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
int dp[][];
int fa[];
int b[], w[];
vector<int> vec[]; int Find(int x) {
if(x == fa[x]) return x;
return fa[x] = Find(fa[x]);
} int main()
{
int n, m, x;
scanf("%d%d%d", &n, &m, &x);
for(int i = ; i <= n; i++) scanf("%d", w + i);
for(int i = ; i <= n; i++) scanf("%d", b + i);
for(int i = ; i <= n; i++) fa[i] = i;
for(int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u = Find(u), v = Find(v);
if(u != v) fa[u] = v;
}
for(int i = ; i <= n; i++)
vec[Find(i)].push_back(i);
int now = ;
for(int i = ; i <= n; i++) {
now = !now;
int ww = , bb = ;
for(int j = ; j <= x; j++) dp[now][j] = dp[!now][j];
for(int j = ; j < vec[i].size(); j++) {
int id = vec[i][j];
int www = w[id], bbb = b[id];
ww += www, bb += bbb;
for(int j = www; j <= x; j++) { // 在当前集合选一个最优
dp[now][j] = max(dp[now][j], dp[!now][j-www] + bbb);
}
}
for(int j = ww; j <= x; j++) { // 如果全部能丢进去
dp[now][j] = max(dp[now][j], dp[!now][j-ww] + bb);
}
}
int ans = ;
for(int i = ; i <= x; i++) ans = max(ans, dp[now][i]);
printf("%d\n", ans);
return ;
}

Codeforces 741B:Arpa's weak amphitheater and Mehrdad's valuable Hoses(01背包+并查集)的更多相关文章

  1. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses

    [题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...

  2. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)

    <题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...

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

  4. D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题

    http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...

  5. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

  6. Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)

    题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...

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

  8. Arpa's weak amphitheater and Mehrdad's valuable Hoses

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

  9. B. Arpa's weak amphitheater and Mehrdad's valuable Hoses

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

随机推荐

  1. Token的管理

    代码: RedisCommon using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  2. 第二篇 Replication:分发服务器的作用

    本篇文章是SQL Server Replication系列的第二篇,详细内容请参考原文. 分发服务器是SQL Server复制的核心组件.分发服务器控制并执行数据从一个服务器移动到另一个服务器的进程. ...

  3. javaScirpt学习之事件

    一.事件的添加和删除 不同浏览器,不同DOM级别,添加和删除事件的方法也不同,具体可以参见后面的EventUtil元素. 二.事件对象 在事件处理程序中,浏览器会为之传入一个event对象,该对象的常 ...

  4. iOS 瀑布流的基本原理

    /** * 源代码链接 * 链接: https://pan.baidu.com/s/1nvLamEX 密码: kya5 */ #import <UIKit/UIKit.h> @interf ...

  5. Nginx实现多重IF判断的办法

    在YII框架中如果访问的图片不存在,会记录大量的错误,于是我想了个办法,凡是访问不存在的图片,直接返回404,不经过YII框架 location / {  set $if_img N;  if ($r ...

  6. 详解 ASP.NET并行,异步,多线程

    最近在学习.net4以上版本新特性的时候,发现在异步这方面提供了很多好玩的.以下Mark 2篇好文,温故而知新! http://www.cnblogs.com/wisdomqq/archive/201 ...

  7. 证明 logX < X 对所有 X > 0 成立

    题目取自:<数据结构与算法分析:C语言描述_原书第二版>——Mark Allen Weiss       练习1.5(a)  证明下列公式: logX < X 对所有 X > ...

  8. C++builder 图像字符流的存储和加载

    __fastcall TForm6::TForm6(TComponent* Owner) : TForm(Owner) { #if 1 //for debug AllocConsole(); Atta ...

  9. webDriver中的alert

    driver.switchTo().alert();这句可以得到alert\confirm\prompt对话框的对象,然后运用其方法对它进行操作.对话框操作的主要方法有:   getText()    ...

  10. 【cube】SSAS(分析服务)优化手册

    优化前注意事项 1.数据质量.数据仓库中维度表的数据质量要严格保证.比如店仓的大区.省区.管理城市等属性和店仓代码没有一对多的上下级关系,这样在设计店仓维度时,就无法建立有效的层次结构.这会拖慢查询时 ...