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


Sample Output


题意: 

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

思路:

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

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

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
#define Bug cout<<"---------------------"<<endl
const int maxn=;
using namespace std; struct edge_node
{
int to;
int val;
int next;
}Edge[maxn*maxn];
int Head[maxn];
int tot; struct point_node
{
int x;
int y;
}PT[]; void Add_Edge(int u,int v,double w)
{
Edge[tot].to=v;
Edge[tot].val=w;
Edge[tot].next=Head[u];
Head[u]=tot++;
} int lowval[maxn];
int pre[maxn];//记录每个点的双亲是谁 void Prim(int n,int st)//n为顶点的个数,st为最小生成树的开始顶点
{
fill(lowval+,lowval++n,INF);//不能用memset(lowval,INF,sizeof(lowval))
memset(pre,,sizeof(pre));
lowval[st]=-;
pre[st]=-;
for(int i=Head[st];i!=-;i=Edge[i].next)
{
int v=Edge[i].to;
int w=Edge[i].val;
lowval[v]=min(lowval[v],w);
pre[v]=st;
}
for(int i=;i<n-;i++)
{
int MIN=INF;
int k;
for(int i=;i<=n;i++)//根据编号从0或是1开始,改i从0--n-1和1--n
{
if(lowval[i]!=-&&lowval[i]<MIN)
{
MIN=lowval[i];
k=i;
}
}
if(MIN!=)//权值不为0,说明要修路
printf("%d %d\n",pre[k],k);
lowval[k]=-;
for(int j=Head[k];j!=-;j=Edge[j].next)
{
int v=Edge[j].to;
int w=Edge[j].val;
if(w<lowval[v])
{
lowval[v]=w;
pre[v]=k;
}
}
}
} int main()
{
int n,m;
scanf("%d",&n);
memset(Head,-,sizeof(Head));
tot=;
for(int i=;i<=n;i++)
{
scanf("%d %d",&PT[i].x,&PT[i].y);
}
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
int x,y;
x=PT[i].x-PT[j].x;
y=PT[i].y-PT[j].y;
int val=x*x+y*y;
Add_Edge(i,j,val);
Add_Edge(j,i,val);
}
}
scanf("%d",&m);
for(int i=;i<m;i++)
{
int u,v;
scanf("%d %d",&u,&v);
Add_Edge(u,v,);
Add_Edge(v,u,);
}
Prim(n,);
return ;
}

POJ-1751 Highways(最小生成树消边+输出边)的更多相关文章

  1. POJ 1751 Highways (最小生成树)

    Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  2. POJ 1751 Highways(最小生成树Prim普里姆,输出边)

    题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...

  3. POJ 1751 Highways (最小生成树)

    Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...

  4. POJ 1751 Highways 【最小生成树 Kruskal】

    Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23070   Accepted: 6760   Speci ...

  5. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

  6. POJ 1751 Highways(最小生成树&Prim)题解

    思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...

  7. POJ 1751 Highways (kruskal)

    题目链接:http://poj.org/problem?id=1751 题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树. 这里用kruskal不会超时 ...

  8. POJ 1751 Highways (ZOJ 2048 ) MST

    http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...

  9. (poj) 1751 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...

随机推荐

  1. C++的vector容器清空

    c++内部STL库中自带了一个容器vetcor, 自带了清空方法——clear().但是clear使用之后,并不能清空数据,其数据再未被覆盖之前是不会改变的,个人猜测clear仅仅把指针挪动到了起始位 ...

  2. POJ 1050:To the Max

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43241   Accepted: 22934 Desc ...

  3. 19 01 19 视图 HttpReqeust对象 GET属性 POST属性 HttpResponse对象

    ---恢复内容开始--- URLconf 义,指定URL和视图函数的对应关系. 在应用内部创建urls.py文件,指定请求地址与视图的对应关系. url(正则,'视图函数名称') 1)如示例在book ...

  4. C++基础--string转

    有时候除了要将数值型转为string外,可能也需要将一些string转为数值型,这个时候也还是可以用sstream字符串流来实现,同时也可以用C++标准库得到函数来实现. 1.字符串流 这个时候使用i ...

  5. swift之保存数据到keychain

    访问KeyChain 1.在mac上按下 Command+Space 输入Keychain Access 2.在终端输入security find-generic-password -help 读取配 ...

  6. Android自定义View——简单实现边缘凹凸电子票效果

        View继承LinearLayout,在View的上下边缘画出白色的圆形即可,这里只要计算出圆的个数和圆的循环规律即可,下面请看分析 我们取卡片的前2个凹凸来看,将其分为四部分,并且两部分为循 ...

  7. POJ 2014:Flow Layout 模拟水题

    Flow Layout Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3091   Accepted: 2148 Descr ...

  8. sed使用案例

    简介: sed是一种流编辑器,它是文本处理中非常重要的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用 ...

  9. CentOS 7.3 安装redis 4.0.2服务

    CentOS 7.3 安装redis 4.0.2服务 1.下载解压 下载地址:/home/xiaoming/ wget http://download.redis.io/releases/redis- ...

  10. Map的6种遍历方法

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 探讨有几种遍历Map的方法其实意义并不大,网上的文章一般讲4种或5种的居多,重要的是知道遍历的内涵,从遍历 ...