Highway (树的直径 + dfs)
鏈接:https://ac.nowcoder.com/acm/contest/1109/H
來源:牛客網
?
時間限制:C/C++ 3秒,其他語言6秒
空間限制:C/C++ 32768K,其他語言65536K
Special Judge, 64bit IO Format: %lld
題目描述
In ICPCCamp there were n towns conveniently numbered with 1,2,…,n1, 2, \dots, n1,2,…,n
connected with (n - 1) roads.
The i-th road connecting towns aia_iai? and bib_ibi? has length cic_ici?.
It is guaranteed that any two cities reach each other using only roads.
Bobo would like to build (n - 1) highways so that any two towns reach each using *only highways*.
Building a highway between towns x and y costs him δ(x,y)\delta(x, y)δ(x,y) cents,
where δ(x,y)\delta(x, y)δ(x,y) is the length of the shortest path between towns x and y using roads.
As Bobo is rich, he would like to find the most expensive way to build the (n - 1) highways.
輸入描述:
The input contains zero or more test cases and is terminated by end-of-file. For each test case:The first line contains an integer n. The i-th of the following (n - 1) lines contains three integers aia_iai?, bib_ibi? and cic_ici?.* 1≤n≤1051 \leq n \leq 10^51≤n≤105 * 1≤ai,bi≤n1 \leq a_i, b_i \leq n1≤ai?,bi?≤n * 1≤ci≤1081 \leq c_i \leq 10^81≤ci?≤108 * The number of test cases does not exceed 10.輸出描述:
For each test case, output an integer which denotes the result.示例1
輸入
復制
5 1 2 2 1 3 1 2 4 2 3 5 1 5 1 2 2 1 4 1 3 4 1 4 5 2輸出
復制
19 15? ? ? ? ? ? ?建立一棵最大生成樹樹B,其中每條邊a,b的距離為現在的樹A上的a,b點的最短距離。
? ? ? ? ? ? ?明顯能夠想到樹上各個點到A的直徑上兩個點其中之一是距離最大的。那么就選擇點i和兩個直徑點之一的那條邊就行。
#include<bits/stdc++.h> using namespace std; const int M = 100010; struct fuck {int v; long long d; }; vector<fuck>G[M]; long long dis[M], udis[M]; long long maxn = -1;int pos = 0; void dfs(int x,int fa,long long dis[]) {for (auto v : G[x]) {if (v.v == fa)continue;dis[v.v] = dis[x] + v.d;if (dis[v.v] > maxn) {maxn = dis[v.v];pos = v.v;}dfs(v.v, x, dis);} } int main() {ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);int n;while (cin >> n) {for (int i = 0; i <= n; i++) G[i].clear();for (int i = 1; i < n; i++) {int a, b; long long c;cin >> a >> b >> c;G[a].push_back(fuck{ b,c }); G[b].push_back(fuck{ a,c });}if (n == 1) {cout << 0 << "\n";continue;}for (int i = 0; i <= n; i++)dis[i] = -1;dis[1] = 0;maxn = -1, pos = 0;dfs(1, 0, dis);int zj1 = pos;for (int i = 0; i <= n; i++)dis[i] = -1;dis[zj1] = 0;maxn = -1, pos = 0;dfs(zj1, 0, dis);int zj2 = pos;for (int i = 0; i <= n; i++)udis[i] = -1;udis[zj2] = 0;maxn = -1, pos = 0;dfs(zj2, 0, udis);long long res = dis[zj2];for (int i = 1; i <= n; i++) {if (i == zj1 || i == zj2)continue;res += max(dis[i], udis[i]);}cout << res << "\n";}return 0; }? ? ? ? ? ? ? ? ? ?
?
?
總結
以上是生活随笔為你收集整理的Highway (树的直径 + dfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java从入门到精通 第22章 多线程
- 下一篇: 网络扫描软件试用体验