nssl1163-小x游世界树【树形dp,二次扫描和换根法】

版权声明:原创,未经作者允许禁止转载 https://blog.csdn.net/Mr_wuyongcong/article/details/82822084

正题


题目大意

一棵树,一条边的权是原本的权值减去出发点的加速。
求一个点使得这个点到所有点路径边权和最小。


解题思路

我们先求出以1为根时的答案
然后用换根法
在这里插入图片描述
我们从1转移到2,我们会发现
在这里插入图片描述
红色的部分的路径都减去的紫色的路径长度,蓝色的部分路径长度都加上这条紫色的路径(注意因为出发点不同所以权值不同)
所以我们推出根转移方程
f y = f x n u m y ( w m o v x ) + ( n n u m y ) ( w m o v y ) f_y=f_x-num_y*(w-mov_x)+(n-num_y)*(w-mov_y)


code

#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
#define N 700010
using namespace std;
struct line{
	ll to,w,next;
}a[N*2];
ll ls[N],mov[N],x,y,w,n,f[N],num[N],tot,ans;
int read(){
    char c=getchar();int x=0;
    for(;'0'>c||c>'9';c=getchar());
    for(;'0'<=c&&c<='9';c=getchar()) x=x*10+(c-'0');
    return x;
}
void addl(ll x,ll y,ll w)
{
	a[++tot].to=y;a[tot].w=w;
	a[tot].next=ls[x];ls[x]=tot;
}
void dp(ll x,ll fa)//计算第一个答案和子树大小
{
	num[x]=1;
	for(ll i=ls[x];i;i=a[i].next)
	{
		ll y=a[i].to;
		if(y!=fa)
		{
			dp(y,x);
			f[1]+=max(a[i].w-mov[x],0ll)*num[y];
			num[x]+=num[y];
		}
	}
}
void dp2(ll x,ll fa)//转移根
{
	if(f[x]<f[ans]||(f[x]==f[ans]&&x<ans)) 
	  ans=x;//统计答案
	for(ll i=ls[x];i;i=a[i].next)
	{
		ll y=a[i].to;
		if(y!=fa)
		{
			f[y]=f[x]-num[y]*(a[i].w-mov[x])+(n-num[y])*(a[i].w-mov[y]);
			//动态转移
			dp2(y,x);
		}
	}
}
int main()
{
	n=read();
	for(ll i=1;i<=n;i++)
	  mov[i]=read();
	for(ll i=1;i<n;i++)
	{
		x=read();y=read();w=read();
		addl(x,y,w);addl(y,x,w);
	}
	dp(1,0);
	ans=1;
	dp2(1,0);
	printf("%lld\n%lld",ans,f[ans]);
}

猜你喜欢

转载自blog.csdn.net/Mr_wuyongcong/article/details/82822084
今日推荐