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

时间:2023-03-09 02:26:09
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 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;
}