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

本文介绍了一种利用树的直径概念解决特定路径寻找问题的方法。通过两次搜索找到树的两个极端点,以此两点构建最昂贵的高速公路网络。文章提供了一个实际的C++代码示例,演示了如何实现这一解决方案。

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 (n1) 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 (n1) 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 (n1) 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 (n1) lines contains three integers aibi and ci.

  • 1n105
  • 1ai,bin
  • 1ci108
  • 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";
    }

}

 

转载于:https://www.cnblogs.com/quintessence/p/6854429.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值