zoukankan      html  css  js  c++  java
  • POJ 1751 Highways (最小生成树)

    Highways
    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
    Appoint description: 

    Description

    The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

    Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

    The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length. 

    Input

    The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built. 

    The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of i th town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location. 

    The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway. 

    Output

    Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space. 

    If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty. 

    Sample Input

    9
    1 5
    0 0 
    3 2
    4 5
    5 1
    0 4
    5 2
    1 2
    5 3
    3
    1 3
    9 7
    1 2

    Sample Output

    1 6
    3 7
    4 9
    5 7
    8 3



    计算的时候不用开方,每合并一次就输出一次。
      1 #include <iostream>
      2 #include <fstream>
      3 #include <cstdio>
      4 #include <string>
      5 #include <queue>
      6 #include <vector>
      7 #include <map>
      8 #include <algorithm>
      9 #include <cstring>
     10 #include <cctype>
     11 #include <cstdlib>
     12 #include <cmath>
     13 #include <ctime>
     14 using    namespace    std;
     15 
     16 const    int    SIZE = 800;
     17 int    FATHER[SIZE],N,M,NUM;
     18 struct    Node
     19 {
     20     int    from,to;
     21     double    cost;
     22 }G[SIZE * SIZE];
     23 struct    
     24 {
     25     int    x,y;
     26 }TEMP[SIZE];
     27 
     28 void    ini(void);
     29 int    find_father(int);
     30 void    unite(int,int);
     31 bool    same(int,int);
     32 void    kruskal(void);
     33 bool    comp(const Node &,const Node &);
     34 double    dis(int,int,int,int);
     35 int    main(void)
     36 {
     37     int    x,y;
     38 
     39     while(~scanf("%d",&N))
     40     {
     41         ini();
     42         for(int i = 1;i <= N;i ++)
     43             scanf("%d%d",&TEMP[i].x,&TEMP[i].y);
     44         for(int i = 1;i <= N;i ++)
     45             for(int j = i + 1;j <= N;j ++)
     46             {
     47                 G[NUM].from = i;
     48                 G[NUM].to = j;
     49                 G[NUM].cost = dis(TEMP[i].x,TEMP[i].y,TEMP[j].x,TEMP[j].y);
     50                 NUM ++;
     51             }
     52         sort(G,G + NUM,comp);
     53         scanf("%d",&M);
     54         for(int i = 1;i <= M;i ++)
     55         {
     56             scanf("%d%d",&x,&y);
     57             unite(x,y);
     58         }
     59         kruskal();
     60     }
     61 
     62     return 0;
     63 }
     64 
     65 void    ini(void)
     66 {
     67     NUM = 0;
     68     for(int i = 1;i <= N;i ++)
     69         FATHER[i] = i;
     70 }
     71 
     72 int    find_father(int n)
     73 {
     74     if(FATHER[n] == n)
     75         return    n;
     76     return    FATHER[n] = find_father(FATHER[n]);
     77 }
     78 
     79 void    unite(int x,int y)
     80 {
     81     x = find_father(x);
     82     y = find_father(y);
     83 
     84     if(x == y)
     85         return    ;
     86     FATHER[x] = y;
     87 }
     88 
     89 bool    same(int x,int y)
     90 {
     91     return    find_father(x) == find_father(y);
     92 }
     93 
     94 bool    comp(const Node & a,const Node & b)
     95 {
     96     return    a.cost < b.cost;
     97 }
     98 
     99 void    kruskal(void)
    100 {
    101     int    count = 0;
    102 
    103     for(int i = 0;i < NUM;i ++)
    104         if(!same(G[i].from,G[i].to))
    105         {
    106             unite(G[i].from,G[i].to);
    107             printf("%d %d
    ",G[i].from,G[i].to);
    108             count ++;
    109             if(count == N - 1)
    110                 break;
    111         }
    112 }
    113 
    114 double    dis(int x_1,int y_1,int x_2,int y_2)
    115 {
    116     return    pow(x_1 - x_2,2) + pow(y_1 - y_2,2);
    117 }
  • 相关阅读:
    向企业一样的思考
    在CentOS上搭建WordPress的博客系统
    DirectoryEntry所有字段对应解释
    Oracle11gSGA调整方法
    Oracle之内存结构(SGA、PGA)
    C# 打开指定目录并定位到文件
    C# 客户端读取共享目录文件
    谈30岁后it人员职业发展规划
    常用HTTP contentType与后端处理方式
    C# worksheet设置Excel样式
  • 原文地址:https://www.cnblogs.com/xz816111/p/4549270.html
Copyright © 2011-2022 走看看