首页 技术 正文
技术 2022年11月14日
0 收藏 448 点赞 2,164 浏览 2312 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,702
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741