Poj1751:
When we know that some edges are created, find other edges in the Minimum Spanning Tree and output their start points and end points.
This topic is SPJ, which has no requirements on the output sequence.
I used Java to write MLE all the time, so I had to think about it, and then I wrote it again in C ++.
This time I finally learned that writing a comparison function in the structure is much faster than writing a CMP outside.
The second time the CMP was written outside, the time was too low.
Read the code.
// ================================================ ==================================================================== // Name: poj1751highways. CPP // Author: xinge // version: // copyright: Your copyright notice // description: NSI-style // ======================================== ========================================================== = # include <iostream> # include <stdio. h> # include <cstdio> # include <cmath> # include <algorithm> # include <math. h> using namespace St D; const int max = 800; int n, m; int parent [Max]; struct point {int X, Y;} data1 [Max]; struct edge {int start, end; double val; bool operator <(const edge a) const {return Val <. val ;}} data2 [(max * max)]; // The card time // bool CMP (edge E1, edge E2) // {// return e1.val <e2.val; //} void Init () {for (INT I = 0; I <Max; I ++) parent [I] = I;} int find (INT X) {If (parent [x]! = X) parent [x] = find (parent [x]); Return parent [X];} void Merge (int A, int B) {int fa = find (a); int Fb = find (B); parent [fa] = FB;} void Kruskal (INT Len) {int I, fa, FB; for (I = 1; I <= Len; I ++) {fa = find (data2 [I]. start); Fb = find (data2 [I]. end); If (Fa! = FB) {printf ("% d \ n", data2 [I]. start, data2 [I]. end); merge (data2 [I]. start, data2 [I]. end) ;}} int main () {scanf ("% d", & N); int I; for (I = 1; I <= N; I ++) scanf ("% d", & (data1 [I]). x, & (data1 [I]). y); Init (); scanf ("% d", & M); Int J, a, B; For (j = 1; j <= m; j ++) {scanf ("% d", & A, & B); merge (a, B);} int Len = 0; for (I = 1; I <= N; I ++) for (j = I + 1; j <= N; j ++) {Len ++; data2 [Len]. start = I; data2 [Len]. end = J; data2 [Len]. val = SQRT (double) (data1 [I]. x-data1 [J]. x) * (data1 [I]. x-data1 [J]. x) + (data1 [I]. y-data1 [J]. y) * (data1 [I]. y-data1 [J]. y);} Sort (data2 + 1, data2 + Len + 1); Kruskal (LEN); Return 0 ;}