背包问题?!+并查集
一组朋友之中最多只能有一个人去,所以需要在写0-1背包时需要调整for循环的顺序
错解:
for(int i=1;i<=n;i++)
{
sum_b=0,sum_w=0;
if(pre[i]==i)
{
for(int j=0;j<vn[i].size();j++)
{
sum_b+=bn[vn[i][j]];
sum_w+=wn[vn[i][j]];
for(int k=w;k>=wn[vn[i][j]];k--)
{
dp[k]=max(dp[k],dp[k-wn[vn[i][j]]]+bn[vn[i][j]]);
}
}
for(int k=w;k>=sum_w;k--)
{
dp[k]=max(dp[k],dp[k-sum_w]+sum_b);
}
}
}
不能保证一组朋友中最多只有一个人去
正解:
for(int i=1;i<=n;i++)
{
if(pre[i]==i)
{
for(int j=w;j>=0;j--)
{
sum_b=0,sum_w=0;
for(int k=0;k<vn[i].size();k++)
{
sum_b+=bn[vn[i][k]],sum_w+=wn[vn[i][k]];
if(j>=wn[vn[i][k]])
dp[j]=max(dp[j],dp[j-wn[vn[i][k]]]+bn[vn[i][k]]);
}
if(j>=sum_w)
dp[j]=max(dp[j],dp[j-sum_w]+sum_b);
}
}
}
调整了里面的for循环的嵌套顺序
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.
#include<stdio.h>
#include<algorithm>
#include<vector>
#define maxn 1005
using namespace std;
int wn[maxn],bn[maxn],pre[maxn];
vector<int> vn[maxn];
int find(int a)
{
return a==pre[a]?a:pre[a]=find(pre[a]);
}
void merge(int a,int b)
{
int fa,fb;
fa=find(a);
fb=find(b);
if(fa!=fb)
{
pre[fa]=fb;
}
}
int main()
{
int n,m,w,dp[maxn]={0},sum_b,sum_w;
scanf("%d%d%d",&n,&m,&w);
for(int i=1;i<=n;i++)
{
scanf("%d",&wn[i]);
pre[i]=i;
}
for(int i=1;i<=n;i++)
scanf("%d",&bn[i]);
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
merge(a,b);
}
for(int i=1;i<=n;i++)
vn[find(i)].push_back(i);
//0-1背包
for(int i=1;i<=n;i++)
{
if(pre[i]==i)
{
for(int j=w;j>=0;j--)
{
sum_b=0,sum_w=0;
for(int k=0;k<vn[i].size();k++)
{
sum_b+=bn[vn[i][k]],sum_w+=wn[vn[i][k]];
if(j>=wn[vn[i][k]])
dp[j]=max(dp[j],dp[j-wn[vn[i][k]]]+bn[vn[i][k]]);
}
if(j>=sum_w)
dp[j]=max(dp[j],dp[j-sum_w]+sum_b);
}
}
}
printf("%d",dp[w]);
return 0;
}