It is also a minimal spanning tree to test the template;
Question: N vertices, coordinates are given, and M paths have been repaired. The starting and ending points of M paths are given, and how far the paths need to be repaired can be minimized.
Tree
Solution: Calculate the repaired path length to 0, and then generate the tree with the minimum value. Then, I wa it once, and I used 64-bit operations for all the questions. I didn't see it. I took the coordinates.
Changed to longlong.
Good water;
[Cpp]
# Include <iostream>
# Include <cstring>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
Using namespace std;
Const int MAXN = 1005;
Struct Edge
{
Int start, end;
Double length;
} Edge [MAXN * MAXN];
Struct Point
{
Long x, y;
} Point [MAXN];
Int father [MAXN], Count;
Bool vis [MAXN] [MAXN];
Double getlength (Point a, Point B)
{
Double len;
Len = sqrt (double (. x-B. x) * (. x-B. x) + (. y-B. y) * (. y-B. y )));
Return len;
}
Bool cmp (Edge a, Edge B)
{
Return a. length <B. length;
}
Int find (int x)
{
If (father [x] = x) return x;
Father [x] = find (father [x]);
Return father [x];
}
Bool Union (int x, int y)
{
Int f1 = find (x );
Int f2 = find (y );
If (f1 = f2) return false;
Else if (f1 <f2) father [f1] = f2;
Else father [f2] = f1;
Return true;
}
Double kruskal (int n)
{
Int I, j = 0;
Double sum = 0;
For (I = 0; I <n; I ++)
Father [I] = I;
Sort (edge, edge + Count, cmp );
For (I = 0; I <Count & j <n; I ++)
{
If (Union (edge [I]. start, edge [I]. end ))
{
Sum + = edge [I]. length;
J ++;
}
}
Return sum;
}
Int main ()
{
Int M, N;
Int I, j, start, end;
Count = 0;
Memset (vis, 0, sizeof (vis ));
Scanf ("% d", & N, & M );
For (I = 0; I <N; I ++)
Scanf ("% d", & point [I]. x, & point [I]. y );
For (I = 0; I <M; I ++)
{
Scanf ("% d", & start, & end );
Vis [start-1] [end-1] = 1;
Vis [end-1] [start-1] = 1;
}
Count = 0;
For (I = 0; I <N-1; I ++)
For (j = I + 1; j <N; j ++)
{
Edge [Count]. start = I;
Edge [Count]. end = j;
If (vis [I] [j] = 0) edge [Count]. length = getlength (point [I], point [j]);
Else edge [Count]. length = 0;
Count ++;
}
// For (I = 0; I <Count; I ++)
// Cout <edge [I]. start <"<edge [I]. end <" <edge [I]. length <endl;
Printf ("%. 2f \ n", kruskal (N ));
Return 0;
}