【hiho一下_week258】EL SUENO

hihocoder的hiho一下,第258周的题目



原题

传送门:https://hihocoder.com/contest/hiho258/problem/1

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

In a video game, Little Hi is going to assassinate the leader of an
evil group, EL SUENO.

There are N high-value targets in the group, numbered from 1 to N.
Each target has another target as his direct superior except for EL
SUENO who has no superior. So the superior-subordinate hierarchy forms
a tree-like structure. EL SUENO is at the root.

The cost of assassinating the i-th target is Ci. Before assassinating
a target Little Hi has to obtain enough information about him. For the
i-th target, Little Hi needs INi units of information. By
assassinating a target Little Hi will obtain some information about
the target’s direct superior. More specifically, the i-th target has
IPi units of information about his superior. So in order to
assassinate EL SUENO, Little Hi needs to assassinate some of his
direct subordinates so that the sum of information obtained is enough;
assassinating the subordinates needs to assassinate their direct
subordinates … until it reaches some targets require zero
information in advance. (Luckily if a target has no subordinate he
always requires zero information.)

How Little Hi wants to know what is the minimum cost for successful
assassinating EL SUENO

输入

The first line contains an integer N.

Then follow N lines. Each line describes a target with 4 integers, Fi,
INi, IPi, Ci.

Fi is i-th target’s superior. For EL SUENO his Fi is zero.

INi is the amount of information needed to assassinate the i-th
target. For a target has no subordinates his INi is always zero.

IPi is the amount of information the i-th target has about his
superior Fi.

Ci is the cost of assassinating the i-th target.

For 30% of the data, 1 <= N <= 10, 0 <= INi, IPi <= 100

For 60% of the data, 1 <= N <= 100, 0 <= INi, IPi <= 1000

For 100% of the data, 1 <= N <= 2000, 0 <= Fi <= N, 0 <= INi, IPi <=
20000, 1 <= Ci <= 1000.

It is guaranteed that the N targets form a tree structure.

输出

The minimum cost. If Little Hi is not able to assassinate EL SUENO output a number -1.


我的思路

这道题类似于0-1背包问题,感觉思路应该还是很清晰的,dfs应该就能做

代码:

#include <iostream>
#include <vector>

using namespace std;
typedef long long int ll;
const int N = 2001;
const long long int MAX = 1e16;

vector<int> inferior[N]; 

int n;
int f[N];
int in[N];
int ip[N];
int c[N];

/* https://hihocoder.com/contest/hiho258 */

ll dfs(int node) {
	ll dp[20020];
    dp[0]=0;
    for(int i=1;i<=in[node];i++) {
    	dp[i]=1e16;	
	}
    for(int i=0;i<inferior[node].size();i++)
    {
        int v=inferior[node][i];
        ll tmp=dfs(v);
        for(int j=in[node];j>0;j--)
            dp[j]=min(dp[j],dp[max(j-ip[v],0)]+c[v]+tmp);
    }
    return dp[in[node]];
}


int main(int argc, char** argv) {
	
	scanf("%d", &n);
	int root = 0;
	for (int i = 1; i <= n; i++) {
		scanf("%d %d %d %d", &f[i], &in[i], &ip[i], &c[i]);
		if (f[i] == 0) {
			root = i;
		}
		// 记录每个节点的下属 
		inferior[f[i]].push_back(i);
	}
	ll ans = dfs(root); 
	if (ans >= MAX) {
		printf("-1\n");
	} 
	else {
		printf("%lld\n", ans+c[root]);
	}
	
	return 0;
}

解析

  • 解析

本题是一道树形DP(递归)+背包的题目。

首先基本的思路是自底(叶子)向上(根)依次计算出刺杀每个目标需要的代价,不妨记为f[i]。

显然对于叶节点来说,由于不需要额外的信息,所以f[i]就等于该目标的Ci。

对于一个非叶子节点v,如何求f[v]呢?

由于我们是自底向上计算的,所以我们可以认为刺杀v的每个子节点的代价都已经求出了。不妨设是f[c1], f[c2] … f[ck]。

现在我们的问题转化为:从c1 … ck中选出若干个,使得这些节点的IP之和大于等于IN[v],并且代价之和最小。

这个子问题显然是一个变形的背包问题,我们可以用DP解决。

这样整个问题就解决了,总时间复杂度是O(N max{IN})的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值