(最小生成树 Prim) Highways --POJ --1751
链接:
http://poj.org/problem?id=1751
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 11507 | Accepted: 3279 | Special Judge |
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
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
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 800
#define INF 0xffffff struct node {int x, y;}a[N]; int n, m, pre[N];
bool used[N][N], vis[N];
double G[N][N], dist[N]; void prim()
{
int i, j;
for(i=; i<=n; i++)
{
dist[i] = G[][i];
pre[i] = ;
} vis[] = ; for(i=; i<n; i++)
{
int Index = ;
double Min = INF;
for(j=; j<=n; j++)
{
if(!vis[j] && Min>dist[j])
{
Min = dist[j];
Index = j;
}
} vis[Index] = ; for(j=; j<=n; j++)
{
if(!vis[j] && dist[j] > G[Index][j])
{
dist[j] = G[Index][j];
pre[j] = Index;
}
}
}
} int main()
{
while(scanf("%d", &n)!=EOF)
{
int i, j, u, v; for(i=; i<=n; i++)
scanf("%d%d", &a[i].x, &a[i].y); for(i=; i<=n; i++)
for(j=; j<=i; j++)
G[i][j] = G[j][i] = sqrt(1.0*(a[i].x-a[j].x)*(a[i].x-a[j].x) + (a[i].y-a[j].y)*(a[i].y-a[j].y)); scanf("%d", &m);
memset(used, , sizeof(used));
for(i=; i<=m; i++)
{
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = ;
used[u][v] = used[v][u] = ;
} memset(vis, , sizeof(vis));
prim(); for(i=; i<=n; i++)
{
if(!used[pre[i]][i] && !used[i][pre[i]])
printf("%d %d\n", i, pre[i]);
}
}
return ;
}
(最小生成树 Prim) Highways --POJ --1751的更多相关文章
- H - Highways - poj 1751(prim)
某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就 ...
- Highways POJ - 1751
题目链接:https://vjudge.net/problem/POJ-1751 思路: 最小生成树板子,只需要多记录每个dis[x]的权值是从哪个点到x这个点的. #include <stdi ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- Highways POJ-1751 最小生成树 Prim算法
Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...
- 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。
//归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...
- 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...
- 最小生成树Prim算法(邻接矩阵和邻接表)
最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...
- 转载:最小生成树-Prim算法和Kruskal算法
本文摘自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html 最小生成树-Prim算法和Kruskal算法 Prim算 ...
- 最小生成树Prim
首先解释什么是最小生成树,最小生成树是指在一张图中找出一棵树,任意两点的距离已经是最短的了. 算法要点: 1.用book数组存放访问过的节点. 2.用dis数组保存对应下标的点到树的最近距离,这里要注 ...
随机推荐
- oracle启动过程
Oracle 的启动需要经历四个状态,SHUTDOWN .NOMOUNT .MOUNT .OPEN. SHUTDOWN状态 ...
- Spring MVC 接受的请求参数
目录 1. 概述 2. 详解 2.1 处理查询参数 2.2 处理路径参数接受输入 2.3 处理表单 3. 补充内容 3.1 Ajax/JSON 输入 3.2 multipart参数 3.3 接收 he ...
- java内存溢出的原因
前几天 ,面试被问到这个, 我只说了个死循环,所以上网查了下 ,下面给个总结: 内存溢出就是系统可以提供给Java虚拟机的内存不足导致的,主要分为以下几种情况: 1.要加载的数据量过大,比如加载一个很 ...
- freemaker基础语法
1.freemarker中Request,Session的用法:java块:request.getSession().setAttribute("qq","http: ...
- django中怎么使用mysql数据库的事务
Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起失败. Django中对于数据库的事务,默认每执行 ...
- php的websocket
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Sock ...
- php 过滤emoji
function filter_emoji_string($str){ $str = trim($str); $str = preg_replace_callback('/./u',function ...
- Resume (Curriculum Vitae)
The resume (Curriculum Vitae) is a selling tool outlining your skills and experience so an employer ...
- SVN 客户端的安装与配置
127.0.0.1这个IP地址代表连接本机,其实https://127.0.0.1:443/svn/Test/和https://ZhongZhenhua-PC/svn/Test/是一样的,因为我这里S ...
- centos 系统下彻底删除mysql
yum remove mysql mysql-server mysql-libs compat-mysql51 rm -rf /var/lib/mysql (删除数据库) rm /etc/my.cnf ...