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

55 篇文章 0 订阅
11 篇文章 0 订阅

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


题解:

比赛的时候没写出这一题。。因为一直觉得是树形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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值