zoukankan      html  css  js  c++  java
  • POJ-1751 Highways(最小生成树消边+输出边)

    http://poj.org/problem?id=1751

    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 ith 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

    题意: 

    有一个N个城市的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输出需要添加边的两端点编号即可.

    思路:

    本题就是求最小生成树的,但是由于本题不需要输出最终生成树的权值,那么我们在求两点距离的时候时间保存距离 dist=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);即可,不用sqrt开方(因为开方费时间).

    然后对于已经连接上的边,我们令这些边长为0,并添加到无向图中去即可(或令他们属于同一个并查集也行)

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <iostream>
      4 #include <string>
      5 #include <math.h>
      6 #include <algorithm>
      7 #include <vector>
      8 #include <stack>
      9 #include <queue>
     10 #include <set>
     11 #include <map>
     12 #include <sstream>
     13 const int INF=0x3f3f3f3f;
     14 typedef long long LL;
     15 const int mod=1e9+7;
     16 //const double PI=acos(-1);
     17 #define Bug cout<<"---------------------"<<endl
     18 const int maxn=760;
     19 using namespace std;
     20 
     21 struct edge_node
     22 {
     23     int to;
     24     int val;
     25     int next;
     26 }Edge[maxn*maxn];
     27 int Head[maxn];
     28 int tot;
     29 
     30 struct point_node
     31 {
     32     int x;
     33     int y;
     34 }PT[1010];
     35 
     36 void Add_Edge(int u,int v,double w)
     37 {
     38     Edge[tot].to=v;
     39     Edge[tot].val=w;
     40     Edge[tot].next=Head[u];
     41     Head[u]=tot++;
     42 }
     43 
     44 int lowval[maxn];
     45 int pre[maxn];//记录每个点的双亲是谁
     46 
     47 void Prim(int n,int st)//n为顶点的个数,st为最小生成树的开始顶点 
     48 {
     49     fill(lowval+1,lowval+1+n,INF);//不能用memset(lowval,INF,sizeof(lowval)) 
     50     memset(pre,0,sizeof(pre));
     51     lowval[st]=-1;
     52     pre[st]=-1;
     53     for(int i=Head[st];i!=-1;i=Edge[i].next)
     54     {
     55         int v=Edge[i].to;
     56         int w=Edge[i].val;
     57         lowval[v]=min(lowval[v],w);
     58         pre[v]=st;
     59     }
     60     for(int i=0;i<n-1;i++)
     61     {
     62         int MIN=INF;
     63         int k;
     64         for(int i=1;i<=n;i++)//根据编号从0或是1开始,改i从0--n-1和1--n
     65         {
     66             if(lowval[i]!=-1&&lowval[i]<MIN)
     67             {
     68                 MIN=lowval[i];
     69                 k=i;
     70             }
     71         }
     72         if(MIN!=0)//权值不为0,说明要修路 
     73             printf("%d %d
    ",pre[k],k);
     74         lowval[k]=-1;
     75         for(int j=Head[k];j!=-1;j=Edge[j].next)
     76         {
     77             int v=Edge[j].to;
     78             int w=Edge[j].val;
     79             if(w<lowval[v])
     80             {
     81                 lowval[v]=w;
     82                 pre[v]=k;
     83             }
     84         }
     85     }
     86 }
     87 
     88 int main()
     89 {
     90     int n,m;
     91     scanf("%d",&n);
     92     memset(Head,-1,sizeof(Head));
     93     tot=0;
     94     for(int i=1;i<=n;i++)
     95     {
     96         scanf("%d %d",&PT[i].x,&PT[i].y);
     97     }
     98     for(int i=1;i<=n;i++)
     99     {
    100         for(int j=i+1;j<=n;j++)
    101         {
    102             int x,y;
    103             x=PT[i].x-PT[j].x;
    104             y=PT[i].y-PT[j].y;
    105             int val=x*x+y*y;
    106             Add_Edge(i,j,val);
    107             Add_Edge(j,i,val);
    108         }
    109     }
    110     scanf("%d",&m);
    111     for(int i=0;i<m;i++)
    112     {
    113         int u,v;
    114         scanf("%d %d",&u,&v);
    115         Add_Edge(u,v,0);
    116         Add_Edge(v,u,0);
    117     }
    118     Prim(n,1);
    119     return 0;
    120 }
  • 相关阅读:
    构建TensorFlow数据流图
    Python小练习:复制操作
    Python小练习:列表的相关操作
    【Jave】接入极光推送 ------- 封装极光推送工具类
    jenkins邮件-使用变量定制化html邮件报告
    十六进制的颜色转变为rgb,设置透明度,通用方法
    一. Go微服务--隔离设计
    7.23 学习笔记
    7.22 学习笔记
    8.28正睿CSP七连测day1
  • 原文地址:https://www.cnblogs.com/jiamian/p/11733683.html
Copyright © 2011-2022 走看看