(最小生成树 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数组保存对应下标的点到树的最近距离,这里要注 ...
随机推荐
- js实现jquery函数animate动画效果
<script> function animate(obj, json, interval, sp, fn) { clearInterval(obj.timer); function ge ...
- scala-- 内建控制结构
内建控制结构 scala 内建的控制结构很少,只有 if while for try match 和函数调用 几种. 因为scala 从语法层面支持函数字面量.几乎所有的scala控制结构都会产生 ...
- mysql备份和还原命令
备份和还原数据库都是在未登录的前提下进行命令操作的: 1.备份表: mysqldump -u root -p dbname table1 table2 > D:\sqlback.sql 2.备份 ...
- SVN服务器端的安装和配置
第2章 SVN 的下载与安装 服务器端的安装和配置 所有的开发人员用自己的账号登录进来就可以拥有对仓库里面的所有文件的读和写的权限 创建用户
- tf.random_normal()
tf.random_normal()函数用于从服从指定正太分布的数值中取出指定个数的值. tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf. ...
- OC 和 swift 冒泡排序
swift 版 var numbers = [17, 28, 36, 15, 39] print("排序前\(numbers)") for i in 0..<numbers. ...
- php Pthread 多线程 (六) Pool类 线程池
Pool对象是多个Worker对象的容器,同时也是它们的控制器,对Worker功能更高抽象. 比如Worker是河,而线程是运行在河里的船.Pool则是管理着多条河. <?php //继承Col ...
- jQuery中animate()对Firefox无效的解决办法
在使用 animate()做返回顶部的动画时,会出现对Firefox无效的情况,如: $('body').animate({scrollTop:'0'},500); 它对Chrome,IE,Opera ...
- DB2数据库常用命令数据库学习
DB2数据库常用命令数据库学习你可以用 get snapshot for locks on XXX 看是那个表锁了,再从相关的操作去查原因吧 db2pd -d 库名 -locks和db2pd -d 库 ...
- CSS中margin边界叠加问题及解决方案(转)
边界叠加简介 边界叠加是一个相当简单的概念.但是,在实践中对网页进行布局时,它会造成许多混淆.简单地说,当两个垂直边界相遇时,它们将形成一个边界.这个边界的高度等于两个发生叠加的边界的高度中的较大者. ...