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. python之异步IO

    协程的用武之地 并发量较大的系统和容易在IO方面出现瓶颈(磁盘IO,网络IO),采用多线程.多进程可以解决这个问题,当然线程.进程的切换时很消耗资源的.最好的解决方案是使用单线程方式解决并发IO问题- ...

  2. Notepad工具使用小技巧

    工欲善其事必先利其器 Notepad++是个很不错的文本编辑工具,掌握它的使用技巧可以提高我们工作的效率.见如下: 比较常用的罗列如下:(如果有更好的建议可以留言哈) 1: 添加书签 CTRL+F2 ...

  3. Project Structure 讲解(转)

    项目的左侧面板 项目设置->Project Project Settings -> Modules Sources面板 Paths面板 dependencies面板 Project Set ...

  4. the max number of open files 最大打开文件数 ulimit -n RabbitMQ调优

    Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE) — RabbitMQ https://www.rabbitmq.com/i ...

  5. JavaScript library of crypto standards. 看源码

    crypto-js - npm https://www.npmjs.com/package/crypto-js crypto-js/docs/QuickStartGuide.wiki <wiki ...

  6. mysql系列之2.mysql多实例

    使用场景 资金紧张; 并发访问不大; 门户网站; 实现 生产硬件配置: mem 32G / 双cpu 8核 / 磁盘6*600G sas 15k, 2-3个实例 安装组件 #yum install n ...

  7. 【题解】HNOI2013比赛

    [题解][P3230 HNOI2013]比赛 将得分的序列化成样例给的那种表格,发现一行和一列是同时确定的.这个表格之前是正方形的,后来长宽都减去一,还是正方形.问题形式是递归的.这就启示我们可以把这 ...

  8. 我的Android进阶之旅------>Android资源文件string.xml中\u2026的意思

    今天看了一个string.xml文件,对其中的一行代码中包含的\u2026不是很理解,后来查阅资料后发现了其中的意思. 代码如下: <resources xmlns:xliff="ur ...

  9. Android/iOS Remote debugging

    简单介绍 使用下面方法可以定位webview中的元素,无法定位view中的元素. 原文地址:http://mp.weixin.qq.com/s/y_UfdgjT_pkKgYivJmqt7Q webvi ...

  10. Spring Boot2.0之多数据源分布式事务问题

    分布式事务解决方案的问题, 分布式事务产生的原因: 多个不同的服务连接不同的数据源 ,做分布式事务的管理. 这种情况是连接两个数据源的情况,然后事务管理器是这样的 只管理了test02的这端业务代码. ...