HDU 6201 transaction transaction transaction (树形DP or 拆点最短路)

16 篇文章 1 订阅
12 篇文章 2 订阅


transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1256    Accepted Submission(s): 601


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  i t  city. Kelukin will take taxi, whose price is  1 yuan  per km and this fare cannot be ignored.
There are  n1  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  ( 1T10 ) , the number of test cases. 
For each test case:
first line contains an integer  n  ( 2n100000 ) means the number of cities;
second line contains  n  numbers, the  i th  number means the prices in  i th  city;  (1Price10000)  
then follows  n1  lines, each contains three numbers  x y  and  z  which means there exists a road between  x  and  y , the distance is  z km   (1z1000)
 

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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6205  6204  6203  6202  6201 
 

题意:给你一颗树,边有权值,现在有本书,每个点都有这本书的价格,现在你可以选任意一点作为起点买这本书,跑去任意一点卖掉,过程中消耗路径的权值花费,问你任选起点和终点最大收益是多少。


思路:树形DP,dp[u][0]表示以u为根的子树中买一本书的最大收益,dp[u][1]表示以u为根的子树中卖一本书的最大收

益。dp[u][0]+dp[u][1]即为在以u为根的子树中选两点的最大收益。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 5;
struct node
{
    int to, w;
    node(){}
    node(int tt, int ww) : to(tt), w(ww){}
};
vector<node> v[maxn];
int n, dp[maxn][2], val[maxn], ans;
void dfs(int u, int f)
{
    dp[u][0] = val[u];
    dp[u][1] = -val[u];
    for(int i = 0; i < v[u].size(); i++)
    {
        int to = v[u][i].to;
        int w = v[u][i].w;
        if(to == f) continue;
        dfs(to, u);
        dp[u][0] = max(dp[u][0], dp[to][0]-w);
        dp[u][1] = max(dp[u][1], dp[to][1]-w);
    }
    ans = max(ans, dp[u][1]+dp[u][0]);
}
int main()
{
    int x, y, z, _;
    cin >> _;
    while(_--)
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            scanf("%d", &val[i]);
        for(int i = 1; i <= n; i++)
            v[i].clear();
        for(int i = 1; i <= n-1; i++)
        {
            scanf("%d%d%d", &x, &y, &z);
            v[x].push_back(node(y, z));
            v[y].push_back(node(x, z));
        }
        ans = 0;
        dfs(1, -1);
        printf("%d\n", ans);
    }
    return 0;
}

拆点加最长路

看到这题就是求未知起点的最长路,但是它有边权,也有点权啊怎么办,可以把点拆成入点和出点,然后构造源点S和终点T,然后这样连边(由于我用最长路求,显然记买入和路费为负,卖出为正):

<i,i+n,0>,自身拆点肯定要连;

<u+n,v,−dis> <v+n,u,−dis>,由于要求价值最大,花费显然要负权;

<S,i,−price[i]>,由于未知起点,那么直接把点都连到S上从S开始,并且这样刚好可以把点权转换成边权

<i+n,T,price[i]>,卖掉第i个后到T点。


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int INF = 1e9;
const int maxn = 2e5 + 5;
int dis[maxn], val[maxn], book[maxn], s, t, n;
struct node
{
    int val, to;
    node(){}
    node(int tt, int vv) : to(tt), val(vv){}
};
vector<node> v[maxn];
void init()
{
    for(int i = 0; i < maxn; i++)
        v[i].clear();
    s = 0, t = n*2+1;
}
int spfa(int u)
{
    memset(book, 0, sizeof(book));
    for(int i = 0; i < maxn; i++) dis[i] = -INF;
    dis[u] = 0;
    queue<int> q;
    q.push(u);
    book[u] = 1;
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        book[u] = 0;
        for(int i = 0; i < v[u].size(); i++)
        {
            int to = v[u][i].to, w = v[u][i].val;
            if(dis[to] < dis[u] + w)
            {
                dis[to] = dis[u] + w;
                if(!book[to])
                {
                    book[to] = 1;
                    q.push(to);
                }
            }
        }
    }
    return dis[t];
}
int main()
{
    int _, x, y, z;
    cin >> _;
    while(_--)
    {
        scanf("%d", &n);
        init();
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &val[i]);
            v[i].push_back(node(i+n, 0));
            v[s].push_back(node(i, -val[i]));
            v[i+n].push_back(node(t, val[i]));
        }
        for(int i = 1; i <= n-1; i++)
        {
            scanf("%d%d%d", &x, &y, &z);
            v[x].push_back(node(y, -z));
            v[y].push_back(node(x, -z));
        }
        printf("%d\n", spfa(s));
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值