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

Problem Description:

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.

Input:

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 xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.

Output:

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.

Sample Input:

4 2 11

2 4 6 6

6 4 2 1

1 2

2 3

Sample Output:

7

这是一个分组背包的问题,仔细想想其实也不难 但一定要用二维dp数组才能写出来!

【题目链接】D. Arpa's weak amphitheater and Mehrdad's valuable Hoses

【题目类型】分组背包+dsu

&题意:

你有\(n\)个\(Hos\)要邀请,每个\(Hos\)都有2个属性,\(w_i\)和\(b_i\)并且这些\(Hos\)会组成一些朋友组,对于每个朋友组,你只能全部选择,或者只选一个,问不超过\(W\)时,最大的\(b\)能是多少?

&题解:

这是分组背包,这题的思路就是优先选择全部的那个,之后去判断,是否是一个朋友组有多个人?

如果是,那么继续细分dp,这时的dp就代表只选一个的情况.

如果不是,那就和01背包一样了

【时间复杂度】\(O(n^2)\)

&代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
#define PI(A) cout<<(A)<<endl;
const int maxn = (int)1e3 + 9;
#define fastio ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); int w[maxn],b[maxn],ww[maxn],bb[maxn],n,m,W;
int dp[maxn][maxn];
bool vis[maxn]; int dsu[maxn];
int FIND(int x) {return x==dsu[x]?x:dsu[x]=FIND(dsu[x]);}
int UNION(int x,int y) {dsu[FIND(x)]=FIND(y);} int main() {
while(cin>>n>>m>>W) {
rep(i,1,n) cin>>w[i];
rep(i,1,n) cin>>b[i];
rep(i,1,n) dsu[i]=i;
while(m--) {
int t1,t2;
cin>>t1>>t2;
UNION(t1,t2);
}
rep(i,1,n) {
int pp=FIND(i);
ww[pp]+=w[i];
bb[pp]+=b[i];
}
int cc=1;
cle(vis,0);
rep(i,1,n) {
int pp=FIND(i);
if (vis[pp]) continue;
vis[pp]=true;
rep(j,1,W) {
dp[cc][j]=dp[cc-1][j];
if (ww[pp]<=j)
dp[cc][j]=max(dp[cc][j],dp[cc-1][j-ww[pp]]+bb[pp]);
}
rep(t,1,n) if(FIND(t)==pp) {
rep(j,1,W) {
if (w[t]<=j)
dp[cc][j]=max(dp[cc][j],dp[cc-1][j-w[t]]+b[t]);
}
}
cc++;
}
int ans=0;
for(int i=0; i<=W; i++) ans=max(ans,dp[cc-1][i]);
PI(ans)
}
return 0;
}

Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)的更多相关文章

  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 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 而且如果邀请 ...

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

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

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

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

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

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

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

  9. Codeforces Round #383 (Div. 2) 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 ...

随机推荐

  1. android view:布局优化

    今天在图书馆看了一个android性能优化. 关于布局优化有几个小技巧: 1.尽量减少布局的嵌套,而使用相对布局,这样的话会减少布局对象的创建,并且可以再事件传递的时候减少传递嵌套. 2.使用incl ...

  2. 更新系统没有mac dashboard 问题解决

    今天更新了mac的系统到EL Capitan,结果出来以后发现平时经常使用的mac dashboard没了,就是这玩意: 找了半天方法,终于知道了这个叫“dashboard” (md之前完全不知道,无 ...

  3. 二叉树[C实现]

    #include<stdio.h> #include<malloc.h> #include<iostream> //定义节点 typedef struct BiNo ...

  4. 小米note3,华为手机,软键盘弹出之后,页面上定位的元素布局会乱掉

    原因:可能是因为,软键盘弹出时,改变了height,使height:100%,不能达到原来的高度. 解决办法: $(document).ready(function () { $('body').he ...

  5. javascript学习笔记全记录

          js的初步了解     1.就是用来修改样式的,修改的是行内样式.任何样式都能够修改.     2.css里面怎么写js就怎么写.     3.任何元素都能加事件:事件都要小写 js的三大 ...

  6. Ftp类

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...

  7. jquery实现AJAX的4种方法

    当我们用javascript写ajax程序写得很“开心”的时候,突然有人告诉你有一种东西叫jquery,它会告诉你不直接和 HttpRequest是多么的快乐,同时你再也不需要再烦恼纠结的ajax乱码 ...

  8. mysql 根据某字段特定值排序

    比如: 表 :user 字段:orders (值为 1,2,3) 要求根据字段  orders 按2 -> 1 -> 3 排序 使用以下语句实现SELECT *FROM userORDER ...

  9. S3C2416 看门狗

    原理:看门狗自己有个硬件计数器,看门狗开启后,计数器就开始计数,当计数为0时触发,触发事件有两个:系统复位和中断,可设置屏蔽. 在计数器计数到0之前,程序可以重新设置计数器中的数值,称之喟狗.计数器的 ...

  10. 使用TCP的HelloServer

    HelloServer是一个在1234端口监听的服务端程序,它接受客户送来的数据,并且把这些数据解释为响应的ASCII字符,再对客户做出类似“Hello,...!"这样的响应.以下是Hell ...