HDU6201

transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 706    Accepted Submission(s): 357

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 30 1 3 2 3 4 10
 

 

Sample Output

8
 

 

Source

 

建立源点和汇点。

源点连所有的树上点, 边权为 -a[i],表示起点,需要花费a[i],所有树上点在连接 汇点, 边权为a[i],表示收益为a[i],然后在根据树建图,边权为-w,表示花费w。 

然后spfa跑个最长路,答案为dis[汇点]。

 

 1 //2017-09-11
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <queue>
 7 
 8 using namespace std;
 9 
10 const int N = 110000;
11 const int INF = 0x3f3f3f3f;
12 int head[N], tot;
13 struct Edge{
14     int v, w, next;
15 }edge[N<<2];
16 
17 void init(){
18     tot = 0;
19     memset(head, -1, sizeof(head));
20 }
21 
22 void add_edge(int u, int v, int w){
23     edge[tot].v = v;
24     edge[tot].w = w;
25     edge[tot].next = head[u];
26     head[u] = tot++;
27 }
28 
29 bool vis[N];
30 int dis[N];
31 int cnt[N];
32 deque<int> dq;
33 bool spfa(int s, int n){
34     memset(vis, 0, sizeof(vis));
35     memset(cnt, 0, sizeof(cnt));
36     for(int i = 0; i <= n+1; i++)
37           dis[i] = -INF;
38     vis[s] = 1;
39     dis[s] = 0;
40     cnt[s] = 1;
41     deque<int> dq;
42     dq.push_back(s);
43     while(!dq.empty()){
44         int u = dq.front();
45         dq.pop_front();
46         vis[u] = 0;
47         for(int i = head[u]; i != -1; i = edge[i].next){
48             int v = edge[i].v;
49             if(dis[v] < dis[u] + edge[i].w){
50                 dis[v] = dis[u] + edge[i].w;
51                 if(!vis[v]){
52                     vis[v] = 1;
53                     dq.push_back(v);
54                     if(++cnt[v] > n)return false;
55                 }
56             }
57         }
58     }
59     return true;
60 }
61 
62 int arr[N], n;
63 
64 int main()
65 {
66     int T;
67     scanf("%d", &T);
68     while(T--){
69         scanf("%d", &n);
70         init();
71         int s = 0, t = n+1;
72         for(int i = 1; i <= n; i++){
73             scanf("%d", &arr[i]);
74             add_edge(s, i, -arr[i]);
75             add_edge(i, t, arr[i]);
76         }
77         int u, v, w;
78         for(int i = 0; i < n-1; i++){
79             scanf("%d%d%d", &u, &v, &w);
80             add_edge(u, v, -w);
81             add_edge(v, u, -w);
82         }
83         spfa(s, n);
84         printf("%d\n", dis[t]);
85     }
86 
87     return 0;
88 }

 

转载于:https://www.cnblogs.com/Penn000/p/7505936.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值