Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

题目链接:http://codeforces.com/contest/742/problem/D


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.




题解:

1.通过并查集,得出每一组有哪些人,并且将这些人的信息重新归类。

2.对于每一组,要么全选,要么只选一人,要么都不选。那么将全选的又看成一个“人 ”,并放入这个集合中,所以就变成了01背包了。

3.代码中,cnt为集合的个数,c[i]为集合i的元素个数,x[i][j]、y[i][j]为i集合中第j个元素的体重和颜值。

类似的题:http://blog.csdn.net/dolfamingo/article/details/73438052




写法一:

#include<bits/stdc++.h>
//#define LOCAL
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e3+10;

int n, m, w, a[maxn], b[maxn];
int fa[maxn], x[maxn][maxn], y[maxn][maxn], c[maxn], cnt, dp[maxn][maxn], vis[maxn];

int find(int x) { return (x==fa[x])?x:x=find(fa[x]); }

void init()
{
    scanf("%d%d%d",&n,&m,&w);
    for(int i = 1; i<=n; i++)
        scanf("%d",&a[i]);
    for(int i = 1; i<=n; i++)
        scanf("%d",&b[i]);
    for(int i = 1; i<=n; i++)
        fa[i] = i;

    for(int i = 1; i<=m; i++)
    {
        int u, v;
        scanf("%d%d",&u, &v);
        u = find(u);
        v = find(v);
        if(u!=v)
            fa[u] = v;
    }

    for(int i = 1; i<=n; i++)
    {
        int f = find(i);
        if(!vis[f]) vis[f] = ++cnt;
        f = vis[f]; //将f赋值为集合的序号
        ++c[f]; //集合内元素的个数
        x[f][c[f]] = a[i];
        y[f][c[f]] = b[i];
    }

    for(int i = 1; i<=cnt; i++) //全部都取,相当于每个集合增加一个元素
    {
        int X = 0, Y = 0;
        for(int j = 1; j<=c[i]; j++)
        {
            X += x[i][j];
            Y += y[i][j];
        }
        ++c[i];
        x[i][c[i]] = X;
        y[i][c[i]] = Y;
    }
}

void solve()
{
    for(int i = 1; i<=cnt; i++)
    for(int j = 1; j<=w; j++)
    {
        for(int k = 1; k<=c[i]; k++)    //Remember!!!
            dp[i][j] = dp[i-1][j];

        for(int k = 1; k<=c[i]; k++)
        if(j>=x[i][k])
            dp[i][j] = max(dp[i][j], dp[i-1][j-x[i][k]]+y[i][k]);
    }

    cout<< dp[cnt][w]<<endl;
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
//      freopen("output.txt", "w", stdout);
#endif
    init();
    solve();
}



写法二:

#include<bits/stdc++.h>
//#define LOCAL
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e3+10;

int n, m, w, a[maxn], b[maxn];
int fa[maxn], x[maxn][maxn], y[maxn][maxn], c[maxn], cnt, dp[maxn], vis[maxn];

int find(int x) { return (x==fa[x])?x:x=find(fa[x]); }

void init()
{
    scanf("%d%d%d",&n,&m,&w);
    for(int i = 1; i<=n; i++)
        scanf("%d",&a[i]);
    for(int i = 1; i<=n; i++)
        scanf("%d",&b[i]);
    for(int i = 1; i<=n; i++)
        fa[i] = i;

    for(int i = 1; i<=m; i++)
    {
        int u, v;
        scanf("%d%d",&u, &v);
        u = find(u);
        v = find(v);
        if(u!=v)
            fa[u] = v;
    }

    for(int i = 1; i<=n; i++)
    {
        int f = find(i);
        if(!vis[f]) vis[f] = ++cnt;
        f = vis[f]; //将f赋值为集合的序号
        ++c[f]; //集合内元素的个数
        x[f][c[f]] = a[i];
        y[f][c[f]] = b[i];
    }

    for(int i = 1; i<=cnt; i++) //全部都取,相当于每个集合增加一个元素
    {
        int X = 0, Y = 0;
        for(int j = 1; j<=c[i]; j++)
        {
            X += x[i][j];
            Y += y[i][j];
        }
        ++c[i];
        x[i][c[i]] = X;
        y[i][c[i]] = Y;
    }
}

void solve()
{
    for(int i = 1; i<=cnt; i++) //01背包
    for(int j = w; j>=1; j--)
    for(int k = 1; k<=c[i]; k++)
        if(j>=x[i][k])
            dp[j] = max(dp[j], dp[j-x[i][k]]+y[i][k]);

    cout<< dp[w]<<endl;
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
//      freopen("output.txt", "w", stdout);
#endif
    init();
    solve();
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动控制节水灌溉技术的高低代表着农业现代化的发展状况,灌溉系统自动化水平较低是制约我国高效农业发展的主要原因。本文就此问题研究了单片机控制的滴灌节水灌溉系统,该系统可对不同土壤的湿度进行监控,并按照作物对土壤湿度的要求进行适时、适量灌水,其核心是单片机和PC机构成的控制部分,主要对土壤湿度与灌水量之间的关系、灌溉控制技术及设备系统的硬件、软件编程各个部分进行了深入的研究。 单片机控制部分采用上下位机的形式。下位机硬件部分选用AT89C51单片机为核心,主要由土壤湿度传感器,信号处理电路,显示电路,输出控制电路,故障报警电路等组成,软件选用汇编语言编程。上位机选用586型以上PC机,通过MAX232芯片实现同下位机的电平转换功能,上下位机之间通过串行通信方式进行数据的双向传输,软件选用VB高级编程语言以建立友好的人机界面。系统主要具有以下功能:可在PC机提供的人机对话界面上设置作物要求的土壤湿度相关参数;单片机可将土壤湿度传感器检测到的土壤湿度模拟量转换成数字量,显示于LED显示器上,同时单片机可采用串行通信方式将此湿度值传输到PC机上;PC机通过其内设程序计算出所需的灌水量和灌水时间,且显示于界面上,并将有关的灌水信息反馈给单片机,若需灌水,则单片机系统启动鸣音报警,发出灌水信号,并经放大驱动设备,开启电磁阀进行倒计时定时灌水,若不需灌水,即PC机上显示的灌水量和灌水时间均为0,系统不进行灌水。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值