zoukankan      html  css  js  c++  java
  • hdu 6201 transaction transaction transaction

    Problem Description
    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 ai yuan in it city. Kelukin will take taxi, whose price is 1yuan per km and this fare cannot be ignored.
    There are n−1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.

     
    Input
    The first line contains an integer T (1≤T≤10) , the number of test cases. 
    For each test case:
    first line contains an integer n (2≤n≤100000) means the number of cities;
    second line contains n numbers, the ith number means the prices in ith city; (1≤Price≤10000) 
    then follows n−1 lines, each contains three numbers x, y and z which means there exists a road between x and y, the distance is zkm (1≤z≤1000). 
     
    Output
    For 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
    Source
     
     
    1、dp
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 #define N 100006
     6 int n;
     7 int dp[N*2];
     8 int a[N];
     9 struct Node{
    10     int x,y,c;
    11 }node[N*2];
    12 int main()
    13 {
    14     int t;
    15     scanf("%d",&t);
    16     while(t--){
    17         scanf("%d",&n);
    18         for(int i=1;i<=n;i++){
    19             scanf("%d",&a[i]);
    20         }
    21         memset(dp,0,sizeof(dp));
    22         int k=0;
    23         for(int i=1;i<n;i++){
    24             scanf("%d%d%d",&node[k].x,&node[k].y,&node[k].c);
    25             k++;
    26             node[k].x = node[k-1].y;
    27             node[k].y = node[k-1].x;
    28             node[k].c = node[k-1].c;
    29             k++;
    30         }
    31         for(int i=0;i<k;i++){
    32             dp[node[i].y] = max(dp[node[i].y],dp[node[i].x] + a[node[i].y] - a[node[i].x] - node[i].c);
    33         }
    34         int res = -1;
    35         for(int i=1;i<=n;i++){
    36             
    37             res = max(res,dp[i]);
    38         }
    39         printf("%d
    ",res);
    40     }
    41     return 0;
    42 } 

    2、dfs暴搜+剪枝

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<vector>
     5 using namespace std;
     6 #define N 100006
     7 int n;
     8 int a[N];
     9 vector< pair<int , int> > v[N];
    10 int dfs(int num,int fa){
    11     int ans = 0;
    12     for(int i=0;i<v[num].size();i++){
    13         int temp = a[v[num][i].first]- a[num] - v[num][i].second;
    14         if(ans + temp < 0 || v[num][i].first == fa) continue;
    15         ans = max(ans,temp + dfs(v[num][i].first,num));
    16     }
    17     return ans;
    18 }
    19 int main()
    20 {
    21     int t;
    22     scanf("%d",&t);
    23     while(t--){
    24         scanf("%d",&n);
    25         for(int i=1;i<=n;i++){
    26             scanf("%d",&a[i]);
    27             v[i].clear();
    28         }
    29         int x,y,cost;
    30         for(int i=1;i<n;i++){
    31             scanf("%d%d%d",&x,&y,&cost);
    32             v[x].push_back(make_pair(y,cost));
    33             v[y].push_back(make_pair(x,cost));
    34         }
    35         int res = -1;
    36         for(int i=1;i<=n;i++){
    37             res = max(res,dfs(i,0));
    38         }
    39         printf("%d
    ",res);
    40         
    41     }
    42     return 0;
    43 }

    此外,用最短路知识应该也能过,有兴趣的可以自己试试,个人认为dp最简单、高效

     
     
     
  • 相关阅读:
    201521123074 《Java程序设计》第5周学习总结
    201521123074 《Java程序设计》第4周学习总结
    201521123074 《Java程序设计》第3周学习总结
    201521123005 《Java程序设计》 第十四周学习总结
    201521123005《Java程序设计》第十三周学习总结
    201521123005 《Java程序设计》 第十二周学习总结
    201521123005 《Java程序设计》 第十一周学习总结
    201521123005 《Java程序设计》 第十周学习总结
    201521123005 《Java程序设计》 第九周学习总结
    201521123005 《java程序设计》 第八周学习总结
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/7570809.html
Copyright © 2011-2022 走看看