Calculate the distance squared explode int The result is WA once ...
-----------------------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <vector>#include <cmath> #define REP (i, n) for (int i = 0; i < n; i++)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const INT MAXN = + 5;int n;struct P {int x, y;void Read () {scanf ("%d%d", &x, &y);}};P a[MAXN];Double Dist (p A, p b) {return sqrt (1LL * (a.x-b.x) * (a.x-b.x) + 1LL * (A.Y-B.Y) * (A.Y-B.Y));}int p[MAXN];void Uf_init () {Rep (i, N)p[i] = i;} int find (int x) {return x = = p[x]? x:p[x] = find (p[x]);}bool Unite (int x, int y) {x = Find (x), y = find (y);p[x] = y;return x! = y;}struct Edge {int u, v;double D;BOOL operator < (const edge &e) Const {return D < E.D;}};vector< edge > E;void E_clear () {e.clear ();}Double MST () {double res = 0;sort (E.begin (), E.end ());Rep (i, E.size ()) {Edge &e = e[i];if (Unite (E.U, e.v))res + = E.D;}return res;}int main () {freopen ("test.in", "R", stdin);int m;cin >> n >> m;uf_init ();e_clear ();Rep (i, N)a[i]. Read (); While (m--) {int u, v;scanf ("%d%d", &u, &v);u--, v--;Unite (U, v);}Rep (i, N)For (int j = i + 1; j < n; ++j) if (Find (i)! = Find (j))E.push_back (Edge) {i, J, Dist (a[i], a[j])}); printf ("%.2lf\n", MST ());return 0;}
-----------------------------------------------------------------------------------------
1626: [Usaco2007 dec]building Roads building the road time limit: 5 Sec Memory Limit: MB
Submit: 1212 Solved: 470
[Submit] [Status] [Discuss] Description
Farmer John has recently got some new farms, and he wants to make some new roads so that all of his farms can go through old or new roads (that is, from any farm you can reach all the remaining farms through a few connected roads). Some farms are connected by roads. All n (1 <= n <= 1,000) farms (with 1.. n sequential numbering) are represented on the map as coordinates (x_i, y_i) points (0 <= x_i <= 1,000,000;0 <= y_i <= 1,000,000), and the length of the two-farm road is naturally the distance between the points that represent them. Now farmer John has also told you what two farms the original M (1 <= m <= 1,000) of the farm is connected to, and he wants you to calculate what the minimum total length of the road he will need to build to connect all the farms.
Input
* Line 1th: 2 integers separated by a space: N and M
* 2nd. N+1 Line: section i+1 behavior 2 integers separated by spaces: x_i, Y_i * n+2..n+m+2 line: Each line with 2 spaces separated by the integer I, J describes an existing road, which connects farm I and Farm J
Output
* Line 1th: The output allows all farms to connect to the minimum total length of the construction road required, retain 2 decimal places, do not have to do any additional rounding operations. To avoid accuracy errors, use 64-bit real variables when calculating distance between farms and answers
Sample Input4 1
1 1
3 1
2 3
4 3
1 4
Input Description:
FJ altogether has 4 coordinates (in total), (3,1), (2,3), (4,3) of the farm. Farm 1 and Farm
There was a road connection between 4.
Sample Output4.00
Output Description:
FJ chose to build a 2.00-length road between Farm 1 and Farm 2, and build one on farm 3 and farm 4.
Road with a length of 2.00. In this way, the total length of the road being built is 4.00, and this is the road in all scenarios
The smallest one of the total length.
HINT
Source
Silver
Bzoj 1626: [Usaco2007 dec]building Roads Building Road (MST)