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 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 
#include 
#include 
#include 
#include 
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

G[N]; int n,vis[N]; ll dis[N]; int dijkstra(int s) { memset(vis,0,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); priority_queueq; 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;idis[now.u]+t.val) { dis[t.to]=dis[now.u]+t.val; if(dis[k]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



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

你可能感兴趣的:(c/c++)