HDU

来源:互联网 发布:淘宝如何使用分期付款 编辑:程序博客网 时间:2024/06/07 07:23

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 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 ith number means the prices in ith city; (1Price10000) 
then follows n1 lines, each contains three numbers xy and z which means there exists a road between x and y, the distance is zkm (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 301 3 23 4 10
 

Sample Output
8
 

Source
2017 ACM/ICPC Asia Regional Shenyang Online
 


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


解题思路: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;}




原创粉丝点击