zoukankan      html  css  js  c++  java
  • B. Arpa's weak amphitheater and Mehrdad's valuable Hoses

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

    思路:并查集+背包;

      1 #include<stdio.h>
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<string.h>
      5 #include<math.h>
      6 #include<queue>
      7 #include<stdlib.h>
      8 #include<set>
      9 #include<vector>
     10 
     11 typedef long long  LL;
     12 using namespace std;
     13 vector<int>vec[1005];
     14 int bin[1005];
     15 int du[1005];
     16 int ans[1005];
     17 int bns[1005];
     18 int aa[1005];
     19 int bb[1005];
     20 bool flag[1005];
     21 int dp[1005][2000];
     22 int main(void)
     23 {
     24     int n,m,k;
     25     while(scanf("%d %d %d",&n,&m,&k)!=EOF)
     26     {
     27         int i,j;
     28         memset(dp,0,sizeof(dp));
     29         memset(flag,0,sizeof(flag));
     30         for(i = 0; i<=1000; i++)
     31         {
     32             vec[i].clear();
     33             du[i] = 1;
     34             bin[i] = i;
     35         }
     36         for(i = 1; i <= n; i++)
     37         {
     38             scanf("%d",&ans[i]);
     39             aa[i] = ans[i];
     40         }
     41         for(i = 1; i <= n; i++)
     42         {
     43             scanf("%d",&bns[i]);
     44             bb[i] = bns[i];
     45         }
     46         while(m--)
     47         {
     48             int x,y;
     49             scanf("%d %d",&x,&y);
     50             int xx,yy;
     51             for(xx = x; xx!=bin[xx];)
     52                 xx = bin[xx];
     53             for(yy = y; yy!=bin[yy];)
     54                 yy =bin[yy];
     55             if(xx !=yy)
     56             {
     57                 if(du[xx] > du[yy])
     58                 {
     59                     du[xx]+=du[yy];
     60                     bin[yy] = xx;
     61                 }
     62                 else
     63                 {
     64                     du[yy]+=du[xx];
     65                     bin[xx] = yy;
     66                 }
     67             }
     68         }
     69         for(i = 1; i <= n; i++)
     70         {
     71             int xx ;
     72             for(xx = i ; xx!=bin[xx];)
     73                 xx = bin[xx];
     74             if(xx!=i)
     75             {
     76                 flag[i] = true;
     77                 ans[xx]+=ans[i];
     78                 bns[xx]+=bns[i];
     79                 vec[xx].push_back(i);
     80             }
     81             vec[i].push_back(i);
     82 
     83         }
     84         for(i = 1; i <= n; i++)
     85         {
     86             if(!flag[i])
     87             {
     88                 for(j = 0; j <=k; j++)
     89                 {
     90                     dp[i][j] = dp[i-1][j];
     91                 }
     92                 for(j = ans[i];j <= k;j++)
     93                 {
     94                     dp[i][j] = max(dp[i][j],dp[i-1][j-ans[i]]+bns[i]);
     95                 }
     96                 for(j = 0;j < vec[i].size();j++)
     97                 {
     98                     int s;int kk = vec[i][j];
     99                     for(s = 0;s <=k;s++)
    100                     {
    101                         dp[i][s] = max(dp[i][s],dp[i-1][s]);
    102                     }
    103                     for(s = aa[kk];s <= k;s++)
    104                     {
    105                         dp[i][s] = max(dp[i][s],dp[i-1][s-aa[kk]]+bb[kk]);
    106                     }
    107                 }
    108             }
    109             else
    110             {
    111                 for(j = 0;j <= k;j++)
    112                     dp[i][j] = dp[i-1][j];
    113             }
    114         }
    115 
    116         printf("%d
    ",dp[n][k]);
    117     }
    118     return 0;
    119 }
  • 相关阅读:
    [HNOI2007]最小矩形覆盖
    [HAOI2008]下落的圆盘
    JSON相关 JSON在线解析 JSON压缩转义工具 JSON着色工具 JSON 在线格式化工具 在线XML/JSON互相转换工具 XML、JSON在线转换
    速度竟差9倍!6款32GB USB3.0优盘横评
    Linux 中用 dd 命令来测试硬盘读写速度
    CrystalDiskMark v7.0.0h中文版
    个人觉得,不单是教育缺失的问题,贫穷才是真像。贫穷分
    就算是3.0的U盘,写入速度10M及以下也是正常的,U盘用很差的闪存颗粒的话就算10Gbps的USB3.1也是很慢的。
    USB历代标准及接口发展
    测试 USB 存储设备读写性能(Mb/s),平均读写速度等
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/6140811.html
Copyright © 2011-2022 走看看