Wust limit l Chanllenge 2014 C question poj1751 Highways

Source: Internet
Author: User

Give n coordinate points, some of which are connected

Find a Minimum Spanning Tree and output the two endpoints of the edge to be connected, so you have to record the path


In fact, the kruskal algorithm should be more concise for this output edge question.


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        # Define inf 0x3f3f3f # define ll _ int64using namespace std; int n, vis [755], pre [755], x [755], y [755], d [755], e [755] [755]; void prim () {int I, j, p, tmp; memset (vis, 0, sizeof vis ); for (I = 2; I <= n; I ++) {pre [I] = 1; // record the previous vertex d [I] = e [1] [I];} d [1] = 0; vis [1] = 1; for (I = 2; I <= n; I ++) {tmp = inf; p = 0; for (j = 1; j <= n; j ++) {if (! Vis [j] & tmp> d [j]) {tmp = d [j]; p = j ;}} if (tmp! = 0) printf ("% d \ n", pre [p], p); if (tmp = inf) break; vis [p] = 1; for (j = 1; j <= n; j ++) {if (! Vis [j] & d [j]> e [p] [j]) {d [j] = e [p] [j]; pre [j] = p ;}}} int main () {int I, j, a, B, q; scanf ("% d", & n ); for (I = 1; I <= n; I ++) scanf ("% d", & x [I], & y [I]); for (I = 1; I <= n; I ++) {for (j = 1; j <= I; j ++) {if (I = j) e [I] [j] = 0; // The specific distance value does not affect the judgment size. Therefore, you can also choose not the root number else e [I] [j] = e [j] [I] = (x [I]- x [j]) * (x [I]-x [j]) + (y [I]-y [j]) * (y [I]-y [j]);} scanf ("% d", & q); while (q --) {scanf ("% d", & a, & B ); // The connected edge directly sets the edge weight to 0 e [a] [B] = e [B] [a] = 0;} prim (); return 0 ;}
       
      
     
    
   
  
 


Related Article

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.