XTOJ1267 Highway 【最小生成树】
来源:互联网 发布:mac与iphone照片同步 编辑:程序博客网 时间:2024/05/21 15:00
HighwayAccepted : 100 Submit : 336Time Limit : 4000 MS Memory Limit : 65536 KBHighwayIn 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 y using 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.1≤n≤1051≤ai,bi≤n1≤ci≤108The number of test cases does not exceed 10.OutputFor each test case, output an integer which denotes the result.Sample Input51 2 21 3 12 4 23 5 151 2 21 4 13 4 14 5 2Sample Output1915SourceXTU OnlineJudge
这OJ接受%I64d %lld会报WA!!!
模仿kurskal 把边从大到小依次加进生成树里即可
随便一个点出发dfs ,得到最远的一个点为x
从x出发dfs,得到最远的点为y
那dis[x][y]是树中距离最远的2点距离
(x,y)也是数的直径
同时,树中任意一点k,距离其他点的最远距离必定<=max(dis[k][x],dis[k][y])
因此 求出x,y的dfs,模拟kurskal即可
#include<stdio.h>#include <iostream>#include<stdlib.h>#include<algorithm>#include<vector>#include<deque>#include<map>#include<set>#include<queue>#include<math.h>#include<string.h>#include<string>using namespace std;#define ll long long#define pii pair<int,int>const int inf = 1e9 + 7;const int N = 1e5+5;struct Edge{ int v,w,next;}edge[2*N];int head[N];inline void addEdge(int k,int u,int v,int w){ edge[k].v=v; edge[k].w=w; edge[k].next=head[u]; head[u]=k;}ll dis1[N];ll dis2[N];void dfs(int u,int fa,ll*dis){ for(int i=head[u];i!=-1;i=edge[i].next){ const int&v=edge[i].v; const int&w=edge[i].w; if(v!=fa){ dis[v]=dis[u]+w; dfs(v,u,dis); } }}pii init(int n){ fill(dis1,dis1+n+1,0); dfs(1,-1,dis1); ll maxDis=0; int idx=0; for(int i=1;i<=n;++i){ if(dis1[i]>maxDis){ maxDis=dis1[i]; idx=i; } } fill(dis1,dis1+n+1,0); dfs(idx,-1,dis1); int idx2=0; maxDis=0; for(int i=1;i<=n;++i){ if(dis1[i]>maxDis){ maxDis=dis1[i]; idx2=i; } } fill(dis2,dis2+n+1,0); dfs(idx2,-1,dis2); return make_pair(idx,idx2);}ll slove(int n){ if(n==1){ return 0; } pii r=init(n); ll ans=dis1[r.second]; for(int i=1;i<=n;++i){ if(i==r.first||i==r.second){ continue; } ans+=max(dis1[i],dis2[i]); } return ans;}int main(){ //freopen("/home/lu/Documents/r.txt","r",stdin); //freopen("/home/lu/Documents/w.txt","w",stdout); int n; while(~scanf("%d",&n)){ fill(head,head+n+1,-1); for(int i=0;i<n-1;++i){ int a,b,c; scanf("%d%d%d",&a,&b,&c); addEdge(2*i,a,b,c); addEdge(2*i+1,b,a,c); } printf("%I64d\n",slove(n)); } return 0;}
阅读全文
0 0
- XTOJ1267 Highway 【最小生成树】
- 最小生成树--highway(poj 2485);
- zoj 3946 Highway Project (spfa + 最小生成树)
- Highway
- 最小比例 最小生成树
- 最小生成树&&次最小生成树
- 2017ccpc全国邀请赛(湖南湘潭) H. Highway (最大生成树)(树的直径)
- 最小生成生成树计数
- 树+最小生成树
- 最小生成树
- 最小生成树 MST
- 最小生成树Kruskal
- kruskal 最小生成树
- 最小生成树
- 最小生成树
- 最小生成树
- 最小生成树
- 最小生成树 MST
- Java的基本数据类型有几种?分别是?
- Genymotion virtualbox 提示错误 (VERR_PDM_DRIVER_NOT_FOUND).
- mongodb C++ driver编译报错解决方案
- 『ORACLE』 DG性能转换(11g)
- 移动web开发经验总结
- XTOJ1267 Highway 【最小生成树】
- Delphi实现防止程序多次运行
- 36. Valid Sudoku
- 智能补全模糊查询select2的下拉选择框使用
- 索引
- php基础知识 12,文件操作函数库
- Scala 开发环境搭建和基础教程
- Ubuntu 16.04 OpenCV多版本冲突
- 909422229_JSP页面动态下拉菜单Ajax