HDU - 3899 JLUCPC(树形dp求距离和)
JLUCPC
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求距离和)的更多相关文章
- HDU 3899 简单树形DP
题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- 浅谈关于树形dp求树的直径问题
在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...
- hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解
题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...
- hdu Anniversary party 树形DP,点带有值。求MAX
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- HDU - 2196(树形DP)
题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...
随机推荐
- 【JAVA学习】struts2的action中使用session的方法
尊重版权:http://hi.baidu.com/dillisbest/item/0bdc35c0b477b853ad00efac 在Struts2里,假设须要在Action中使用session.能够 ...
- FI模块与SD、MM的接口配置方法
[转自 http://blog.itpub.net/195776/viewspace-1023910/] 1 FI/SD 借口配置FI/SD通过tcode VKOA为billing设置过帐科目,用户可 ...
- access 驱动在win64位出现问题
如果是调试的话,将应用程序池的 启动win32应用程序池 为 true
- python无法安装cv2的解决办法
问题:在windows命令窗口输入pip install cv2后出现:Could not find a version that satisfies the requirement cv2... 解 ...
- HTML5响应式导航
HTML5响应式导航HTML5,响应式,jQuery特效,HTML5导航,HTML5响应式导航是一款基于HTML5实现的深灰色响应式导航菜单. 地址:http://www.huiyi8.com/sc/ ...
- 通过阿里云域名动态解析 IP 地址
这两天在家里用树莓派折腾了一个家用服务器,主要用来做 mac 的 Time Machine ,还有就是当做下载机和 nas ,想着平时上班时间家里没人用网络,空着也是空着,就可以利用空闲带宽下个美剧啥 ...
- blog集合
godiscoder的技术blog 一个不错的技术架构设计blog MySQLOPS 数据库与运维自动化技术分享 stone的技术blog 陈皓专栏 风雪涟漪的技术blog 华为首席科学家 张宴技术b ...
- div img 垂直水平居中
<style> div { width: 600px; height: 578px; text-align: center; display: table-cell; vertical-a ...
- super.onCreate(savedInstanceState) 以及onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
super.onCreate(savedInstanceState) 调用父类的onCreate构造函数. 当一个Activity在生命周期结束前,会调用onSaveInsanceState()这个回 ...
- UNR #1 火车管理
很简单 用一个线段树维护 1.答案 2.当前栈顶是什么时候push进来的 然后用一棵以时间为版本的可持久化线段树维护每个操作之后第一个覆盖到他的操作是哪个 就可以了 询问直接在线段树上询问,修改在两棵 ...