HDU - 6201 transaction transaction transaction (2017 ACM-ICPC 亚洲区 (沈阳赛区) 网络赛 1008)

本文介绍了一道关于商人Kelukin在不同城市买卖书籍以获取最大利润的问题,并通过SPFA算法解决。该问题考虑了各城市间不同的书籍价格及旅行成本。

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


题意:给你一棵树,书上每个节点代表一个城市,有一本书,每个城市的售价不一样,问你选择两个城市,一个城市买,一个城市卖,能获得的最大利润是多少,树的边的权值就是路费,也要算进去。


解题思路:n遍spfa就过了……比赛时没想到,一直以为这是n平方的算法,实际上不是,因为在前面你求得最短路的时候,后面的spfa实际上不用执行n遍,即很少结点会入队……spfa就会退化变成一个动态规划算法了……另外,因为是树,不用判负环,之前一直负环,导致多了一个数组的初始化,一直超时……然后插入边的时候权值稍作修改,可以节省代码,详见代码。




#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
using namespace std;
const int maxn = 100500;
inline void scan_d(int &ret){
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}

struct edge{
    int v1,v2,w,next;
}e[2*maxn];//这里要开两倍……否则超时……醉了

int N;
int edge_num;
int head[maxn];
int d[maxn];

int price[maxn];

void insert_edge(int v1,int v2,int w){
    e[edge_num].v1=v1;
    e[edge_num].v2=v2;
    e[edge_num].w=w;
    e[edge_num].next=head[v1];
    head[v1]=edge_num++;

}



void spfa(int s){
    queue<int> que;
    que.push(s);

    while(!que.empty()){
        int tp=que.front();
        que.pop();

        for(int i=head[tp];i!=-1;i=e[i].next)
            if(d[tp]+e[i].w>d[e[i].v2]){//求最大路
                d[e[i].v2]=d[tp]+e[i].w;
                que.push(e[i].v2);

            }
    }
}


int main ()
{
    int t;
    scanf("%d",&t);

    while(t--){
        scan_d(N);

        memset(d,0,sizeof(d));
        memset(head,-1,sizeof(head));

        for(int i=1;i<=N;i++){
            scan_d(price[i]);
        }

        int u, v, ds;

        edge_num=0;

        for(int i=1;i<=N-1;i++){
            scan_d(u);
            scan_d(v);
            scan_d(ds);

            insert_edge(u, v, -ds-price[u]+price[v]);//实际上就是在u买书,v卖书,直接算进权值就好了
            insert_edge(v, u, -ds-price[v]+price[u]);

        }

        for(int i=1;i<=N;i++)
            spfa(i);

        int ans=0;

        for(int i=1;i<=N;i++)
            ans=max(ans,d[i]);

        printf("%d\n",ans);


    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值