URAL 1039 ,CF1083A

Background
The president of the Ural State University is going to make an 80’th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor relation forms a tree rooted at the president. Employees are numbered by integer numbers in a range from 1 to N, The personnel office has ranked each employee with a conviviality rating. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.
Problem
Your task is to make up a guest list with the maximal conviviality rating of the guests.
Input
The first line of the input contains a number N. 1 ≤ N ≤ 6000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from –128 to 127. After that the supervisor relation tree goes. Each line of the tree specification has the form

which means that the K-th employee is an immediate supervisor of L-th employee. Input is ended with the line
0 0
Output
The output should contain the maximal total rating of the guests.
Example
input output
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
5
水题,直接上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define N 6666
int n;
int a[N];
int fa[N];
int dp[N][2];
bool vis[N];
vector<int> g[N*4];
void dfs(int u)
{
	int i,j;
	dp[u][0]=0;dp[u][1]=a[u];
    //cout<<dp[u][0]<<" * "<<dp[u][1]<<endl;
	for(i=0;i<g[u].size();i++)
	{
		int v=g[u][i];
		dfs(v);   //自底向上的计算  
		dp[u][0]+=max(dp[v][0],dp[v][1]);
		dp[u][1]+=dp[v][0];
		
	}
	
}
int main()
{
//	freopen("q.in","r",stdin);
	int n;
	int i,j;
	int l,k,root;
	memset(fa,0,sizeof(fa));
	memset(dp,0,sizeof(dp));
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	
	while(~scanf("%d%d",&l,&k) && l+k)
	{
		g[k].push_back(l);
		fa[l]=k;		
	}
	for(i=1;i<=n;i++)if(fa[i]==0){root=i;break;}
    dfs(root);
     cout<<max(dp[root][0],dp[root][1])<<endl;
	
}

1 二:
The Fair Nut is going to travel to the Tree Country, in which there are n cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent a car in the city u and go by a simple path to city v. He hasn’t determined the path, so it’s time to do it. Note that chosen path can consist of only one vertex.

A filling station is located in every city. Because of strange law, Nut can buy only wi liters of gasoline in the i-th city. We can assume, that he has infinite money. Each road has a length, and as soon as Nut drives through this road, the amount of gasoline decreases by length. Of course, Nut can’t choose a path, which consists of roads, where he runs out of gasoline. He can buy gasoline in every visited city, even in the first and the last.

He also wants to find the maximum amount of gasoline that he can have at the end of the path. Help him: count it.

Input
The first line contains a single integer n (1≤n≤3⋅105) — the number of cities.

The second line contains n integers w1,w2,…,wn (0≤wi≤109) — the maximum amounts of liters of gasoline that Nut can buy in cities.

Each of the next n−1 lines describes road and contains three integers u, v, c (1≤u,v≤n, 1≤c≤109, u≠v), where u and v — cities that are connected by this road and c — its length.

It is guaranteed that graph of road connectivity is a tree.

Output
Print one number — the maximum amount of gasoline that he can have at the end of the path.

Examples
Input
3
1 3 3
1 2 2
1 3 2
Output
3
Input
5
6 3 2 5 0
1 2 10
2 3 3
2 4 1
1 5 1
Output
7
Note
The optimal way in the first example is 2→1→3.

The optimal way in the second example is 2→4.

题意:

就是点是可以加油的,然后线上是耗油的。问任意两点之间的所有路径的和最大的值。

思路:经典的树形dp。

用dp[u]表示以u为起点的最大值,dp_z[u]代表,u作为中间点的最大值。那么状态转移方程自然就出来了:

对于点u,从所有儿子中找到一个最大的dp[ son1 ],次大的dp[ son2 ],
dp_z[ u ]=a[ u ]+dp[ son1 ]+dist( u,son1 )+dp[ son2 ]+dist( u,son2 ),
dp[ u ]=a[ u ]+dp[ son1 ]+dist( u,son1 )
代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=300000+100;
typedef long long LL;
vector<int>G[maxn],dist[maxn];
LL a[maxn],dp[maxn],dp_z[maxn];
int w;
int u,v;
LL ans;
void dfs(int u,int fa){
    LL mx1=0;
    LL mx2=0;
    ///dp[u]=a[u];///u作为起点
    ///dp_z[u]=a[u];///作为中间点
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==fa)
            continue;
        dfs(v,u);
        if(dp[v]-dist[u][i]>mx1){
            mx2=mx1;
            mx1=dp[v]-dist[u][i];
        }
        else if(dp[v]-dist[u][i]==mx1){
            mx2=mx1;
        }
        else{
            if(dp[v]-dist[u][i]>mx2)
                mx2=dp[v]-dist[u][i];
        }
    }
    dp_z[u]=a[u]+mx1+mx2;
    dp[u]=a[u]+mx1;
    ans=max(dp_z[u],ans);
    ans=max(dp[u],ans);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
            ans=0;
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        for(int i=1;i<=n-1;i++){
            scanf("%d%d%d",&u,&v,&w);
            G[u].push_back(v);
            G[v].push_back(u);
            dist[u].push_back(w);
            dist[v].push_back(w);
        }
        dfs(1,-1);
        cout<<ans<<endl;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值