首页 > 其他分享 >Moving to Nuremberg UVA12223

Moving to Nuremberg UVA12223

时间:2023-04-25 13:23:15浏览次数:41  
标签:Nuremberg 景点 int add Moving UVA12223 ans include hd

题目大意:给出n,一个无根的树,每条边上都有权值。

现在每个位置都有一个景点,一个人想在一年之内去cnt[ i ] 次景点,所以接下来给出m,表示说在m个位置上有这个人想去的地方,给出位置以及想去的次数(注意,每去一个景点都要返回自己的住处),namo这个人该住在哪里走的路程才最短。

换根dp

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N =1e5,M=N;
#define int long long
const int inf =1e9 ;
 int n,m,f[N],g[N],s[N] ;
 int ans ;
 int nxt[M],go[M],hd[N],all=1,w[M];
 
 void add_(int x,int y,int z){
 	all++;go[all]=y; nxt[all]=hd[x]; hd[x]=all;
 	w[all]=z;
 }
 void clr() {
 	ans=inf;
    memset(s, 0, sizeof s);
    all=1; for (int i= 0;i<=n; i++) hd[i]=0;
 }
 void dfs(int x,int fa){
 	f[x]=0;
 	for(int i=hd[x];i;i=nxt[i]){
 		int y=go[i],z=w[i];  if(fa==y) continue;
 		dfs(y,x); 
 		
 		s[x]+=s[y];
 		f[x]+=f[y]+2*z*s[y] ;
 	}
 }
 void dp(int x,int fa){
 	ans=min(ans,g[x]); 
 	for(int i=hd[x];i;i=nxt[i]){
 		int y=go[i],z=w[i];  if(fa==y) continue;
 		g[y]= g[x]- s[y]*2*z + (s[1]-s[y])*2*z;
 		dp(y,x); 
 	}
 }
 signed main(){
 	int tes;cin>>tes;
 	while(tes--){
	 	cin>>n;
	 	clr();
	 	int x,y,z;
	 	for(int i=1;i<n;i++)
	 		cin>>x>>y>>z,add_(x,y,z),add_(y,x,z);
	 	cin>>m;
	 	for(int i=1;i<=m;i++) 
	 		cin>>x>>z,s[x]=z;
	 		
	 	dfs(1,0); 
	 	g[1]=f[1]; ans=g[1];
	 	dp(1,0);
	 	cout<<ans <<endl; 
	 	
	 	int flg = 0;
        for (int i=1;i<=n;i++) {
            if (g[i] ==ans && !flg) {
                cout<<i;
                flg = 1;
            }
            else if (g[i] ==ans) cout<<' '<<i;
        }
       cout<<endl;
 	}
 }

 

标签:Nuremberg,景点,int,add,Moving,UVA12223,ans,include,hd
From: https://www.cnblogs.com/towboa/p/17352301.html

相关文章

  • AtCoder Regular Contest 114 D Moving Pieces on Line
    洛谷传送门AtCoder传送门挺有意思的题。首先显然地,一个棋子不会走回头路。于是一个棋子沿着边走的效果就是区间异或。更进一步,设\(s_i\)为\(i-1\toi\)的边颜色与\(i\toi+1\)的边颜色是否相同(差分),相当于对于每个\(i\)都选择\(s_{a_i}\)和\(s_{x_i}\),将它们异或......
  • [LeetCode] 2390. Removing Stars From a String
    Youaregivenastring s,whichcontainsstars *.Inoneoperation,youcan:Chooseastarin s.Removetheclosest non-star charactertoits left,aswellasremovethestaritself.Return thestringafter all starshavebeenremoved.Note:Thei......
  • 使用Python实现Hull Moving Average (HMA)
    赫尔移动平均线(HullMovingAverage,简称HMA)是一种技术指标,于2005年由AlanHull开发。它是一种移动平均线,利用加权计算来减少滞后并提高准确性。HMA对价格变动非常敏感,同时最大程度地减少短期波动可能产生的噪音。它通过使用加权计算来强调更近期的价格,同时平滑数据。计算HMA的公......
  • CF1788D Moving Dots 题解
    可能更好的阅读体验题目传送门题目翻译题目解析考虑怎样才能产生贡献,显然对于留下的相邻的\(l,r\),需要让\(l\)向右,\(r\)向左即可产生\(1\)的贡献。接下来就是考......
  • D. Moving Dots(组合数学,贡献,二分/双指针)
    题目https://codeforces.com/contest/1788/problem/D思路从题目给的“2”这个信息入手,从贡献这个方面来考虑对于任意两不同的点,具有一定的范围,让这个范围内的点都被......
  • D. Moving Dots
    D.MovingDotsWeplayagamewith$n$dotsonanumberline.Theinitialcoordinateofthe$i$-thdotis$x_i$.Thesecoordinatesaredistinct.Everydotstar......
  • 时间序列分析 Tsfresh 基于统计学的时间序列分析方法 3、差分整合移动平均自回归模型(A
    原文链接:点这里在了解了AR和MA模型后,我们将进一步学习结合了这两者的ARIMA模型,ARIMA在时间序列数据分析中有着非常重要的地位。但在这之前,让我们先来看ARIMA的简化版ARMA......
  • Kafka学习笔记(七):Kafka KRaft - Removing Zookeeper
    关于KafkaKRaft在2020年,ApacheKafka项目做开始着手移除Zookeeper依赖(KIP-500)当Kafka集群拥有超过10万个分区时,Zookeeper有扩展问题删除Zookeeper之后,ApacheKa......
  • [LeetCode] 1753. Maximum Score From Removing Stones
    Youareplayingasolitairegamewith threepiles ofstonesofsizes a​​​​​​, b,​​​​​​and c​​​​​​respectively.Eachturnyouchoosetw......
  • Fast22 - Removing Double-Logging with Passive Data Persistence in LSM-tree based
    基于LSM-tree的关系型数据库中,通过被动的数据持久化方式移除双重日志记录原文链接摘要    存储引擎是关系型数据库(RDB)中的重要组成部分。随着互联网服务和应用......