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 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.
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).
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
建立源点和汇点。
源点连所有的树上点, 边权为 -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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2016-09-11 HDU5875
2016-09-11 HDU5873
2016-09-11 HDU5874