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. Android --固定底部

    相对布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  2. 8、JavaScript深入浅出——数据类型

    一.六种数据类型 Javascript是弱类型. 五种原始类型和一种对象类型: number String boolean null undefined Object 二.隐式转换 +与-的运算举例: ...

  3. 6、JavaScript进阶篇③——浏览器对象、Dom对象

    一.浏览器对象 1. window对象 window对象是BOM的核心,window对象指当前的浏览器窗口. window对象方法: 注意:在JavaScript基础篇中,已讲解了部分属性,windo ...

  4. css背景图片拉伸 以及100% 满屏显示

    如何用css背景图片拉伸 以及100% 满屏显示呢?这个问题听起来似乎很简单.但是很遗憾的告诉大家.不是我们想的那么简单. 比如一个容器(body,div,span)中设定一个背景.这个背景的长宽值在 ...

  5. redsocks 配合iptables设置全局sockts5代理

    参照:http://kuaile.in/archives/1370 架构图: 第一步,安装redsocks 1. 安装依赖 yum install libevent-devel 2. 下载编译 git ...

  6. fail2ban使用

    转子: http://www.2cto.com/Article/201406/310910.html 1.fail2ban简介: fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹 ...

  7. WPF 面试题及答案(三)

    一 · 路由事件的三种方式/策略(冒泡 直接 隧道) WPF中的路由事件是沿着VisualTree传递的,作用是用来调用应用程序的元素树上的各种监听器上的处理程序. (1)冒泡,这种事件处理方式是从源 ...

  8. 异步调用webservice

    一.异步调用 asynchronous call(异步调用):一个可以无需等待被调用函数的返回值就让操作继续进行的方法 举例: 异步调用就是你 喊 你朋友吃饭 ,你朋友说知道了 ,待会忙完去找你 ,你 ...

  9. Java线程总结

    在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 对于直接继承Thread的类来说,代码大致框架是: class 类名 extends Thread ...

  10. URAL 1416 Confidential(次小生成树)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1416 Zaphod Beeblebrox — President of the Impe ...