JLUCPC

Dr. Skywind and Dr. Walkoncloud are planning to hold the annual JLU Collegiate Programming Contest. The contest was always held in the college of software in the past. However, they changed their minds and decided to find the most convenient location from all colleges this year. 
Each college in JLU is located in one of the N (1 <= N <= 100,000) different locations (labeled as 1 to N) connected by N-1 roads. Any two colleges are reachable to each other. The Contest can be held at any one of these N colleges. Moreover, Road i connects college A_i and B_i (1 <= A_i <=N; 1 <= B_i <= N) and has length L_i (1 <= L_i <= 1,000). College i has T_i (0 <= T_i <= 1,000) teams participating in the contest. 
When choosing the college to hold the Contest, Dr. Skywind wishes to minimize the inconvenience of the chosen location. The inconvenience of choosing college P is the sum of the distance that all teams need to reach college P (i.e., if the distance from college i to college P is 20, then the travel distance is T_i*20). Please help Dr. Skywind and Dr. Walkoncloud to choose the most convenient location for the contest.

InputThere are multiple test cases. For each case, the first line contains a single integer N, indicating the number of colleges. The next N lines describes T_1 to T_n. Then, each of the last N-1 lines will contain 3 integers, namely A_i, B_i and L_i.OutputFor each case, output the minimum inconvenience possibleSample Input

3
1
1
2
1 2 2
2 3 1
4
100
1
1
1
1 2 1
2 3 1
2 4 1

Sample Output

4
5 求一点到其他点的距离和的最小值(含点权边权)。
因为涉及到多源,用最短路求解复杂度O(n^3),因此需要用到树形dp思想。
两次dfs。以1点为根节点,第一次从下往上递归,将每个点的子点与子和(真子节点×当前边)求出,这样便知道了各点以下的距离和。
然后第二次dfs从上往下,将各点以上的距离和加入当前点,处理方法是+(父节点距离和-当前点距离和),还要对连接父子节点的边进行处理,详见代码:
#include<bits/stdc++.h>
#define MAX 100005
#define INF 1000000000000000000
using namespace std;
typedef long long ll; ll p[MAX],cnt[MAX],sum[MAX];
struct Node{
ll v,w;
}node;
vector<Node> v[MAX]; void dfs(ll x,ll pre){
cnt[x]=p[x];sum[x]=; //点权
for(int i=;i<v[x].size();i++){
ll to=v[x][i].v;
if(to==pre) continue;
ll w=v[x][i].w;
dfs(to,x);
cnt[x]+=cnt[to];
sum[x]+=sum[to]+cnt[to]*w; //边权
}
}
void dfss(ll x,ll pre){ for(int i=;i<v[x].size();i++){
ll to=v[x][i].v;
if(to==pre) continue;
ll w=v[x][i].w;
sum[to]+=sum[x]-sum[to]-cnt[to]*w+(cnt[x]-cnt[to])*w; //边权
cnt[to]+=cnt[x]-cnt[to];
dfss(to,x);
}
}
int main()
{
int n,i;
ll x,y,w;
while(~scanf("%d",&n)){
for(i=;i<=n;i++){
scanf("%I64d",&p[i]);
cnt[i]=;
sum[i]=;
v[i].clear();
}
for(i=;i<n;i++){
scanf("%I64d%I64d%I64d",&x,&y,&w);
node.v=y;
node.w=w;
v[x].push_back(node);
node.v=x;
v[y].push_back(node);
}
dfs(,-);
dfss(,-);
ll minn=INF;
for(i=;i<=n;i++){
minn=min(minn,sum[i]);
}
printf("%I64d\n",minn);
}
return ;
}

HDU - 3899 JLUCPC(树形dp求距离和)的更多相关文章

  1. HDU 3899 简单树形DP

    题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...

  2. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  3. 浅谈关于树形dp求树的直径问题

    在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...

  4. hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解

    题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...

  5. hdu Anniversary party 树形DP,点带有值。求MAX

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. HDU 2196 Computer 树形DP 经典题

    给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...

  7. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  9. HDU - 2196(树形DP)

    题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...

随机推荐

  1. 几句话搞懂URI、URL、URN之间的关系

    1.URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源. 2.RL是uniform resource locator,统一资源定位器,它是一种具体 ...

  2. C#操作XML方法:新增、修改和删除节点与属性

    一 前言 先来了解下操作XML所涉及到的几个类及之间的关系  如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家 * 1 XMLElement 主要是针对节点的一些属性进行操 ...

  3. 怎么使用Aspose.Cells读取excel 转化为Datatable

    说明:vs2012 asp.net mvc4 c# 使用Aspose.Cells 读取Excel 转化为Datatable 1.HTML前端代码 <%@ Page Language=" ...

  4. sort()函数到底是怎样进行数字排序的

    很多人会用sort(),并不见得知道它具体是怎样给数字排序的.其实不知道也行,会用就可以,感兴趣的可以来看看. var numberArray = [2,4,1,3]; numberArray.sor ...

  5. CF(439E - Devu and Birthday Celebration)莫比乌斯容斥

    题意:将n个糖果插入f-1个挡板分成f分(a1,a2,a3...af). 问有多少种分法能够使得gcd(a1,a2,a3...af)=1; 解法.莫比乌斯容斥,首先按1为单位分,这时候有C(n-1,f ...

  6. PHP上传视频

    https://github.com/chaping/plupload_docs 这下载 lupload有以下功能和特点: 1.拥有多种上传方式:HTML5.flash.silverlight以及传统 ...

  7. Docker实践中遇到的坑

    1.docker容器中后台运行退出执行curl+p+q,再次进入执行命令docker attach 容器id. 2.容器中exit退出后,还原方法为docker ps -a 查看历史运行容器,dock ...

  8. Android Weekly Notes Issue #261

    Android Weekly Issue #261 June 11th, 2017 Android Weekly Issue #261 本期内容包括: Adaptive Icons; Kotlin实现 ...

  9. Django+ajax+jsonp实现借口调用文本处理

    首页 提交 <script src="/static/jquery-2.1.4.min.js"></script> <script type=&quo ...

  10. Module.exports和exports的区别

    原文链接: https://www.ycjcl.cc/2017/02/10/module-exportshe-exportsde-qu-bie/ 学习Seajs时,看到了exports.doSomet ...