• HDU


    transaction transaction transaction

    Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell. 
    As we know, the price of this book was different in each city. It is aiai yuanyuan in iittcity. Kelukin will take taxi, whose price is 11yuanyuan per km and this fare cannot be ignored. 
    There are n1n−1 roads connecting nn cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get. 

    InputThe first line contains an integer TT (1T101≤T≤10) , the number of test cases. 
    For each test case: 
    first line contains an integer nn (2n1000002≤n≤100000) means the number of cities; 
    second line contains nn numbers, the iithth number means the prices in iithth city; (1Price10000)(1≤Price≤10000) 
    then follows n1n−1 lines, each contains three numbers xx, yy and zz which means there exists a road between xx and yy, the distance is zzkmkm (1z1000)(1≤z≤1000). 
    OutputFor each test case, output a single number in a line: the maximum money he can get. 
    Sample Input

    1  
    4  
    10 40 15 30  
    1 2 30
    1 3 2
    3 4 10

    Sample Output

    8





    一个人在任意点买书(消费点权),经过边(花费边权),在任意点卖书(收获点权),求最大收益。
    树形结构,因为父子关系未知,所以双向建边。
    dp[i][0]表示i子树中的最少买书消费,dp[i][1]表示i子树中的最大卖书收益,
    dp[i][0]+dp[i][1]表示以i为最近公共父节点的最大收益,最优解并非根节点,因此ans需要不断更新。



    #include<bits/stdc++.h>
    #define MAX 100005
    #define INF 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    
    int a[MAX];
    int dp[MAX][2];
    int ans;
    struct Node{
        int v,w;
    }node;
    vector<Node> v[MAX];
    
    void dfs(int x,int pre){
        dp[x][0]=-a[x];dp[x][1]=a[x];
        for(int i=0;i<v[x].size();i++){
            int to=v[x][i].v;
            if(to==pre) continue;
            int w=v[x][i].w;
            dfs(to,x);
            dp[x][0]=max(dp[x][0],dp[to][0]-w);
            dp[x][1]=max(dp[x][1],dp[to][1]-w);
        }
        ans=max(ans,dp[x][0]+dp[x][1]);     //不断更新
    }
    int main()
    {
        int t,n,i;
        int x,y,w;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            for(i=1;i<=n;i++){
                scanf("%d",&a[i]);
                v[i].clear();
            }
            for(i=1;i<n;i++){
                scanf("%d%d%d",&x,&y,&w);
                node.v=y;node.w=w;
                v[x].push_back(node);
                node.v=x;
                v[y].push_back(node);
            }
            ans=-INF;
            dfs(1,-1);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    gulp中glob的匹配规则
    ES6————let和const命令
    location
    section,div,artical的区别
    Mysql基本语法
    Nodejs基础中间件Connect
    springMVC结合Jcrop实现头像上传裁剪预览功能--javaweb修订版
    Nodejs Express 4.X 中文API 4--- Router篇
    Nodejs Express 4.X 中文API 3--- Response篇
    Nodejs Express 4.X 中文API 2--- Request篇
  • 原文地址:https://www.cnblogs.com/yzm10/p/9589073.html
Copyright © 2020-2023  润新知