[Explanations] HLOJ 1253 Corridor Songkran

Topic Portal

Topic Description:
We have a total of N OIER intend to participate in the Songkran Festival, at the same time it happened is exactly N taps (As to why, I do not explain). Exactly N-1 trail between N taps, faucets and each can reach the other trail through the tap (this is a tree, you should know ...).
However, in order to meet their OIER of challenges, we decided to build some roads (As for how to repair, secret), so that each tap between each tap has a direct road connection (that is, to form a complete graph chant).
But OIER are lazy, and memory is not good, they will go away and that N-1 trail, and hope to build between all the faucets on the road, trail must be greater than all previously connected two taps (of course, if the shortest path of the). So God COW who help those who OIER calculate it, the total length of the shortest road construction that is how much, after all, is to spend the construction of roads


Input Format
plurality of sets of data present entitled
first row t, t expressed set of test data
for each set of data
of the first row N, the number of taps (the number of course OIER); 2
to N rows, each row of three integer X, Y, Z, and X represents a faucet taps have a length Y Z trail.


Output Format
For each test, output an integer that represents the minimum value of the total length of all of the road construction.


Analysis: The
meaning of the questions can be converted to give you a unique minimum spanning tree, lets you add side, the entire map becomes a complete graph, the smallest edge weight is added and how much.

Then we think:

  • This is only the minimum spanning tree minimum spanning tree, indicating complete graph minimum weight is also unique. We would like the idea Kruscal: Figure from small to large will be sorted by the right side, after each screening of the smallest side, if not in the same collection, you add this edge.

This problem has given us a unique minimum spanning tree, whether we can reverse method Kruscal push it?

use n in m [ i ] Surely, [i] indicates the number of ancestors of a set of point i
by f a [ i ] four [c] denotes the i-th point ancestor

First side in ascending sort order, the enumeration of each edge, if the two sides not in the same collection, then we can start with a plus side.

Set the starting point of this edge ancestors x x , the end of ancestors Y Y , the right side is v v
For x x and Y Y two sets, wherein each edge even a weight of each edge can be set to another value v + 1 v + 1 side (because this is the only minimum spanning tree, so if even if they are violating the v only One only this condition).

which is a n s + = v ( n in m [ x ] + n in m [ Y ] 1 ) ans = + V (whether [x] + num [y] -1)
(-1 because the two sets have been previously connected to one side, then this edge can not be considered.)

Each point is done in the same set after n-1 times, this case is a complete graph in FIG, a n s years is the answer we need

Then the specific code as follows:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
struct node{
    int x,y;
	LL v;
}e[200001];
int num[100001];
int fa[100001];
int t,n;
LL ans=0;
int getfa(int k){
    return fa[k]==k?k:fa[k]=getfa(fa[k]);
}
inline bool mycmp(node x,node y){
    return x.v<y.v;
}
int main(){
	freopen("input.in","r",stdin);
	freopen("input.out","w",stdout);
    scanf("%d",&t);
    while (t--){
		scanf("%d",&n);
	    for (int i=1;i<=n;i++) fa[i]=i,num[i]=1;
	    ans=0;
	    for (int i=1,x,y,z;i<n;i++)
	      scanf("%d %d %lld",&e[i].x,&e[i].y,&e[i].v),ans+=e[i].v;
	    sort(e+1,e+n,mycmp); 
		for (int i=1;i<n;i++){
		    int x=getfa(e[i].x),y=getfa(e[i].y);
		    if (x==y) continue;
		    fa[x]=y;
		    ans+=(LL)(e[i].v+1)*(num[x]*num[y]-1);
		    num[y]+=num[x];
		}
		printf("%lld\n",ans);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

Guess you like

Origin blog.csdn.net/huang_ke_hai/article/details/87918496