洛谷P1455 搭配购买

输入输出样例

5 3 10
3 10
3 10
3 10
5 100
10 1
1 3
3 2
4 2



1


说明

30%的数据满足：n<=100

50%的数据满足：n<=1000;m<=100;w<=1000;

100%的数据满足：n<=10000;0<=m<=5000;w<=10000.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int n,m,w,fa[10010],v1[10010],c1[10010],cnt,v[10010],p[10010],f[10010];

int find(int x)
{
if (x == fa[x])
return x;
return fa[x] = find(fa[x]);
}

void hebing(int x,int y)
{
int fx = find(x),fy = find(y);
if (fx != fy)
{
fa[fx] = fy;
v1[fy] += v1[fx];
c1[fy] += c1[fx];
}
}

int main()
{
scanf("%d%d%d",&n,&m,&w);
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= n; i++)
scanf("%d%d",&v1[i],&c1[i]);
for (int i = 1; i <= m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
hebing(u,v);
}
for (int i = 1; i <= n; i++)
if (fa[i] == i)
{
v[++cnt] = v1[i];
p[cnt] = c1[i];
}
for (int i = 1; i <= cnt; i++)
for (int j = w; j >= v[i]; j--)
f[j] = max(f[j],f[j - v[i]] + p[i]);
printf("%d\n",f[w]);

return 0;
}

posted @ 2017-09-09 20:39 zbtrs 阅读(...) 评论(...) 编辑 收藏