日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Highway (树的直径 + dfs)

發布時間:2024/1/18 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。