HDU

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]=0;     //点权
    for(int i=0;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=0;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=1;i<=n;i++){
            scanf("%I64d",&p[i]);
            cnt[i]=0;
            sum[i]=0;
            v[i].clear();
        }
        for(i=1;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(1,-1);
        dfss(1,-1);
        ll minn=INF;
        for(i=1;i<=n;i++){
            minn=min(minn,sum[i]);
        }
        printf("%I64d
",minn);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yzm10/p/9579709.html