XTOJ1267 Highway 【最小生成树】

Highway

Accepted : 100      Submit : 336
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 y using 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

Source

XTU 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
highway_env是一个用于创建和测试自动驾驶智能体的Python库。它旨在提供一个可扩展的环境,用于开发和评估各种自动驾驶算法。 highway_env提供了一个高速公路环境,其中包括多车道、交通流量、车辆状态和路标等元素。该库提供了一些基本的环境配置选项,例如车道宽度、车辆速度、车辆密度等,以帮助用户创建符合实际情况的测试场景。 在使用highway_env进行自动驾驶智能体开发时,用户可以通过编程和配置文件来设置环境参数,例如目标速度、道路长度、交通流量分布等。智能体可以通过接受环境状态和奖励信号来决定自己的行动,并通过交互与环境进行学习和优化。 高速公路环境中的多车道和交通流量使得智能体需要学习适应不同的交通状况,并在保持安全的同时尽可能快地到达目的地。因此,highway_env非常适合用于测试各种自动驾驶技术的性能和鲁棒性。 此外,highway_env还提供了一系列实用的函数和方法来帮助用户进行环境和智能体参数的配置、数据记录、可视化等。这些功能大大简化了自动驾驶算法的开发和调试过程,提高了开发效率。 总之,highway_env是一个功能强大的Python库,可以提供可扩展的高速公路环境,用于开发和评估自动驾驶智能体。通过使用highway_env,用户可以方便地创建各种测试场景,并进行自动驾驶算法的开发和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值