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 ...
随机推荐
- 下页小希学MVC5+EF6.2 学习记录二
目的:1 学习mvc+ef 2 写下日记也是对自己的督促 从前端的UI开始 MVC分离的比较好,开发顺序没有特别要求,先开发哪一部分都可以,这次我们主要讲解前端UI的部分. ASP.NET MVC抛 ...
- Axure下载,附注册码
下载地址:https://www.axure.com.cn/3510/ 汉化教程:https://www.axure.com.cn/2616/ 注册码: Axure RP 8.1.0.3377--可用 ...
- C/C++学习笔记_gdb调试
1.前提条件:可执行文件包含调试信息 gcc -g 2.gdb 文件名 ---启动gdb调试 3.查看代码的命令 当前文件: list 行号(函数名) 指定文件: list 文件名:行号(函数名)4. ...
- Python __name__="__main__"的作用
该语句加在模块的最后,可以让这个模块,即可以被别人import,又可以直接运行. fibo.py文件: def fibo(): pass # fibo函数的内容 if __name__==" ...
- webUploader多个实例
webUploader上传图片插件一个页面多个实例多处使用
- CentOS 7 连接不到网络解决方法
使用VM12创建虚拟机并安装CentOS 7,但是安装完成后发现连接不到网络. ping jd.com发现不通 因为在创建虚拟机的时候我们选择的是NAT模式 这里给出NAT模式下对应的的解决方法: 一 ...
- 抓DHCP客户端ip脚本
cat testnew.sh #!/bin/bash catch_ip (){Ip=`sudo nmap -sP 192.168.1.0/24 |grep -i -B2 $mac|grep Nmap ...
- kube-proxy详解
KUBE_LOGTOSTDERR="--logtostderr=true"KUBE_LOG_LEVEL="--v=4"NODE_HOSTNAME="- ...
- js数组全等
js 数组全等(对象) if(this.eqOrNotEq(arr)){} eqOrNotEq(arr) { return !arr.some(function(value, index) { ret ...
- hdu1232 城镇间修路(并查集)
问题是这样的: Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇 ...