UVa12093 Protecting Zonk

本文介绍了一种解决覆盖无根树所有边最小花费的算法,通过树型dp实现,具体步骤包括选择装置类型(A或B)及覆盖状态(0-3),并计算最小总花费。

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

        题意:给定一个有n个节点的无根树,有两种装置A和B,每种都有无限多个。在某个节点X使用A装置需要C1的花费,并且此时与节点X相连的边都被覆盖。在某个节点X使用B装置需要C2的花费,并且此时与节点X相连的边以及与X相连的点相连的边都被覆盖。求覆盖所有边的最小花费。

        思路:树型dp。随便拿一个点当作树根,dp(i,j,k),i代表节点号,j代表选择装置的类型,k代表覆盖状态,数组存该状态下最小花费。其中j=0:不部署装置,j=1:部署A装置,j=2:部署b装置。k=0:当前节点与父节点连接的边没有覆盖,k=1:覆盖了当前节点与父节点连接的边,k=2:覆盖了当前节点与父子节点连接的边,k=3:覆盖了当前节点与父节点,子孙节点连接的边。


#include <iostream>    
#include <stdio.h>    
#include <cmath>    
#include <algorithm>    
#include <iomanip>    
#include <cstdlib>    
#include <string>    
#include <memory.h>    
#include <vector>    
#include <queue>    
#include <stack>    
#include <map>  
#include <set>  
#include <ctype.h>    
#define INF 10000000
#define ll long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define MAXN 100010

using namespace std;  

vector<int> E[10010];
int dp[10010][3][4];
int n,c1,c2;

inline int fun(int u,int f,int type,int statu){
	if(dp[u][type][statu]!=-1)return dp[u][type][statu];
	
	int re=0;
	
	if(statu==0){
		re=INF;
		int tmp=0;
		for(int i=0;i<E[u].size();i++){
			if(E[u][i]==f)continue;
			tmp+=min3( fun(E[u][i],u,0,1) , 
			          fun(E[u][i],u,1,2) ,
					  fun(E[u][i],u,2,3) );
		}
		for(int i=0;i<E[u].size();i++){
			if(E[u][i]==f)continue;
			re=min(re,tmp-min3( fun(E[u][i],u,0,1) , 
			          fun(E[u][i],u,1,2) ,
					  fun(E[u][i],u,2,3) )+fun(E[u][i],u,2,3));
		}
		
	}else{
		for(int i=0;i<E[u].size();i++){
			if(E[u][i]==f)continue;
			re+=min3( fun(E[u][i],u,0,statu-1) , 
			          fun(E[u][i],u,1,2) ,
					  fun(E[u][i],u,2,3) );
		}
		
		int tmp=0;
		for(int i=0;i<E[u].size();i++){
			if(E[u][i]==f)continue;
			tmp+=min3( fun(E[u][i],u,0,1) , 
			          fun(E[u][i],u,1,2) ,
					  fun(E[u][i],u,2,3) );
		}
		for(int i=0;i<E[u].size();i++){
			if(E[u][i]==f)continue;
			re=min(re,tmp-min3( fun(E[u][i],u,0,1) , 
			          fun(E[u][i],u,1,2) ,
					  fun(E[u][i],u,2,3) )+fun(E[u][i],u,2,3));
		}
	}
	if(type==1)re+=c1;
	if(type==2)re+=c2;
	dp[u][type][statu]=re;
	return re;
}


int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	while(cin>>n>>c1>>c2){
		if(n==0&&c1==0&&c2==0)break;
		memset(dp,-1,sizeof(dp));
		for(int i=1;i<=n;i++)E[i].clear();
		//
		int u,v;
		for(int i=1;i<n;i++){
			cin>>u>>v;
			E[u].push_back(v);
			E[v].push_back(u);
		}
		int ans=min3( fun(1,-1,0,1) , fun(1,-1,1,2) , fun(1,-1,2,3)  );
		cout<<ans<<endl;
	}
	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、付费专栏及课程。

余额充值