HDU 3899 树形DP

JLUCPC

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1346    Accepted Submission(s): 397

Problem Description

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.

Input

There 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.

Output

For each case, output the minimum inconvenience possible

Sample 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

Source

2011 Multi-University Training Contest 6 - Host by JLU

题意:

有n个点,每个点有相应的人数,n-1条带权边,从a点的所有人走到b点的花费是a点的人数*边权值,问所有人在哪个点集合能够使花费最小求最小花费。

代码:

//先dfs求出以1为根节点使每个点后代到这个点集合的花费sum以及这个点及其后代共有几个人cnt
//然后枚举每个点i作为集合点(根节点)时他的花费就是sum[i]+他的父节点减去从i来的人的花费+
//父节点总人数减去从i来的人数*边权值。并且更新最小花费和sum[i],cnt[i].
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=100009;
int n,val[maxn],head[maxn],cnt[maxn],tol;
ll sum[maxn],ans;
struct node{
    int to,w,next;
}nodes[maxn*2];
void Add(int x,int y,int z){
    nodes[tol].to=y;
    nodes[tol].w=z;
    nodes[tol].next=head[x];
    head[x]=tol++;
}
void dfs(int x,int fa){
    cnt[x]=val[x];sum[x]=0;
    for(int i=head[x];i!=-1;i=nodes[i].next){
        int y=nodes[i].to;
        if(y==fa) continue;
        dfs(y,x);
        cnt[x]+=cnt[y];
        sum[x]+=(sum[y]+1LL*cnt[y]*nodes[i].w);
    }
}
void dfs1(int x,int fa){
    for(int i=head[x];i!=-1;i=nodes[i].next){
        int y=nodes[i].to;
        if(y==fa) continue;
        sum[y]=sum[y]+sum[x]-sum[y]-1LL*cnt[y]*nodes[i].w+1LL*(cnt[x]-cnt[y])*nodes[i].w;
        cnt[y]+=(cnt[x]-cnt[y]);
        //cout<<y<<" "<<sum[y]<<endl;
        //ans=min(ans,sum[y]+sum[x]-sum[y]-1LL*cnt[y]*nodes[i].w+1LL*(cnt[x]-cnt[y])*nodes[i].w);
        ans=min(ans,sum[y]);
        dfs1(y,x);
    }
}
int main()
{
    while(scanf("%d",&n)==1){
        memset(val,0,sizeof(val));
        memset(head,-1,sizeof(head));
        tol=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&val[i]);
        for(int i=1;i<n;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            Add(x,y,z);
            Add(y,x,z);
        }
        dfs(1,0);
        ans=sum[1];
        dfs1(1,0);
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-11-04 17:12:53

HDU 3899 树形DP的相关文章

HDU 3899 树形DP||树上递推

JLUCPC 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3899 Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1375    Accepted Submission(s): 413 Problem Description Dr. Skywind and Dr. Walkonclo

hdu 4123 树形DP+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=4123 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses,

hdu 1250 树形DP

Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-07-27) Description There is going to be a party to celebrate the 80-th Anniversary of the Ural St

hdu 4276(树形dp)

题意:带权树上有起点终点每个点上有宝藏,一个人只有T分钟要从起点到重点,问你最多能收集多少宝藏. 思路:树形dp,首先判断能不能走到终点,然后把路径上的边权变为0时间减去所有边权.dp[v][j]表示从v出发回到v话费j分钟最多能收集到的宝藏. dp[v][j] = max(dp[v][j], dp[x][k] + dp[v][j-k-2*val]); 被G++卡了好长时间,换成c++就过了. 代码如下: 1 #include <stdio.h> 2 #include <string.h

hdu 5148 树形dp+分组背包问题

http://acm.hdu.edu.cn/showproblem.php?pid=5148 Problem Description Long long ago,there is a knight called JayYe.He lives in a small country.This country is made up of n cities connected by n-1 roads(that means it's a tree).The king wants to reward Ja

HDU 1520 树形dp裸题

1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b using nam

hdu 3586 树形dp+二分

题目大意:给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵 树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线.现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所 有前线与司令部联系所花费的总费用少于m时的最小limit.1<=n<=1000,1<=m<=100万 题目要问的是最小的最大限制,必然二分答案 然后对于每一个值,树形DP判定是否可行 dp[i]表示要切断以i为根的其它所有子树的最小代价. 其中设定叶子结点的代价为无穷大

HDU 1561 树形DP入门

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6011    Accepted Submission(s): 3555 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物

hdu 1561 树形dp

又一道树形dp,发现其实树形dp长得都挺像的. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int INF = -9999999; 7 const int N = 201; 8 int dp[N][N]; 9 int head[N]; 10 int value[N]; 11 int n, m, e; 12 13 void i