Poj1751 when it is known that some edges have been created, the minimum number of edges to be created in the Spanning Tree is obtained.

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.