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

题意:

给定N个节点的点权Ti[i],N-1条边的权值,以某点为根的代价是,所有点的点权值*到树根的距离,找出最小的代价。

Hint:

树上递推

假设求得以a为树根的总代价,b是a的子节点,tot是树的总点权值,则以b为树根的代价,与a相比

多了tot-size[b] 少了size[a] 倍的edge(a,b)

递推写法:

#include<bits/stdc++.h>
#define Pii pair<int,long long>
#define fi first
#define se second
#define INF 0x7fffffff
#define pb push_back
#define mp make_pair
#define maxn 101000
#define LL long long
using namespace std;
vector<Pii> G[maxn];
int n,m,Ti[maxn],size[maxn];
LL tot,res=0,ans=0;
void dfs(int x,int fa,LL cnt){
    Pii now;
    size[x]=Ti[x];
    ans+=cnt*Ti[x];
    for(int i=0;i<G[x].size();i++){
        now=G[x][i];
        if(now.fi==fa) continue;
        dfs(now.fi,x,cnt+now.se);
        size[x]+=size[now.fi];
    }
}
void solve(int x,int fa,LL cnt){
    ans=min(ans,cnt);
    Pii now;
    for(int i=0;i<G[x].size();i++){
        now=G[x][i];
        if(now.fi==fa) continue;
        solve(now.fi,x,cnt+now.se*(tot-2*size[now.fi]));
    }
}
int main(){
    int T;
    int u,v,w;
    while(scanf("%d",&n)!=EOF){
        tot=0;
        for(int i=1;i<=n;i++) cin>>Ti[i],tot+=Ti[i];
        for(int i=1;i<n;i++){
            cin>>u>>v>>w;
            G[u].pb(mp(v,w));
            G[v].pb(mp(u,w));
        }
        ans=0;
        dfs(1,-1,0);
        solve(1,-1,ans);
        cout<<ans<<endl;
        for(int i=1;i<=n;i++) G[i].clear();
    }
    return 0;
}

DP写法:

 1 #include<bits/stdc++.h>
 2 #define Pii pair<int,long long>
 3 #define fi first
 4 #define se second
 5 #define INF 0x7fffffff
 6 #define pb push_back
 7 #define mp make_pair
 8 #define maxn 101000
 9 #define LL long long
10 using namespace std;
11 vector<Pii> G[maxn];
12 int n,m,Ti[maxn],size[maxn];
13 LL tot,res=0,ans=0,dp[maxn];
14 void dfs(int x,int fa,LL cnt){
15     Pii now;
16     size[x]=Ti[x];
17     ans+=cnt*Ti[x];
18     for(int i=0;i<G[x].size();i++){
19         now=G[x][i];
20         if(now.fi==fa) continue;
21         dfs(now.fi,x,cnt+now.se);
22         size[x]+=size[now.fi];
23     }
24 }
25 void solve(int x,int fa){
26     Pii now;
27     for(int i=0;i<G[x].size();i++){
28         now=G[x][i];
29         if(now.fi==fa) continue;
30         dp[now.fi]=dp[x]+now.se*(tot-2*size[now.fi]);
31         solve(now.fi,x);
32     }
33 }
34 int main(){
35     int T;
36     int u,v,w;
37     while(scanf("%d",&n)!=EOF){
38         tot=0;
39         for(int i=1;i<=n;i++) cin>>Ti[i],tot+=Ti[i];
40         for(int i=1;i<n;i++){
41             cin>>u>>v>>w;
42             G[u].pb(mp(v,w));
43             G[v].pb(mp(u,w));
44         }
45         ans=0;
46         dfs(1,-1,0);
47         dp[1]=ans;
48         solve(1,-1);
49         for(int i=1;i<=n;i++) ans=min(ans,dp[i]);
50         cout<<ans<<endl;
51         for(int i=1;i<=n;i++) G[i].clear(),dp[i]=0;
52     }
53     return 0;
54 }
时间: 2024-10-20 01:05:55

HDU 3899 树形DP||树上递推的相关文章

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 Pro

hdu 1284 钱币兑换问题 (递推 || DP || 母函数)

钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5069    Accepted Submission(s): 2868 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Outpu

[ACM] hdu 3853 LOOPS (概率DP,递推)

LOOPS Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help her friend Madoka save the world. But because of the plot of the Boss Incubator, she is trapped in a labyrinth called LOOPS. The planform of the

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 4869 Turn the pokers(递推&amp;组合数学&amp;逆元)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1279    Accepted Submission(s): 466 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t

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 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 2050 折线分割平面 (递推)

折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15709    Accepted Submission(s): 10836 Problem Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面