POJ-1751 Highways(最小生成树消边+输出边)
http://poj.org/problem?id=1751
Description
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 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
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(最小生成树消边+输出边)的更多相关文章
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 1751 Highways(最小生成树Prim普里姆,输出边)
题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- POJ 1751 Highways 【最小生成树 Kruskal】
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23070 Accepted: 6760 Speci ...
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- POJ 1751 Highways(最小生成树&Prim)题解
思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...
- POJ 1751 Highways (kruskal)
题目链接:http://poj.org/problem?id=1751 题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树. 这里用kruskal不会超时 ...
- POJ 1751 Highways (ZOJ 2048 ) MST
http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...
- (poj) 1751 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...
随机推荐
- 基于Ambari的WebUI部署HBase服务
基于Ambari的WebUI部署HBase服务 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.部署Ambari服务 博主推荐阅读: https://www.cnblogs.co ...
- Android 为控件添加点击涟漪效果
Android在5.0版为Button默认添加了点击时的涟漪效果,而且在其他的控件上也可以轻松的实现这种炫酷的效果.涟漪效果可以分为两种,一种时有边界的涟漪,另一种时无边界的涟漪.所谓的有边界,即涟漪 ...
- Scrapy的学习和使用
1.安装 在windows下安装是真心复杂,不搞了,直接在linux下研究吧! sudo pip install scrapy 2.安装遇到问题 2.1.bz2 module not availabl ...
- Java底层魔术类Unsafe用法简述
1 引子 Java中没有指针,不能直接对内存地址的变量进行控制,但Java提供了一个特殊的类Unsafe工具类来间接实现.Unsafe主要提供一些用于执行低级别.不安全操作的方法,如直接访问系统内存资 ...
- java获取配置文件信息
两个类 package com.censoft.util; import java.util.Properties; import java.io.*; import java.util.ArrayL ...
- MySQL 插入 中文数据乱码解决
问题描述: 1.在命令行中进行插入,没有问题.但是显示存在部分乱码 2.在JDBC中插入成功.中文是直接以“??”形式显示. 通过Navicat客户端查看 与在网页中看到的一一致,说明读取没有问题,问 ...
- F5 BIG-IPLTM但比组网的三种连接模式(转)
原文链接:https://www.cnblogs.com/yujianadu/p/11850977.html作者:遇见阿杜
- Digit sum (第 44 届 ACM/ICPC 亚洲区域赛(上海)网络赛)进制预处理水题
131072K A digit sum S_b(n)Sb(n) is a sum of the base-bb digits of nn. Such as S_{10}(233) = 2 + 3 ...
- [CISCN2019 华北赛区 Day2 Web1]Hack World
知识点:题目已经告知列名和表明为flag,接下来利用ascii和substr函数即可进行bool盲注 eg: id=(ascii(substr((select(flag)from(flag)),1,1 ...
- STM32F407读编码器没上拉电阻遇见的问题
在调试之前由于本科阶段参加飞思卡尔智能汽车的竞赛,一直在使用与竞赛相关的单片机和编码器,后来由于工程的需要开始使用STM32的板子,在调试编码器的时候遇见了,使用了STM32的官方标准库中的定时器正交 ...