HDU2196 Computer 树形DP

 这道题有一个很经典的算法。搜索两次,第一次记录每一个节点最大的两个值,记录其中最大值的来源,第二次搜索跟新每一个节点经过父亲节点所能到大的最大值。最后从之前所记录的值中找出最大值,就是题目中要求我们找的每一个节点最远的传输距离。

推荐大家去看一篇文章《运用树型动态规划的解题思路和方法的探析》其中对记录最大值的来源思想,有相关例题介绍。

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1112    Accepted Submission(s): 538


Problem Description
A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.


Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.

Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.

Output
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).

Sample Input
  
  
5 1 1 2 1 3 1 1 1

Sample Output
  
  
3 2 3 4 4
 
#include<iostream>
#include<vector>
#include<queue>
#define MAXN 10100
//树形dp 经典 
using namespace std;

int   n;
bool visited[MAXN];
int dp[3][MAXN];    // [0]最大 [1] 次大  [2] 经过父亲节点的最大值 
int longfrom[MAXN];

class adj
{
	public:
		int dis;
		int node;
};

typedef vector<adj>  computer;

computer c[MAXN];

int max(int a,int b)
{
	return a>b?a:b;
}

void init()
{
	memset(visited,0,sizeof(visited));
	memset(dp,0,sizeof(dp));
	memset(longfrom,0,sizeof(longfrom));
}

void dp1(int root) //dfs  第一次搜索,保存每一个结点最大值和次大值,并且记录最大值的来源 
{
	if(visited[root])
		return;
	visited[root]=true; 
	dp[0][root]=0;
	dp[1][root]=0;
	dp[2][root]=0;
	longfrom[root]=0;
	for(int i=0;i<c[root].size();i++)
	{
		int no=c[root][i].node;
		if(visited[no])
			continue;
		dp1(no);
		if(dp[0][root]<dp[0][no]+c[root][i].dis)
		{
			dp[1][root]=dp[0][root];                 //更新次大值 
			longfrom[root]=no;                       //记录最大值的来源 
			dp[0][root]=dp[0][no]+c[root][i].dis;    //更新最大值 
		}
		else
		{
			if(dp[1][root]<dp[0][no]+c[root][i].dis) //更新次大值 
				dp[1][root]=dp[0][no]+c[root][i].dis;
		}	
	}	
} 

void dp2(int root)  //bfs   ,此处我才用的BFS,采用DFS也可以,貌似更加高效 
{
	queue<int> ns;
	if(visited[root])
		return ;
	ns.push(root);
	while(!ns.empty())
	{
		int nn=ns.size();
		for(int i=0;i<nn;i++)
		{
			int temp=ns.front();
			ns.pop();
			for(int j=0;j<c[temp].size();j++)
			{
				int no=c[temp][j].node;
				if(!visited[no])
					ns.push(no);
				else
					continue;
				if(no!=longfrom[temp])     //判断父亲节点的最大值的来源 
					dp[2][no]=max(dp[2][temp],dp[0][temp])+c[temp][j].dis;    //父亲节点最大值不经过当前节点,则选择父亲结点出最大值更新当前节点经过父亲结点的最大值 
				else	
					dp[2][no]=max(dp[2][temp],dp[1][temp])+c[temp][j].dis;    //否则选择次大值更新 
			}
			visited[temp]=true;
		}
    }
} 

int main()
{
	while(cin>>n)
	{
		init();
		for(int i=0;i<=n;i++)
			c[i].clear();
		for(int i=2;i<=n;i++)
		{
			int to,l;
			adj news;
			cin>>to>>l;
			news.node=i;
			news.dis=l;
			c[to].push_back(news);
			news.node=to;
			c[i].push_back(news);
		}
		dp1(1);     //一次搜索 
		memset(visited,0,sizeof(visited));
		dp2(1);     //二次搜索更新最值 
		for(int i=1;i<=n;i++)
		{
			cout<<max(dp[1][i],max(dp[0][i],dp[2][i]))<<endl;  //选择每个节点三个值的最大值 
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值