背包问题(分组背包)

D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
time limit per 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 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 nm and w (1  ≤  n  ≤  10001 ≤ 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 ≤ nxi ≠ yi), meaning that Hoses xiand 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.


解决这个问题之前先让我们了解一下分组背包问题:

有N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。这些物品被划分为若干组,每组中的物品互相冲突,最多选一件。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。 
算法 
这个问题变成了每组物品有若干种策略:是选择本组的某一件,还是一件都不选。也就是说设f[k][v]表示前k组物品花费费用v能取得的最大权值,则有f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]|物品i属于第k组}。 
使用一维数组的伪代码如下: 

for 所有的组k 

for v=V..0 
for 所有的i属于组k 

f[v]=max{f[v],f[v-c[i]]+w[i]}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define rep(i,a,n) for(int i=a;i<n;++i)
#define per(i,a,n) for(int i=n-1;i>=a;--i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mem(a,t) memset(a,t,sizeof(a))
#define all(v) v.begin(),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){   //把属于同一组的物品重新标号
        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){  // 对于有多个物品的组,增添一个所有物品都要选的“大物品”
        f1=findd(i);
        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){   //分组背包算法
        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;
}


猜你喜欢

转载自blog.csdn.net/u011721440/article/details/53610605