(树的直径)第九届湘潭市大学生程序设计比赛 H-Highway

Highway

Accepted : 25   Submit : 104
Time Limit : 4000 MS   Memory Limit : 65536 KB 

Highway

In 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.

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 n. The i-th of the following (n?1) lines contains three integers ai, bi and ci.

  • 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

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

Sample Output

19
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";
    }

}
时间: 2025-01-08 20:59:04

(树的直径)第九届湘潭市大学生程序设计比赛 H-Highway的相关文章

第九届福建省大学生程序设计竞赛 2018.8.26组队训练赛

题目链接:http://acm.fzu.edu.cn/contest/list.php?cid=158 A题题目: 题意: 给你六种操作:def, mul,mod,div, add, sub.除了看这几个字母也都知道是啥意思了,其中def是进行define. 思路: 比赛时队友写的,直接模拟,不过赛后补题时队友和我说mul时需要快速乘. 代码实现如下: 1 #include <set> 2 #include <map> 3 #include <queue> 4 #inc

FZU2295 Human life:网络流-最大权闭合子图-二进制优化-第九届福建省大学生程序设计竞赛

目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 ?原题目描述在最下面. ?题意就是很裸的最大权闭合子图. ?推荐阅读:胡伯涛<最小割模型在信息学竞赛中的应用> ?完完全全的模板题:新疆大学五月月赛-D-勤奋的杨老师 ?本题题意:m(50)个任务,n个技能.完成每个任务由相应的收益,完成每个任务前必须学一些技能.有些技能由先修技能. ?有些任务不能同时完成. Solution: ?训练

ZZUOJ-1195-(郑州大学第七届ACM大学生程序设计竞赛E题)

1195: OS Job Scheduling Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 106  Solved: 35 [Submit][Status][Web Board] Description OS(Operating System) is to help user solve the problem, such as run job(task).A multitasking OS is one that can simultaneo

ZZUOJ-1195-OS Job Scheduling(郑州大学第七届ACM大学生程序设计竞赛E题)

1195: OS Job Scheduling Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 106  Solved: 35 [Submit][Status][Web Board] Description OS(Operating System) is to help user solve the problem, such as run job(task).A multitasking OS is one that can simultaneo

第六届福建省大学生程序设计竞赛不完全题解

昨天自己队做了一下第六届福建省赛,感觉有些蒙,赛后补了几道题,能力有限,一共只出A了7道题 A题   Super Mobile Charger 题目链接 http://acm.fzu.edu.cn/problem.php?pid=2212 水题 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a[10005]; int b[10005]; int main(

ACM学习历程—SNNUOJ 1110 传输网络((并查集 &amp;&amp; 离线) || (线段树 &amp;&amp; 时间戳))(2015陕西省大学生程序设计竞赛D题)

Description Byteland国家的网络单向传输系统可以被看成是以首都 Bytetown为中心的有向树,一开始只有Bytetown建有基站,所有其他城市的信号都是从Bytetown传输过来的.现在他们开始在其他城市陆 续建立了新的基站,命令“C x“代表在城市x建立了一个新的基站,不会在同一个城市建立多个基站:城市编号为1到n,其中城市1就是首都Bytetown.在建立基站的过程中他们还 会询问某个城市的网络信号是从哪个城市传输过来的,命令”Q x“代表查询城市x的来源城市. Inpu

(最短路)第七届福建省大学生程序设计竞赛 Problem J- X

Problem Description X is a fully prosperous country, especially known for its complicated transportation networks. But recently, for the sake of better controlling by the government, the president Fat Brother thinks it's time to close some roads in o

好老师 (第九届湖南大学计算机程序设计竞赛)

1334: 好老师 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 267  Solved: 131 [Submit][id=1334" style="color:rgb(26,92,200)">Status][pid=1334" style="color:rgb(26,92,200)">Web Board] Description 我想当一个好老师,所以我决定记住全部学生的名字.但是不

ZZUOJ-1194-ARM立即数寻址 (郑州大学第七届ACM大学生程序设计竞赛正式赛F题)

Problem F: ARM立即数寻址 Time Limit: 4 Sec  Memory Limit: 128 MB Submit: 53  Solved: 12 [Submit][Status][Web Board] Description 在ARM处理器立即数寻址方式中,立即数是由一个8位的无符号常数(大于等于0,小于等于0xff),先扩展为32位,然后循环右移偶数位得到.所以类似0x101,0x102,0xFF1,0xFF04,0x8000007F等都是无效的立即数,而像0xFF,0x3