xtu 1267 Highway 湘潭邀请赛H



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  y  using 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  ai bi  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


http://www.dengwenhuo.cn/?id=453

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <queue>
using namespace std;
 
#define ll __int64
const int N=100005;
 
struct p{
    ll to,val;
};
 
struct pp{
    ll u,dis;
    bool operator < (const pp&r) const{
        return dis>r.dis;
    }
};
vector<p>G[N];
int n,vis[N];
ll dis[N];
 
int dijkstra(int s)
{
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    priority_queue<pp>q;
    dis[s]=0;
    q.push(pp{s,0});
    int k=1;
    while(!q.empty())
    {
        pp now=q.top();
        q.pop();
        if(vis[now.u]) continue;
        vis[now.u]=1;
        for(int i=0;i<G[now.u].size();i++)
        {
            p t=G[now.u][i];
            if(dis[t.to]>dis[now.u]+t.val)
            {
                dis[t.to]=dis[now.u]+t.val;
                if(dis[k]<dis[t.to])
                    k=t.to;
                q.push(pp{t.to,dis[t.to]});
            }
        }
    }
    return k;
}
void get(int s)
{
    memset(vis,0,sizeof(vis));
    queue<pp>q;
    vis[s]=1;
    q.push(pp{s,0});
    while(!q.empty())
    {
        pp now=q.front();
        q.pop();
        dis[now.u]=max(now.dis,dis[now.u]);
        for(int i=0;i<G[now.u].size();i++)
        {
            p t=G[now.u][i];
            if(!vis[t.to])
            {
                vis[t.to]=1;
                q.push(pp{t.to,now.dis+t.val});
            }
        }
    }
}
int main()
{
    ll u,v,c;
    int k1,k2;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++) G[i].clear();
        for(int i=1;i<n;i++)
        {
            scanf("%I64d %I64d %I64d",&u,&v,&c);
            G[u].push_back(p{v,c});
            G[v].push_back(p{u,c});
        }
        k1=dijkstra(1);
        ll t=dis[k1];
        k2=dijkstra(k1);
        t=max(t,dis[k2]);
        memset(dis,0,sizeof(dis));
        get(k1);get(k2);
        ll ans=0;
        for(int i=1;i<=n;i++)
            ans+=dis[i];
        printf("%I64d\n",ans-t);
    }
    return 0;
}


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  y  using 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  ai bi  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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值