Codeforces724D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集+背包)

来源:互联网 发布:中兴证券软件下载 编辑:程序博客网 时间:2024/05/16 10:33

D. Arpa’s weak amphitheater and Mehrdad’s valuable Hoses
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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

#include<cstdio>#include<iostream>#include<string.h>#include<stdlib.h>#include<algorithm>#include<math.h>#include<vector>using namespace std;const int maxn=1005;int dp[maxn];int c[maxn];int w[maxn];int fa[maxn];vector<int> g[maxn];int getfa(int x){    if(fa[x]==x)  return x;    else  return  fa[x]=getfa(fa[x]);}void merge(int x,int y){    int fax=getfa(x),fay=getfa(y);    if(fax!=fay)        fa[fax]=fay;}int main(){    int n,m,V;    scanf("%d%d%d",&n,&m,&V);;    memset(dp,0,sizeof(dp));    //init    for(int i=1;i<=maxn;i++)        fa[i]=i;    for(int i=1;i<=n;i++)        scanf("%d",&c[i]);    for(int i=1;i<=n;i++)        scanf("%d",&w[i]);    for(int i=1;i<=m;i++){        int x,y;        scanf("%d%d",&x,&y);        merge(x,y);    }    // 分组     for(int i=1;i<=n;i++)        g[getfa(i)].push_back(i);    for(int i=1;i<=n;i++){        if(fa[i]==i){            for(int j=V;j>=0;j--){                int sumc=0,sumw=0;                for(int k=0;k<g[i].size();k++){                    sumc += c[g[i][k]]; //第i组第 k个成员的cost                     sumw += w[g[i][k]];      //第i组第 k个成员的worth                     // 单个物品                     if(j>=c[g[i][k]])                        dp[j]=max(dp[j],dp[j-c[g[i][k]]]+w[g[i][k]] );                }                //多个物品                if(j >=sumc)                    dp[j]=max(dp[j],dp[j-sumc]+ sumw);                }        }    }    cout<<dp[V]<<endl;  }
0 0
原创粉丝点击