Zoj 2048 highways (Kruskal ~)

Source: Internet
Author: User

This question is the same as that of poj, but the content is different ..

 

This question is actually quite watery. if you add a Lexicographic Order, you will be confused. Unfortunately, special judge is still absent ~

 

I started to know wa after reading the party. I started to judge whether to join the set, I thought that there was a high-speed path in the following process without adding it to the node. Wa proved that I was wrong... Because the Kruskal algorithm is used to judge the points in a set with it... And so on... If they are not added, they are not included in the set in the PRE array... It's quite tangled ~

 

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <iostream> <br/> # include <math. h> <br/> # include <memory. h> <br/> # define n 755 <br/> using namespace STD; <br/> int N, P = 0; <br/> int pre [N]; <br/> struct edge <br/> {<br/> int X, Y; <br/> double Len; <br/>} City [N * n]; <br/> typedef struct edge node; <br/> double compute (double X1, double Y1, double X2, double Y2) <br/> {<br/> return (x1-x2) * (x1-x2) + (Y1 -Y2) * (y1-y2); <br/>}< br/> int find (int x) <br/>{< br/> while (X! = Pre [x]) <br/> X = pre [X]; <br/> return X; <br/>}< br/> void input () <br/>{< br/> int M, X, Y; <br/> P = 0; <br/> double COOR [N] [2], Len; <br/> CIN> N; <br/> for (INT I = 1; I <= N; I ++) <br/> CIN> COOR [I] [0]> COOR [I] [1]; <br/> CIN> m; <br/> for (INT I = 1; I <= N; I ++) <br/> pre [I] = I; <br/> for (INT I = 1; I <= m; I ++) <br/>{< br/> CIN> x> Y; <br/> X = find (x); <br/> Y = find (y); <br/> If (X! = Y) <br/> pre [x] = y; <br/>}< br/> for (INT I = 1; I <= N; I ++) <br/> for (Int J = I + 1; j <= N; j ++) <br/> {<br/> Len = compute (COOR [I] [0], COOR [I] [1], COOR [J] [0], COOR [J] [1]); <br/> City [p]. X = I; City [p]. y = J; <br/> City [p]. len = Len; <br/> P ++; <br/>}< br/> int CMP (const void * a, const void * B) <br/> {<br/> return (node *) A)-> Len> (node *) B)-> Len? 1:-1; <br/>}< br/> void Kruskal () <br/>{< br/> int A, B, I; <br/> qsort (city, P, sizeof (node), CMP); <br/> for (I = 0; I <p; I ++) <br/> {<br/> A = find (City [I]. x); <br/> B = find (City [I]. y); <br/> if (! = B) <br/>{< br/> pre [B] = A; <br/> cout <City [I]. x <"" <City [I]. Y <Endl; <br/>}< br/> int main (void) <br/>{< br/> int ncases; <br/> CIN> ncases; <br/> while (ncases --) <br/>{< br/> input (); <br/> Kruskal (); <br/> If (ncases! = 0) <br/> cout <Endl; <br/>}< br/> return 0; <br/>}< br/> 

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.