AcWing 1207. 8 大臣的旅费
原题链接
简单
作者:
QAQ_ning
,
2021-04-18 22:21:04
,
所有人可见
,
阅读 702
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
struct Edge
{
int id, w;
};
vector<Edge> h[N];
int dist[N];
void dfs(int u, int father, int distance)
{
dist[u] = distance;
for (auto node : h[u])
if (node.id != father)
dfs(node.id, u, distance + node.w);
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n - 1; i ++ )
{
int p, q, d;
scanf("%d%d%d", &p, &q, &d);
h[p].push_back({q, d});
h[q].push_back({p, d});
}
dfs(1, -1, 0);
int u = 1;
for (int i = 2; i <= n; i ++ )
if (dist[u] < dist[i]) u = i;
dfs(u, -1, 0);
int distance = 0;
for (int i = 1; i <= n; i ++ )
if (distance < dist[i]) distance = dist[i];
printf("%lld\n", distance * 10 + distance * (distance + 1ll)/2 ); //1ll使整个表达式的值变成long long类型
return 0;
}