HDU 6201 transaction transaction transaction(SPFA算法求最长路径)

时间:2021-10-09 22:46:05

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  ii ttcity. Kelukin will take taxi, whose price is  11 yuanyuan 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  ii thth number means the prices in  ii thth city;  (1Price10000)(1≤Price≤10000) 
then follows  n1n−1 lines, each contains three numbers  xxyy and  zz  which means there exists a road between  xx and  yy, the distance is  zz kmkm  (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,没敢动,比完赛听别人说用图论方法也可以做出来orz,想了很久也没相出来,还是搜博客做出来的,弱哭

思路:

假设一个源点和汇点,然后将源点与其他点建一条值为v的路,将汇点和其他点建一条值为-v的路,其他点之间的边就都为负的权值的路。。然后以源点为起点,汇点为终点用SPFA跑一遍最长路径就好了。。。这个建图太巧妙了,我是想不出来,这样建图,源点和汇点的最长路就是答案。。orz

还有就是我用静态数组的邻接表来写这题就无限TLE,估计是一个点连接的边特别的多,用动态的邻接表800ms就过了。。

代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
#define ll long long
#define INF 100861111
using namespace std;
struct edge
{
    int to;
    int v;
};
vector<edge>a[100005];
int vis[100005];
int dis[100005];
int n;
int ans;
void addEdge(int from,int to,int v)
{
    edge t;
    t.to=to;
    t.v=v;
    a[from].push_back(t);
}
void spfa()
{
    queue<int>q;
    int now,next;
    now=0;
    q.push(now);
    int i;
    while(!q.empty())
    {
        now=q.front();
        vis[now]=0;
        q.pop();
        for(i=0;i<a[now].size();i++)
        {
            next=a[now][i].to;
            if(dis[now]+a[now][i].v>dis[next])
            {
                dis[next]=dis[now]+a[now][i].v;
                if(!vis[next])
                {
                    vis[next]=1;
                    q.push(next);
                }
            }
        }
    }
    printf("%d\n",dis[n+1]);
}
int main()
{
    int i,j,m,test,d1,d2,v,minn,tar;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d",&n);
        ans=0;
        for(i=0;i<=n+1;i++)
        {
            a[i].clear();
            vis[i]=0;
            dis[i]=0;
        }
        for(i=1;i<=n;i++)
        {
            scanf("%d",&v);
            addEdge(0,i,v);
            addEdge(i,n+1,-v);
        }
        for(i=1;i<=n-1;i++)
        {
            scanf("%d%d%d",&d1,&d2,&v);
            addEdge(d1,d2,-v);
            addEdge(d2,d1,-v);
        }
        spfa();
    }
    return 0;
}