zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    NETCore EF 数据库连接正确nuget和MySql错误异常
    JS 对象属性名排序
    NET 在一个数组中查找另一个数组所在起始位置(下标从0开始,未找到返回-1)
    NET 判断是否为回文
    NET/Regex 处理连续空格
    NET 已知excel表格前面26个是a到z,27是aa28是ab,以此类推,N是多少
    Regex 首字母转大写/小写,全大写,全小写
    .NETCore下访问img、js等静态资源404解决办法
    WPF-后台代码使用Behavior
    Socket-服务器端与客户端互相通信
  • 原文地址:https://www.cnblogs.com/yzm10/p/9589073.html
Copyright © 2011-2022 走看看