HDU - 6201— transaction transaction transaction (最短路)

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 iittcity. Kelukin will take taxi, whose price is 11yuanyuan per km and this fare cannot be ignored. 
There are n−1n−1 roads connecting nn 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 TT (1≤T≤101≤T≤10) , the number of test cases. 
For each test case: 
first line contains an integer nn (2≤n≤1000002≤n≤100000) means the number of cities; 
second line contains nn numbers, the iithth number means the prices in iithth city; (1≤Price≤10000)(1≤Price≤10000) 
then follows n−1n−1 lines, each contains three numbers xx, yy and zz which means there exists a road between xx and yy, the distance is zzkmkm (1≤z≤1000)(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

以后做最短路一定要思考一个超级汇点啊。。。。

题目大意:

无向图,边权表示路费,每点还有一个val表示改点书的价格。只能选两个点,求能获取的最大利润。

思路:

假设超级原点,与所有点连线的边权为改点的书的价格。spfa求出的最短路其实是成本,然后遍历所有点求最大利润。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e5+10;

struct node
{
    int to,w,next;
};
node Map[3*maxn];
int head[maxn];
int dist[maxn],vis[maxn],n,a[maxn];

void add(int u,int v,int w,int &cnt)
{
    Map[cnt].to = v;
    Map[cnt].w = w;
    Map[cnt].next = head[u];
    head[u] = cnt++;
}
void SPFA(int u)
{
    memset(vis,0,sizeof vis);
    memset(dist,INF,sizeof dist);
    queue<int> q;
    q.push(u);
    vis[u]=1;
    dist[u]=0;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];~i;i=Map[i].next)
        {
            int to=Map[i].to,w=Map[i].w;
            if(dist[to]>dist[u]+w)
            {
                dist[to]=dist[u]+w;
                if(!vis[to])
                {
                    vis[to]=1;
                    q.push(to);
                }
            }
        }
    }
    int ans = 0;
    for(int i=1;i<=n;i++)
    {
        ans=max(ans,a[i]-dist[i]);
    }
    cout<<ans<<endl;
}
int main()
{
    int t,i,u,v,w;
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        cin>>n;
        int cnt=0;
        memset(head,-1,sizeof head);
        memset(a,0,sizeof a);
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            add(0,i,a[i],cnt);

        }
        for(i=1;i<n;i++)
        {
            cin>>u>>v>>w;
            add(u,v,w,cnt);
            add(v,u,w,cnt);
        }
        SPFA(0);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值