D. Arpa ' s weak amphitheater and Mehrdad ' valuable hoses time limit/test 1 second memory limit per test 256 megabytes Input standard input Output standard 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 are a sequence of hoses A1, A2, ..., AK such that a I and AI + 1 are friends for each 1≤i < K, and a1 = x and ak = y.
Arpa allowed to the "amphitheater of Palace to Mehrdad for the". Arpa ' s amphitheater can hold at most w weight on it.
Mehrdad is so greedy this he wants to invite some hoses such this sum of their weights is not greater than W and sum of th Eir Beauties is as large as possible. Along with this, from the friendship group he can either invite all hoses, or no more than one. Otherwise, some hoses would be hurt. Find for Mehrdad The maximum possible total beauty of hoses him can invite so no one gets hurt and the total weight do ESN ' t exceed W. Input
The contains integers n, M and W (1≤n≤1000, 1≤w≤1000)-the number of hoses, the number of pair O F Friends and the maximum total weight to 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), mea Ning that hoses Xiand Yi are friends. Note This friendship is bidirectional. All pairs (xi, Yi) are distinct. Output
Print The maximum possible total beauty of hoses Mehrdad can invite then no one gets hurt and the total weight ' t exceed W. Examples input
3 1 5
3 2 5 2 4 2 1 2
Output
6
Input
4 2 2 4 6 6 6 4 2 1 1 2 2-
3
Output
7
Note
In the There are two friendship groups:hoses {1, 2} and Hos {3}. The best way is to choose all of hoses in the, sum of their weights are 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 of the hoses from the ' the one because their total weight are >, thus the best way is to Choose the "Hos" from the "the" and "only one" to the second group. The total weight is 8, and the total beauty would be 7.
Before we solve this problem, let's take a look at the packet knapsack problem:
There are n items and a backpack with a capacity of V. The cost of item I is c[i], value is w[i]. These items are divided into groups, and items in each group conflict with each other, with a maximum of one selected. To find out what items to pack into the backpack can make the sum of these items does not exceed the backpack capacity, and the total value of the largest.
Algorithm
The problem has become a number of strategies for each group: selecting one of the groups or not. That is, set F[K][V] represents the first K group Items cost v can obtain the maximum weight, then there are f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]| items I belong to the K group.
Use the pseudo code of a one-dimensional array as follows:
For all Groups K
For V=v.. 0
For all I belong to Group K
F[v]=max{f[v],f[v-c[i]]+w[i]}
#include <bits/stdc++.h> using namespace std; #define LL Long #define REP (i,a,n) for (int i=a;i<n;++i) #define/(I,a,n) for (int i=n-1;i>=a;--i) #define MP M Ake_pair #define PB push_back #define FI #define SE second #define MEM (a,t) memset (A,t,sizeof (a)) #define ALL (v) v.b
Egin (), V.end () #define SZ (v) ((int) sizeof (v)) #define N 1100 const int inf=2e+9;
int w[n],b[n];
int pre[n],vis[n];
Vector<pair<int,int> >f[N];
int dp[n];
vector<int>g[n];
int findd (int k) {if (K!=pre[k]) pre[k]=findd (pre[k));
return pre[k];
int main () {int n,w,m;
cin>>n>>m>>w;
Rep (i,0,n) {scanf ("%d", &w[i]);
} rep (i,0,n) {scanf ("%d", &b[i]);
} Rep (i,0,n) pre[i]=i;
int u,v,cnt,f1,f2;
Rep (i,0,m) {scanf ("%d%d", &u,&v);
--u;--v;
F1=FINDD (U);
F2=findd (v);
if (F1!=F2) pre[f1]=f2;
} mem (vis,-1);
cnt=0; Rep (i,0,n) {//Re-label items belonging to the same group F1=FINDD (i);
if (vis[f1]==-1) {vis[f1]=cnt++;
} F[VIS[F1]].PB (MP (W[i],b[i));
} rep (i,0,cnt) {if (F[i].size () >1) F[I].PB (MP (0,0));
} rep (I,0,n) {//For groups with multiple items, add a "Big item" F1=FINDD (i) for all items to be selected;
int j=f[vis[f1]].size ();
if (j==1) continue;
F[vis[f1]][j-1].fi+=w[i];
F[vis[f1]][j-1].se+=b[i];
int k,ans=0;
Rep (i,0,cnt) {//packet knapsack algorithm int len=f[i].size (); for (K=w;k>=0;--k) {rep (J,0,len) {if (k>=f[i][j].fi) {Dp[k]=max (dp[k),
dp[k-f[i][j].fi]+f[i][j].se);
Ans=max (Ans,dp[k]);
}}} cout<<ans<<endl;
return 0; }