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. intellij idea jdk language level

    jdk的新的版本会兼容旧的版本. 如果安装了新的jdk,但是还是希望使用旧版本的特性,这个可以使用jdk language level来实现. 比如安装了jdk8,但是希望用7,那么language ...

  2. 聚聚科技——php开发笔试题及答案

    聚聚科技是一个刚创立的公司,很小很小,人很少,老板感觉是个典型的北京小伙儿,戾气很重,很有个性.笔试题倒是简单: 1. echo(), print(), print_r()的区别? echo是PHP语 ...

  3. 【题解】quake

    [题解]\(quake\) 题目大意 我们共有报酬\(f\)元,一条边有它的价值\(w_i\),有它的建造时间\(t_i\).要求建一些边,生成一颗树.求最大的利润率. 数据范围 \(n\le 400 ...

  4. Java for LeetCode 083 Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  5. Java基础教程:多线程基础(2)——线程间的通信

    Java基础教程:多线程基础(2)——线程间的通信 使线程间进行通信后,系统之间的交互性会更强大,在大大提高CPU利用率的同时还会使程序员对各线程任务在处理的过程中进行有效的把控与监督. 线程间的通信 ...

  6. -es6的部分语法

    es6的语法 一 . let 和 var 的区别 : 1 . let 和 val 的区别 :  ES6新增了let命令 , 用来声明变量,它的用法类似于 var (ES5), 但是所声明的变量,只在l ...

  7. 20145239杜文超 《Java程序设计》第10周学习总结

    20145239 <Java程序设计>第10周学习总结 教材学习内容总结 Java的网络编程 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 网络概述 1.计算机 ...

  8. javscript 一些常用的工具方法

    一些工作中经常会用到的js代码,可以封装成一个工具库. 积少成多,从现在开始吧! -------------- 1 . 判断一段文字的长度.要求中文相当于2个字符,非中文的相当于1个字符 String ...

  9. Matlab的publish功能和cell功能

    Matlab的publish功能能够让写的代码变成优美的文档.类似为知笔记的markdown语言. cell功能配合publish使用,可以形成不同的功能块.而且调试的时候,可以按section调试. ...

  10. Servlet传递数据方式

    Servlet传递数据方式 基本概述 Servlet传递数据的方式有很多,这里提供五种方式: 1.静态变量 2.HttpServletResponse的sendRedirect()方法 3.HttpS ...