关闭

xtu 1267 Highway 湘潭邀请赛H

451人阅读 评论(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 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


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 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
0
0
查看评论

2016年湘潭地区邀请赛总结

这是我第一次参加大型比赛吧。成功实力打铁。。 记得前一天晚上明明闹好了早上6点20的闹钟,结果却没!响! 我的天,,这种事情我一直以为只会发生在其他人身上,,哎!幸亏在7点20左右被学长他们的QQ电话‘准时’叫醒,然后牙也不刷了,,狂奔向校车。出发之后才发现因为太急没带几何模板,,幸好没出几何题。。...
  • qq_30927651
  • qq_30927651
  • 2016-06-06 22:15
  • 981

Xiangtan Invitation Contest 2017(2017湘潭邀请赛)(Done)

叉姐出的题。 题目链接: 点击打开题目链接 题号:1260−1269.题号:1260-1269.A.Determinant.(Accept)A. Determinant.(Accept) 题解:把矩阵补全成n∗nn*n的。那么题目就是要你求逆矩阵的第一列,高斯消元就可以啦。B.Roads.(...
  • liangzhaoyang1
  • liangzhaoyang1
  • 2017-05-29 00:00
  • 559

Echo (湘潭大学OJ)

“湖南华园科技杯”第4届湘潭市程序设计比赛暨2012ACM/ICPC湘潭邀请赛(Semilive) http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1144 Echo Accepted :...
  • hnuzengchao
  • hnuzengchao
  • 2012-05-20 18:12
  • 5158

ACM赛后总结(湘潭邀请赛,怀化交流赛)

转眼ACM也搞了这么久了,怎么说呢!虽然说是断断续续的搞得,但是还是有点累啊,不过还是值得的,我感觉我成长了。从初入大学的迷茫,转变到现在有点事情做了,也还算不错。         湘潭怀化的赛事,算上来是第一次正式的对外赛事,...
  • u014688855
  • u014688855
  • 2014-06-09 13:29
  • 6834

Collatz Conjecture(湘潭大学OJ)

“湖南华园科技杯”第4届湘潭市程序设计比赛暨2012ACM/ICPC湘潭邀请赛(Semilive) http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1142 Collatz Conjecture ...
  • hnuzengchao
  • hnuzengchao
  • 2012-05-20 18:08
  • 4784

2017CCPC中南地区赛暨湘潭大学邀请赛总结

1.    5月12号晚上19点的火车从南阳--->长沙坐了十个小时,第二天早上7点到了长沙,第一次坐这么长时间的硬座(为什么不坐卧铺呢。。我也不知道。。),很痛苦,还好的是火车上没什么人,晚上可以睡一会,然后到了长沙以后,就有坐了动车去了湘潭,动车环境比火车好多了,赞一下...
  • riba2534
  • riba2534
  • 2017-05-17 18:39
  • 995

2017 ccpc 湘潭邀请赛 H highway

先求出树的最远点对(树的直径)d1,d2,再求出以直径的两个端点为起点的dist[i](起点到i的距离),首先将直径(d1,d2的距离)加入集合,对于其他点i,加入max(d1到i的距离,d2到i的距离)到集合,集合所构成的树就是题目的答案 交了n次都wa了,对拍了一个下午,结果发现是没选择c++...
  • chen_minghui
  • chen_minghui
  • 2017-05-25 17:43
  • 262

2017 湘潭大学邀请赛H题--Highway

2017湘潭大学邀请赛H题—Highway
  • qq_37960603
  • qq_37960603
  • 2017-08-02 23:19
  • 131

2016湖南湘潭邀请赛题解:2016年“长城信息”杯中国大学生程序设计比赛中南地区邀请赛(迟来的题解)

2016湖南湘潭邀请赛题解:2016年“长城信息”杯中国大学生程序设计比赛中南地区邀请赛(迟来的题解) 提交OJ:http://202.197.224.59/OnlineJudge2/index.php/Problem/index/p/13/ 题号:xtu 1243-1252 xtu 1243...
  • shengjmm
  • shengjmm
  • 2017-08-17 16:18
  • 159

NEFU 2 猜想 高效素数打表

点击打开链接     猜想 Time Limit 3000ms Memory Limit 65536K description 哥德巴赫(Goldbach ]C.,1690.3.18~1764.11.20)是德国数学家;...
  • Dinivity123
  • Dinivity123
  • 2013-09-20 15:53
  • 729
    个人资料
    • 访问:64246次
    • 积分:2239
    • 等级:
    • 排名:第19758名
    • 原创:160篇
    • 转载:30篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论