UPC—— 最勇敢的机器人(并查集+分组背包)

简介: UPC—— 最勇敢的机器人(并查集+分组背包)

问题 K: 最勇敢的机器人

时间限制: 1 Sec 内存限制: 128 MB


题目描述

机器人们都想知道谁是最勇敢的,于是它们比赛搬运一些物品。

它们到了一个仓库,里面有n个物品,每个物品都有一个价值Pi和重量Wi,但是有些物品放在一起会爆炸,并且爆炸具有传递性。(a和b会爆炸、b和c会爆炸则a和c会爆炸)机器人们可不想因此损失自己好不容易从Wind那里敲诈来的装备,于是它们想知道在能力范围内,它们最多可以拿多少价值的物品。

你能帮助它们吗?


输入

每组测试数据

第1行为n,Wmax,k(0<=n,Wmax,k<=1000)

接下来n行,为每个物品的Pi,Wi(0<=Pi<=1000,1<=Wi<=10,均为整数)

再接下来k行,每行2个数字a,b表示a和b会发生爆炸

输出

对每组数据输出1行,为最大可能价值

样例输入 Copy

3 10 1

100 1

200 5

10 5

1 2

样例输出 Copy

210

思路:

互相引起爆炸的相当于一个一组,一组里只能取一个,用并查集维护在哪组,分组背包求答案。

代码:

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
char F[200];
inline void out(I_int x)
{
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0)
    {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int maxn=1100,N=1e6+100,mod=1e9+7;
const double PI = atan(1.0)*4;
const double eps=1e-6;
int root[maxn],w[maxn],p[maxn];
int n,m,k;
vector<int>v[maxn];
int Find(int x){
    if(x!=root[x]) root[x]=Find(root[x]);
    return root[x];
}
int dp[1100];
int ww[1100][1100],pp[1100][1100];
int tot[maxn];
int main()
{
    n=read(),m=read(),k=read();
    for(int i=1;i<=n;i++) w[i]=read(),p[i]=read();
    for(int i=1;i<=n;i++) root[i]=i;
    while(k--){
        int u=read(),v=read();
        u=Find(u),v=Find(v);
        if(u!=v) root[u]=v;
    }
    for(int i=1;i<=n;i++) v[Find(i)].push_back(i);
    int cnt=0;
    for(int i=1;i<=n;i++)
        if(i==Find(i)){
            cnt++;
            tot[cnt]=v[i].size();
            int idx=0;
            for(auto t:v[i]){
                idx++;
                ww[cnt][idx]=w[t];
                pp[cnt][idx]=p[t];
            }
        }
    for(int i=1;i<=cnt;i++)
        for(int j=m;j>=0;j--)
            for(int k=1;k<=tot[i];k++)
                if(pp[i][k]<=j)
                    dp[j]=max(dp[j],dp[j-pp[i][k]]+ww[i][k]);
    out(dp[m]);
    return 0;
}
目录
相关文章
|
9月前
|
机器学习/深度学习 人工智能 网络架构
P1563 [NOIP2016 提高组] 玩具谜题(找规律,心要细,数学思维)
P1563 [NOIP2016 提高组] 玩具谜题(找规律,心要细,数学思维)
39 0
|
11月前
|
人工智能
upc 2021级新生个人训练赛第53场(珂朵莉与数字,珂朵莉与序列,珂朵莉与字符串,珂朵莉与面积)
upc 2021级新生个人训练赛第53场(珂朵莉与数字,珂朵莉与序列,珂朵莉与字符串,珂朵莉与面积)
72 0
|
算法
【递归与递推】洛谷[NOIP2002 普及组] 过河卒
前言 本题来自洛谷P1002. 题目链接:[NOIP2002 普及组] 过河卒 - 洛谷
148 0
|
人工智能 BI
UPC Decayed Bridges(并查集+思维)
UPC Decayed Bridges(并查集+思维)
86 0
|
人工智能 BI
upc-2021个人训练赛第27场 D: Values(思维+并查集)
upc-2021个人训练赛第27场 D: Values(思维+并查集)
66 0
|
机器学习/深度学习
2019CCPC厦门站 H. Zayin and Obstacles(三维前缀和 bfs)
2019CCPC厦门站 H. Zayin and Obstacles(三维前缀和 bfs)
64 0
PTA团体程序设计天梯赛-练习集 L3-020 至多删三个字符 (dp)
PTA团体程序设计天梯赛-练习集 L3-020 至多删三个字符 (dp)
122 0
UPC-游戏智商+ 路标 (动态规划+DFS)
UPC-游戏智商+ 路标 (动态规划+DFS)
92 0
|
机器学习/深度学习
[Nowcoder | UPC] 2021年度训练联盟热身训练赛第六场 Hopscotch | 最短路 bfs
题目描述 There’s a new art installation in town, and it inspires you… to play a childish game. The art installation consists of a floor with an n×n matrix of square tiles. Each tile holds a single number from 1 to k. You want to play hopscotch on it.
100 0
|
人工智能 安全
UPC-2021个人训练赛第20场-部分题解
RGB Triplets 题目描述 输入 输出 样例输入 Copy 样例输出 Copy 提示 Select Half 题目描述 输入 输出 样例输入 Copy 样例输出 Copy 提示 心灵的抚慰 题目描述 输入 输出 样例输入 Copy 样例输出 Copy 提示
147 0

热门文章

最新文章