zoukankan      html  css  js  c++  java
  • codeforces 742D (分组背包)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses

     

    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  ≤  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 ≤ 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.

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    #define pb push_back
    using namespace std;
    typedef long long LL;
    vector<int>G[1005];
    vector<int>block[1005];
    int w[1005],b[1005],mark[1005],dp[1005],sumw[1005],sumb[1005],n,m,W;
    int max(int a,int b,int c){return max(a,max(b,c));}
    void dfs(int u,int id)
    {
        mark[u]=id;
        block[id].pb(u);
        sumw[id]+=w[u];
        sumb[id]+=b[u];
        for(int i=0;i<G[u].size();i++)
            if(!mark[G[u][i]])
                dfs(G[u][i],id);
    }
    void work(int cur)
    {
        for(int j=W;j>=0;j--)
        {
            for(int i=0;i<block[cur].size();i++)
                if(j>=w[block[cur][i]])
                    dp[j]=max(dp[j],dp[j-w[block[cur][i]]]+b[block[cur][i]]);
            if(j>=sumw[cur])
                dp[j]=max(dp[j],dp[j-sumw[cur]]+sumb[cur]);
        }
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&W);
        for(int i=1;i<=n;i++)scanf("%d",&w[i]);
        for(int i=1;i<=n;i++)scanf("%d",&b[i]);
        for(int i=0,u,v;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            G[u].pb(v);
            G[v].pb(u);
        }
        int id=0;
        for(int i=1;i<=n;i++)
            if(!mark[i])dfs(i,++id);
        for(int i=1;i<=id;i++)work(i);
        printf("%d
    ",dp[W]);
        return 0;
    }
  • 相关阅读:
    DS4700磁盘阵列的控制器微码升级操作记录(收录百度文库)
    Android 解决布局无法对齐的情况
    android 模仿大众点评团购卷列表多余3条时折叠,点击时显示剩余全部的功能
    android 解决ScrollView中的子布局不能够填充整个ScrollView的情况。
    Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。
    android RadioGroup中设置selector后出现多个别选中的RadioButton的解决办法
    Android 动态的给Button、TextView、ImageView等控件设置了background后,再设置padding属性时该属性不起作用
    Android Universal Image Loader java.io.FileNotFoundException: http:/xxx/lxx/xxxx.jpg
    Android2.3系统 自定义的PopupWindow在实例化时报空指针异常
    android精品开源项目整理
  • 原文地址:https://www.cnblogs.com/homura/p/6171343.html
Copyright © 2011-2022 走看看