Uva 12093 Protecting Zonk

本文介绍了一道关于无根树的动态规划题目。首先通过深度优先搜索(DFS)将无根树转换为有根树,然后从根节点出发,采用动态规划策略解决两类子问题:一种情况是在子节点中至少有一个使用B装置的情况;另一种情况是所有子节点都不使用B装置。文章详细解释了算法思路,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一道动态规划的题目,首先要利用DFS将无根树变为有根树。然后从根开始,这个时候就可以分为两种情况来进行考虑:第一种情况是与根相连接的子节点中至少有一个子节点使用的是B装置,第二种情况是与根节点相连接的所有子节点使用的都不是B装置,对于第一种情况,首先递归算出所有的子节点的最小代价之和,然后对于每一个子节点,都尝试去除当前最优的放置方案,强制放置一个B装置得到的新的代价之和,求出所有的新的代价之和的最小值并且进行保存。对于第二种情况,首先判断当前的节点与其子节点所连接的边是否是激活状态(激活状态只能存在两种情况:第一种情况是当前的节点上放置的是A装置,或者是当前节点相连接的父节点放置的是B装置),然后考虑剩下的两种情况的最小值:对应的子节点放置的是A装置以及对应的子节点没有放置装置,然后求出所有情况的最小值并且返回即可,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;

const int MAX = 10010;

class Edge{
public:
	int from;
	int to;
	int next_e;
};

int dp[MAX][3][3][2];//村庄的个数
Edge edge[MAX*2];
int head[MAX];
int fa[MAX];

class Solve{
public:
	int n, c1, c2;
	int amount;

	void addEdge(int from,int to){
		edge[amount].from = from;
		edge[amount].to = to;
		edge[amount].next_e = head[from];
		head[from] = amount++;
	}

	void dfs(int a,int b){
		fa[a] = b;
		for (int i = head[a]; i != -1; i = edge[i].next_e){
			if (edge[i].to != b){
				dfs(edge[i].to, a);
			}
		}
	}

	void Init(){
		amount = 0;
		memset(head,-1,sizeof(head));
		for (int i = 1; i < n; i++){
			int a, b;
			cin >> a >> b;
			addEdge(a, b);
			addEdge(b, a);
		}
		dfs(1, 0);
		memset(dp,-1,sizeof(dp));
		
	}

	int DP(int ind,int a,int b,int c){
		if (dp[ind][a][b][c] !=-1) return dp[ind][a][b][c];
		int ans = 1<<30;
		int cur = 0;
		for (int i = head[ind]; i != -1;i=edge[i].next_e){//至少有一个装置为B
			int ind2 = edge[i].to;
			if (ind2 != fa[ind]){
				cur += min(min(DP(ind2, 0, a, 1), DP(ind2, 1, a, 1)), DP(ind2, 2, a, 1));
			}
		}
		for (int i = head[ind]; i != -1; i = edge[i].next_e){
			int ind2 = edge[i].to;
			if (ind2 != fa[ind]){
				int temp = min(min(DP(ind2, 0, a, 1), DP(ind2, 1, a, 1)), DP(ind2, 2, a, 1));
				ans = min(ans, cur - temp + DP(ind2, 2, a, 1));
			}
		}
		if (c != 0){//不存在装置B
			int state = (a != 0 || b == 2) ? 1 : 0;
			cur = 0;
			for (int i = head[ind]; i != -1; i = edge[i].next_e){
				int ind2 = edge[i].to;
				if (ind2 != fa[ind]){
					cur += min(DP(ind2, 0, a, state), DP(ind2, 1, a, 1));
				}
			}
			ans = min(ans,cur);
		}
		if (a == 1) ans += c1;
		if (a == 2) ans += c2;
		dp[ind][a][b][c] = ans;
		return ans;
	}

	void Deal(){
		Init();
		cout << min(min(DP(1, 0, 0, 1), DP(1, 1, 0, 1)), DP(1, 2, 0, 1)) << endl;
	}
};

int main(){
	Solve a;
	while (cin >> a.n >> a.c1 >> a.c2){
		if (a.n == 0 && a.c1 == 0 && a.c2 == 0) break;
		a.Deal();
	}
	return 0;
}


### USACO 2007 January Silver Problem K11715 Protecting the Flowers 解析 #### 背景与问题描述 这是一道来自USACO 2007年1月银级的比赛题目,名为《保护花朵》[^1]。在这个场景中,Farmer John有N头奶牛正在践踏他的花园里的花丛。每头奶牛有一个特定的时间t_i离开花园,并且会破坏a_i朵花直到它被赶走。 为了最小化损失,FJ可以在任意时刻选择一头或多头奶牛并将其驱逐出花园。然而每次行动都会消耗一定的能量值E。因此目标是在总能量消耗不超过给定的最大允许值M的情况下,使最终剩余未受损的花朵数量最大化。 #### 思路分析 此题属于典型的贪心算法应用案例之一。核心在于如何合理安排驱逐顺序来达到最优解: - 对于每一组(t, a),即某只奶牛停留时间和其造成的损害程度; - 计算单位时间内该奶牛所造成的影响因子f=a/t; - 将所有影响因子按降序排列; - 根据排序后的列表依次处理各只奶牛,在满足能量预算的前提下尽可能早地移除那些高影响度个体以减少整体损害。 通过上述策略可以有效地降低总的花卉损坏量,从而实现最佳解决方案[^3]。 #### Python 实现代码 下面给出了一种基于Python语言的具体实现方式: ```python def protecting_the_flowers(cows, max_energy): # cows is list of tuples (time_to_leave, flowers_destroyed), max_energy is integer # Calculate destruction rate per unit time and sort by this value descendingly. sorted_cows = sorted([(flowers/float(time), i) for i, (time, flowers) in enumerate(cows)], reverse=True) total_saved = 0 used_energy = 0 while sorted_cows and used_energy + 1 <= max_energy: _, index = sorted_cows.pop(0) time, flowers = cows[index] if used_energy + 1 <= max_energy: total_saved += flowers used_energy += 1 return total_saved # Example usage: cows_data = [(10, 80), (20, 90)] # Each cow's leaving time and destroyed flowers count max_allowed_energy = 1 # Maximum allowed energy to use on chasing away cows print(f"Maximum saved flowers: {protecting_the_flowers(cows_data, max_allowed_energy)}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值