2017 ACM/ICPC Asia Regional Shenyang transaction transaction transaction

来源:互联网 发布:数学手册 知乎 编辑:程序博客网 时间:2024/06/09 23:44

transaction transaction transaction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1454 Accepted Submission(s): 700

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 n−1 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 (1≤T≤10) , the number of test cases.
For each test case:
first line contains an integer n (2≤n≤100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1≤Price≤10000)
then follows n−1 lines, each contains three numbers x, y and z which means there exists a road between x and y, the distance is zkm (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

Source
2017 ACM/ICPC Asia Regional Shenyang Online

Recommend
liuyiding | We have carefully selected several similar problems for you: 6205 6204 6203 6202 6201

这道题目是队友做出来的,我看了好多题解,几乎都是spfa,这里写个Bellman_Ford~(队友的思路)

#include <bits/stdc++.h>#define INF 0x3f3f3f3fusing namespace std;struct node{   int u, v, w;}t[567890];int n, top;int a[121211];int dis[121211];void Bellman_Ford()//我开始的想法是再加一层循环,其实根本就不用,排个序比较大小就行了{    memset(dis, 0, sizeof(dis));    dis[0] = 0;    int f;    for(int i=1;i<n;i++)    {       f = 0;       for(int j=0;j<top;j++)       {          if(dis[t[j].v]<dis[t[j].u]+t[j].w)          {          dis[t[j].v] = dis[t[j].u]+t[j].w;          f  = 1;          }       }       if(f==0)      break;    }     sort(dis, dis+n+1);//排序     cout<<dis[n]<<endl;//选择最大值}int main(){   int T;   scanf("%d", &T);   while(T--)   {     scanf("%d", &n);     for(int i=1;i<=n;i++)     {       scanf("%d", &a[i]);     }     top = 0;//记录所加的边数     int x, y, z;     for(int i=1;i<n;i++)     {       scanf("%d %d %d", &x, &y, &z);       t[top].u = x;       t[top].v = y;       t[top].w = a[y] - a[x] - z;//带权值       top++;       t[top].u = y;       t[top].v = x;       t[top].w = a[x] - a[y] - z;       top++;     }     Bellman_Ford();   }  return 0;}
阅读全文
0 0