Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J
Description
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
The first line of the input contains the number of friends n (3 ≤ n ≤ 100). The next n - 1 lines each contain three space-separated integers u, vand c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and vis c.
It is guaranteed that the social network of the input forms a tree.
Output a single integer – the maximum sum of costs.
4
0 1 4
0 2 2
2 3 3
5
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
105
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
5551
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).
题意:通俗一点,求树的每条分支的和,把最大的拿出来
解法:dfs,遍历到叶子就比较一下喽
1 #include <bits/stdc++.h> 2 using namespace std; 3 int n; 4 vector<pair<int,int>>q[1234]; 5 int sum; 6 int vis[1234]; 7 void dfs(int x,int cot,int pre) 8 { 9 if(vis[x]) 10 { 11 return; 12 } 13 vis[x]=1; 14 for(int i=0; i<q[x].size(); i++) 15 { 16 if(vis[q[x][i].first]) 17 { 18 { 19 sum=max(sum,cot); 20 continue; 21 } 22 } 23 dfs(q[x][i].first,cot+q[x][i].second,x); 24 } 25 } 26 int main() 27 { 28 cin>>n; 29 for(int i=1; i<n; i++) 30 { 31 int v,u,c; 32 cin>>v>>u>>c; 33 q[v].push_back({u,c}); 34 q[u].push_back({v,c}); 35 } 36 dfs(0,0,0); 37 cout<<sum<<endl; 38 return 0; 39 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~