2017湘潭赛XTU1267Highway
来源:互联网 发布:u盘低格后数据恢复 编辑:程序博客网 时间:2024/05/18 11:48
Highway
Accepted : 169 Submit : 577Time Limit : 4000 MS Memory Limit : 65536 KBHighway
In ICPCCamp there were
Bobo would like to build
As Bobo is rich, he would like to find the most expensive way to build the
Input
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
1≤n≤105 1≤ai,bi≤n 1≤ci≤108 - The number of test cases does not exceed
10 .
Output
For each test case, output an integer which denotes the result.
Sample Input
51 2 21 3 12 4 23 5 151 2 21 4 13 4 14 5 2
Sample Output
1915
Source
XTU OnlineJudgehttp://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1267
本题的题意是真的坑爹 告诉你一颗树 你就可以求出两两的距离 再用这些距离去构造最大生成树 4到5的距离为6
此题需要知道一个东西 就是树的直径 http://www.cnblogs.com/xubenben/archive/2012/12/28/2837971.html
#include<stdio.h>#include<vector>#include<queue>#include<string.h>using namespace std;#define ll long longbool vis[100010];ll gg[100010];ll gg1[2][100010];int pp;ll total;int qd;struct node{ int yd,val,et;};vector<node>ap[100010];vector<node>ap1[100010];int bfs(int ss){ int zd; queue<int>qe; qe.push(ss); vis[ss]=1; ll maxx=0; while(!qe.empty()) { int dt=qe.front();//原点 qe.pop(); for(int i=0; i<ap[dt].size(); i++) { node ft=ap[dt][i]; if(vis[ft.et]) continue; vis[ft.et]=1; gg[ft.et]=gg[dt]+ap[dt][i].val; qe.push(ft.et); if(maxx<gg[ft.et]) { maxx=gg[ft.et]; zd=ft.et; } } } return zd;}void dfs(int k,ll sum){ for(int i=0; i<ap1[k].size(); i++) { int vv=ap1[k][i].et; if(vis[vv]) continue; vis[vv]=1; dfs(vv,sum+ap1[k][i].val); } if(total<sum) { total=sum; qd=k; }}void dfs1(int k,ll sum){ for(int i=0; i<ap1[k].size(); i++) { int vv=ap1[k][i].et; if(vis[vv]) continue; vis[vv]=1; gg1[pp][vv]=sum+ap1[k][i].val; dfs1(vv,sum+ap1[k][i].val); }}int main(){ int n,i; while(scanf("%d",&n)!=EOF) { for(i=0; i<=n; i++) { ap[i].clear(); ap1[i].clear(); } node xf; int u,v,w; for(i=1; i<n; i++) { scanf("%d %d %d",&u,&v,&w); xf.yd=u; xf.et=v; xf.val=w; ap[u].push_back(xf); ap1[u].push_back(xf); xf.yd=v; xf.et=u; ap[v].push_back(xf); ap1[v].push_back(xf); } memset(vis,0,sizeof(vis)); int zd=bfs(1); //求直径一端点 memset(vis,0,sizeof(vis)); total=0; vis[zd]=1; dfs(zd,0); //求直径长度 // printf("%d %d %I64d\n",qd,zd,total); //求到两个端点的距离 memset(vis,0,sizeof(vis)); vis[zd]=1; pp=0; dfs1(zd,0); memset(vis,0,sizeof(vis)); vis[qd]=1; pp=1; dfs1(qd,0); ll sum=0; gg1[0][zd]=0; gg1[1][qd]=0; for(i=1; i<=n; i++) { //printf("%I64d %I64d\n",gg1[0][i],gg1[1][i]); sum+=max(gg1[0][i],gg1[1][i]); } sum-=gg1[0][qd]; printf("%I64d\n",sum); } return 0;}
- 2017湘潭赛XTU1267Highway
- 2017湘潭赛总结
- 2017湘潭赛XTU1266Parentheses
- 2017湘潭赛a题
- 2017湘潭赛Partial Sum
- 2017湘潭赛XTU1264Partial Sum
- 09湘潭赛
- 湘潭大学校赛
- 2014湘潭赛
- 2013湘潭赛
- Partial Sum (2017湘潭)
- 2017CCPC中南地区赛暨湘潭大学邀请赛总结
- 2017 湘潭大学邀请赛/江苏省省赛 H题 Highway
- 湘潭赛的一些感想
- 2013湘潭赛 卡片重组
- 2014/5/31湘潭赛
- 湘潭大学新生赛总结
- 2017年 湘潭校赛 H题 Highway 最长路径和
- tf.contrib
- 如何取出表中指定区间的记录
- Unity3D中通过代码修改子物体层级的顺序
- 机器学习的损失函数
- 整数中1出现的次数
- 2017湘潭赛XTU1267Highway
- [模板]poj3259(判断是否存在负环)
- vi三种模式
- React-Native项目中使用TabBar
- 树控件示例
- Python
- 国内人脸识别研究现状2010年之前
- 马氏距离+协方差公式+无偏估计
- Codeforces Round #423 (Div. 2) B. Black Square