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.
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.
Examples
input
3 1 5
3 2 5
2 4 2
1 2
output
6
input
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
output
7
Note
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.
这个题其实就是个并查集+01背包
先用并查集把所有的集合给分出来,然后把和也放在集合里
一开始我用的dfs对每一种状态进行了01背包
超时了…
然后想到对于每一个集合对每个集合里的所有成员进行01背包
这个不能用滚动的…不然同一个集合里面会计算多次没法区分
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<memory.h>
#include<map>
using namespace std;
int n, m, w;
long long ww[100000], bb[100001], ba[100001], jihew[100001], jiheb[100001], lins[1001];
int zhao(int x)
{
while (x != ba[x])x = ba[x];
return x;
}
long long biaotou[1001][1001], zzz[1001][1001], bbb[1001][1001];
map<int, int>se;
long long dp[1001][1005];
long long dada = 0;
int main()
{
#define int long long
cin >> n >> m >> w;
for (int a = 1;a <= n;a++)scanf("%I64d", &ww[a]), ba[a] = a;
for (int a = 1;a <= n;a++)scanf("%I64d", &bb[a]);
int q, uuuu;
for (int a = 1;a <= m;a++)
{
scanf("%I64d%I64d", &q, &uuuu);
ba[zhao(q)] = zhao(uuuu);
}
for (int a = 1;a <= n;a++)
{
int yyy = zhao(a);
if (!se[yyy])se[yyy] = se.size();
int u = se[yyy];
biaotou[u][++biaotou[u][0]] = a;
zzz[u][++zzz[u][0]] = ww[a];
bbb[u][++bbb[u][0]] = bb[a];
jihew[u] += ww[a];
jiheb[u] += bb[a];
}
int qqqq = se.size();
for (int a = 1;a <= qqqq;a++)
{
zzz[a][++zzz[a][0]] = jihew[a];
bbb[a][++bbb[a][0]] = jiheb[a];
}
for (int a = 1;a <= qqqq;a++)
{
for (int b = 0;b <= w;b++)dp[a][b] = dp[a - 1][b];
for (int b = 1;b <= zzz[a][0];b++)
{
for (int c = w;c >= 0;c--)
{
if (c - zzz[a][b] < 0)break;
dp[a][c] = max(dp[a - 1][c - zzz[a][b]] + bbb[a][b], dp[a][c]);
dada = max(dada, dp[a][c]);
}
}
}
cout << dada << endl;
return 0;
}

本文介绍了一个结合并查集与01背包算法的问题解决方案。通过并查集划分不同的友谊组,并针对每个组应用01背包算法,以在重量限制下最大化总美丽值。文章提供了一个具体的实现示例。
602

被折叠的 条评论
为什么被折叠?



