POJ:1751-Highways(Kruskal和Prim)
Highways
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6078 Accepted: 1650 Special Judge
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
解题心得:
- 题目很简单,就是跑一个最小生成树,然后记录需要建立的新边。但是很坑啊,题目中说如果没有输出那么就会建立一个空白的文件,所以如果是写的多组输入,就会WA,不知道为啥,可能是没有建立空白的新文件吧。
- 然后就是邪最小生成树,两种写法
- Kruskal算法先得出每一条边,然后对边排序,从小的边开始选择,用并查集来判断是否形成了环。
- Prim算法是选择一个点,然后找出距离这个点最近的一个点,连成边,然后以找出的店为目标,再从没被连接的点中找出一个距离最近的点,连成边,然后一直将所有的点全部连接。
Kruskal算法代码:
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 1e3+100;
struct node
{
int x,y;
} p[maxn*maxn];
struct Path
{
int s,e,len;
} path[maxn*maxn];
int n,m,father[maxn*maxn],ans,t;
vector <pair<int,int> > ve;
bool cmp(Path a,Path b)
{
return a.len<b.len;
}
int find(int x)
{
if(father[x] == x)
return x;
return father[x] = find(father[x]);
}
void merge(int x,int y)
{
int fx = find(x);
int fy = find(y);
if(fx != fy)
father[fy] = fx;
}
int dis(int x,int y)
{
int d = (p[x].x - p[y].x)*(p[x].x - p[y].x) + (p[x].y - p[y].y)*(p[x].y - p[y].y);
return d;
}
void init()
{
ans = t = 0;
for(int i=1; i<=n; i++)
{
father[i] = i;
scanf("%d%d",&p[i].x,&p[i].y);
}
//枚举每一条边
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
{
path[t].s = i;
path[t].e = j;
path[t++].len = dis(i,j);
}
sort(path,path+t,cmp);//将边从小到大排序
cin>>m;
for(int i=0; i<m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(find(a) != find(b))
merge(a,b);//将已经有路的点合并
}
}
void solve()
{
for(int i=0; i<t; i++)
{
int x = path[i].s;
int y = path[i].e;
int len = path[i].len;
if(find(x) != find(y))//如果不是同一个祖先那么连接就不会形成环
{
ans += len;
merge(x,y);
ve.push_back(make_pair(x,y));//记录需要连接的点
}
}
for(int i=0; i<ve.size(); i++)
{
pair<int,int> p;
p = ve[i];
printf("%d %d\n",p.first,p.second);
}
ve.clear();
}
int main()
{
cin>>n;
init();
solve();
return 0;
}
Prim算法代码
#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1000;
//lowcast记录的是各点距离已经生成了的树的距离
int maps[maxn][maxn],lowcost[maxn],n,m,Edge[maxn];
bool vis[maxn];//记录点是否已经在树中
struct NODE
{
int x,y;
}node[maxn];
int get_dis(int x,int y)
{
int dis = (node[x].x - node[y].x)*(node[x].x - node[y].x) + (node[x].y - node[y].y)*(node[x].y - node[y].y);
return dis;
}
void init()
{
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&node[i].x,&node[i].y);
for(int j=1;j<i;j++)
maps[i][j] = maps[j][i] = get_dis(i,j);//记录两点之间的距离
maps[i][i] = 0x3f3f3f3f;//不可能自身到自身
}
memset(vis,0,sizeof(vis));//记录该点是否已经在树上
vis[1] = 1;
cin>>m;
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
maps[a][b] = maps[b][a] = 0;
}
for(int i=1;i<=n;i++)
{
lowcost[i] = maps[i][1];//先得到所有点距离第一个点的距离
Edge[i] = 1;
}
}
void Prim()
{
for(int i=1;i<n;i++)
{
int Min = 0x3f3f3f3f;
int point;
for(int j=1;j<=n;j++)//当前树距离最近的点
if(!vis[j] && Min > lowcost[j])
{
Min = lowcost[j];
point = j;
}
vis[point] = true;//将这个点加入树中
for(int k=1;k<=n;k++)
{
if(!vis[k] && lowcost[k] > maps[point][k])
{
Edge[k] = point;//记录添加边的两个点
lowcost[k] = maps[point][k];
}
}
if(maps[Edge[point]][point])
printf("%d %d\n",Edge[point],point);
}
}
int main()
{
init();
Prim();
return 0;
}
POJ:1751-Highways(Kruskal和Prim)的更多相关文章
- POJ 1751 Highways (kruskal)
题目链接:http://poj.org/problem?id=1751 题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树. 这里用kruskal不会超时 ...
- POJ 1751 Highways(最小生成树&Prim)题解
思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 1751 Highways 【最小生成树 Kruskal】
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23070 Accepted: 6760 Speci ...
- 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 (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 ...
- Highways POJ-1751 最小生成树 Prim算法
Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...
- 关于最小生成树 Kruskal 和 Prim 的简述(图论)
模版题为[poj 1287]Networking. 题意我就不说了,我就想简单讲一下Kruskal和Prim算法.卡Kruskal的题似乎几乎为0.(●-`o´-)ノ 假设有一个N个点的连通图,有M条 ...
随机推荐
- python 遇到的一些问题和解决方法
安装crypto python3里面这个改成了pycryptodome 1. pip3 install pycryptodome 或者 pip3 install -i https://pypi.do ...
- 《深入理解java虚拟机》笔记(2)HotSpot虚拟机对象探秘
一.对象的创建 1.类加载: 虚拟机在遇到一条new指令时候,检查类是否已被加载.解析.初始化过,如果没有,则执行类加载过程. 2.分配内存:类加载完成后,则为新对象从java堆上分配内存,分配内存有 ...
- 将JWT与Spring Security OAuth结合使用
1.概述 在本教程中,我们将讨论如何使用Spring Security OAuth2实现来使用JSON Web令牌. 我们还将继续构建此OAuth系列的上一篇文章. 2. Maven配置 首先,我们需 ...
- 开机报错 the connected AC adapter has a lower wattage than the recommended model which was shipped with the system。
机型:联想Thinkpad T410 报错场景:在电脑插上电源充电情况下开机,会自动进入bios setup utility提示你需要重新设置日期时间.date/time 报错提示:The conne ...
- spring data jpa自定义baseRepository
在一些特殊时候,我们会设计到对Spring Data JPA中的方法进行重新实现,这将会面临一个问题,如果我们新创建一个实现类.如果这个实现类实现了JpaRepository接口,这样我们不得不实现该 ...
- web标准、可用性、可访问性
前言:大家不难发现,只要是招聘UED相关的岗位,如前端开发工程师.交互设计师.用户研究员甚至视觉设计师,一般都对web标准.可用性和可访问性的理解有要求.那么到底什么是web标准.可用性.可访问性呢? ...
- SQLServer 2012 报表服务部署配置(1)
由于最近客户项目中,一直在做SQL Server 方面配置.就给大家概况简述一下 报表服务安装及遇到问题.安装和运行 SQL Server 2012 的微软原厂都有最低硬件和软件要求,对于我们大多数新 ...
- python读xml文件
# -*- coding:utf-8 -*- import jsonimport requestsimport os curpath=os.path.dirname(os.path.realpath( ...
- GoAccess自动分割Nginx日志
GoAccess 是一款开源的网站日志实时分析工具.GoAccess 的工作方式很容易理解,就是读取和解析 Apache/Nginx/Lighttpd 的访问日志文件 access log,然后以更友 ...
- wall命令
wall——发送广播信息 write all /usr/bin/wall 示例1: # wall 输入命令之后回车便可以广播消息,如输入Hello everybody online后Ctrl+D结束并 ...