(树的直径)第九届湘潭市大学生程序设计比赛 H-Highway
Posted 惜取少年时
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(树的直径)第九届湘潭市大学生程序设计比赛 H-Highway相关的知识,希望对你有一定的参考价值。
Highway |
||
Accepted : 25 | Submit : 104 | |
Time Limit : 4000 MS | Memory Limit : 65536 KB |
HighwayIn ICPCCamp there were n towns conveniently numbered with 1,2,…,n connected with (n?1) roads. The i-th road connecting towns ai and bi has length ci. 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) cents, where δ(x,y) is the length of the shortest path between towns x and yusing roads. As Bobo is rich, he would like to find the most expensive way to build the (n?1) highways. InputThe 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 ai, bi and ci.
OutputFor each test case, output an integer which denotes the result. Sample Input5 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 Sample Output19 15 |
长见识的一道题,第一次听说树的直径这一概念,百度了一下发现还是很强大的。
对所有点,加上这一点到直径两端点的距离中大的一个即可,最后再减去一个直径(因为被重复算了一次)即可。
(代码全是了解直径这一概念之后现写的,可能会比较繁琐)
#include <iostream> #include <string> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> #include <queue> #include <set> #include <map> #include <list> #include <stack> #define mp make_pair typedef long long ll; typedef unsigned long long ull; const int MAX=1e5+5; const int INF=1e9+5; const double M=4e18; using namespace std; const int MOD=1e9+7; typedef pair<int,int> pii; typedef pair<int,long long> pil; const double eps=0.000000001; int n; vector<pil> edge[MAX]; int a,b; ll c; ll d[MAX]; bool vi[MAX]; int lo1,lo2; ll oh; int findlong(int st) { memset(vi,false,sizeof(vi)); vi[st]=true; queue<pil> que; ll dismax=0,dis; int an,tem; que.push(mp(st,0LL)); while(!que.empty()) { tem=que.front().first; dis=que.front().second; pil lin; que.pop(); for(int i=0;i<edge[tem].size();i++) { lin=edge[tem][i]; if(!vi[lin.first]) { vi[lin.first]=true; if(dismax<dis+lin.second) { dismax=dis+lin.second; an=lin.first; } que.push(mp(lin.first,dis+lin.second)); } } } oh=dismax; return an; } void dfs(int st) { memset(vi,false,sizeof(vi)); vi[st]=true; queue<pil> que; int tem; ll dis; que.push(mp(st,0LL)); while(!que.empty()) { tem=que.front().first; dis=que.front().second; pil lin; que.pop(); d[tem]=max(d[tem],dis); for(int i=0;i<edge[tem].size();i++) { lin=edge[tem][i]; if(!vi[lin.first]) { vi[lin.first]=true; que.push(mp(lin.first,dis+lin.second)); } } } } ll finan; int main() { while(~scanf("%d",&n)) { if(n==1) {printf("0\n");continue;} oh=0LL; for(int i=1;i<=n;i++) { edge[i].clear(); d[i]=0LL; } for(int i=1;i<n;i++) { scanf("%d%d%I64d",&a,&b,&c); edge[a].push_back(mp(b,c)); edge[b].push_back(mp(a,c)); } lo1=findlong(1); lo2=findlong(lo1); dfs(lo1);dfs(lo2); finan=0; for(int i=1;i<=n;i++) { finan+=d[i]; } finan-=oh; cout<<finan<<"\n"; } }
以上是关于(树的直径)第九届湘潭市大学生程序设计比赛 H-Highway的主要内容,如果未能解决你的问题,请参考以下文章
湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator
“浪潮杯”第九届山东省ACM大学生程序设计竞赛重现赛 C-Cities