Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)
题目链接:http://codeforces.com/contest/742/problem/D
1 second
256 megabytes
standard input
standard output
Just to remind, girls in Arpa's land are really nice.
Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and
some beauty bi.
Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are
in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such
that ai and ai + 1 are
friends for each 1 ≤ i < k, and a1 = x and ak = y.
Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.
Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties
is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt
and the total weight doesn't exceed w.
The first line contains integers n, m and w (1 ≤ n ≤ 1000, , 1 ≤ w ≤ 1000) —
the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) —
the weights of the Hoses.
The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) —
the beauties of the Hoses.
The next m lines contain pairs of friends, the i-th
of them contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi),
meaning that Hoses xiand yi are
friends. Note that friendship is bidirectional. All pairs (xi, yi) are
distinct.
Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.
3 1 5
3 2 5
2 4 2
1 2
6
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
7
In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.
In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
题解:
1.通过并查集,得出每一组有哪些人,并且将这些人的信息重新归类。
2.对于每一组,要么全选,要么只选一人,要么都不选。那么将全选的又看成一个“人 ”,并放入这个集合中,所以就变成了01背包了。
3.代码中,cnt为集合的个数,c[i]为集合i的元素个数,x[i][j]、y[i][j]为i集合中第j个元素的体重和颜值。
类似的题:http://blog.csdn.net/dolfamingo/article/details/73438052
写法一:
#include<bits/stdc++.h>
//#define LOCAL
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e3+; int n, m, w, a[maxn], b[maxn];
int fa[maxn], x[maxn][maxn], y[maxn][maxn], c[maxn], cnt, dp[maxn][maxn], vis[maxn]; int find(int x) { return (x==fa[x])?x:x=find(fa[x]); } void init()
{
scanf("%d%d%d",&n,&m,&w);
for(int i = ; i<=n; i++)
scanf("%d",&a[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++)
{
int f = find(i);
if(!vis[f]) vis[f] = ++cnt;
f = vis[f]; //将f赋值为集合的序号
++c[f]; //集合内元素的个数
x[f][c[f]] = a[i];
y[f][c[f]] = b[i];
} for(int i = ; i<=cnt; i++) //全部都取,相当于每个集合增加一个元素
{
int X = , Y = ;
for(int j = ; j<=c[i]; j++)
{
X += x[i][j];
Y += y[i][j];
}
++c[i];
x[i][c[i]] = X;
y[i][c[i]] = Y;
}
} void solve()
{
for(int i = ; i<=cnt; i++)
for(int j = ; j<=w; j++)
{
for(int k = ; k<=c[i]; k++) //Remember!!!
dp[i][j] = dp[i-][j]; for(int k = ; k<=c[i]; k++)
if(j>=x[i][k])
dp[i][j] = max(dp[i][j], dp[i-][j-x[i][k]]+y[i][k]);
} cout<< dp[cnt][w]<<endl;
} int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
init();
solve();
}
写法二:
#include<bits/stdc++.h>
//#define LOCAL
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e3+; int n, m, w, a[maxn], b[maxn];
int fa[maxn], x[maxn][maxn], y[maxn][maxn], c[maxn], cnt, dp[maxn], vis[maxn]; int find(int x) { return (x==fa[x])?x:x=find(fa[x]); } void init()
{
scanf("%d%d%d",&n,&m,&w);
for(int i = ; i<=n; i++)
scanf("%d",&a[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++)
{
int f = find(i);
if(!vis[f]) vis[f] = ++cnt;
f = vis[f]; //将f赋值为集合的序号
++c[f]; //集合内元素的个数
x[f][c[f]] = a[i];
y[f][c[f]] = b[i];
} for(int i = ; i<=cnt; i++) //全部都取,相当于每个集合增加一个元素
{
int X = , Y = ;
for(int j = ; j<=c[i]; j++)
{
X += x[i][j];
Y += y[i][j];
}
++c[i];
x[i][c[i]] = X;
y[i][c[i]] = Y;
}
} void solve()
{
for(int i = ; i<=cnt; i++) //01背包
for(int j = w; j>=; j--)
for(int k = ; k<=c[i]; k++)
if(j>=x[i][k])
dp[j] = max(dp[j], dp[j-x[i][k]]+y[i][k]); cout<< dp[w]<<endl;
} int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
init();
solve();
}
Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)的更多相关文章
- 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 ...
- 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 而且如果邀请 ...
- Codeforces 741B:Arpa's weak amphitheater and Mehrdad's valuable Hoses(01背包+并查集)
http://codeforces.com/contest/741/problem/B 题意:有 n 个人,每个人有一个花费 w[i] 和价值 b[i],给出 m 条边,代表第 i 和 j 个人是一个 ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
- 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 ...
- 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 ...
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan
C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...
- Codeforces Round #383 (Div. 2) B. 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 so ...
随机推荐
- ros pub sub ("~")
base_velocity_smoother.cpp remap ("~") 订阅: "cmd_vel" ...
- numpy常用函数学习
目录numpy常用函数学习点乘法线型预测线性拟合裁剪.压缩和累乘相关性多项式拟合提取符号数组杂项点乘法该方法为数学方法,但是在numpy使用的时候略坑.numpy的点乘为a.dot(b)或numpy. ...
- DBCC
http://www.cnblogs.com/lyhabc/archive/2013/01/19/2867174.html http://www.cnblogs.com/lyhabc/articles ...
- UITableView 滚动时使用reloaddata出现 crash'-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (0)' Crash
例子: - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)in ...
- 深入理解Activity启动流程(四)–Activity Task的调度算法
本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--Activity启动的概要流程 深入理解Activity启动流程(二)- ...
- 使用zerorpc踩的第一个坑:
Server端代码:注意s.run() 和 s.run的区别,一个括号搞死我了.如果不加括号,服务端服务是不会启动的,客户端就会报连接超时的错误 Server端在本机所有IP上监听4242端口的tcp ...
- Neural Networks and Deep Learning学习笔记ch1 - 神经网络
近期開始看一些深度学习的资料.想学习一下深度学习的基础知识.找到了一个比較好的tutorial,Neural Networks and Deep Learning,认真看完了之后觉得收获还是非常多的. ...
- VC++中MCI播放音频文件 【转】
MCI播放mp3音频文件例程 源文件中需要包含头文件 Mmsystem.h,在Project->Settings->Link->Object/libray module中加入库 Wi ...
- 【翻译自mos文章】检查$ORACLE_HOME是否是RAC的HOME的方法以及relink RAC的Oracle binary的方法
检查$ORACLE_HOME是否是RAC的HOME的方法以及relink RAC的Oracle binary的方法 来源于: How to Check Whether Oracle Binary/In ...
- 没有IP地址的主机怎样保持IP层联通
在<两台不同网段的PC直连能否够相互ping通>一文中,我有点像在玩旁门左道,本文中.我继续走火入魔.两台机器,M1和M2,各自有一个网卡eth0,配置例如以下:M1的配置:eth0上不配 ...