POJ 1751 Highways(最小生成树Prim普里姆,输出边)
题目链接:点击打开链接
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
题目大意:直接解读样例:9个村庄的坐标,三个已经有了的公路,输出还要建的公路以使公路总长最短。
思路:稠密图求最小生成树的问题,既然是稠密图,可以用Prim算法。已经有了的边要加入最小生成树中,就令这些边的权值为零。这题的邻接矩阵cost[][],注意邻接矩阵的主对角线是0, 而且对称。 每条边的权值为两点之间的距离,因为只是比较距离,所以在prim里直接比较距离的平方就行,这样也可以避免sqrt之后变成double出现精度问题。
还有就是如何输出边的问题,在prim中要求输出边的话,可以新建一个edge[]数组,edge[i] = j表示i是从j延伸过来的,代码中有有三处出现了edge[],仔细思考
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<set>
typedef long long ll;
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 800;
bool vis[maxn];
int edge[maxn];
int lowc[maxn];
int cost[maxn][maxn];
void prim(int cost[][maxn], int n){
int ans = 0;
bool ard = false;
memset(vis, false, sizeof(vis));
vis[0] = inf;
for(int i = 1; i < n; i++) {lowc[i] = cost[0][i];edge[i] = 0;}/////////////1
for(int i = 1; i < n; i++){
int minc = inf;
int p = -1;
for(int j = 0; j < n; j++){
if(!vis[j] && minc > lowc[j]){
minc = lowc[j];
p = j;
}
}
if(p == -1)return ;
ans += minc;
vis[p] = true;
for(int j = 0; j < n; j++){
if(!vis[j] && lowc[j] > cost[p][j]) {lowc[j] = cost[p][j];edge[j] = p;}/////////////////2
if(edge[p] == j &&cost[p][j] == minc && minc!= 0 ) {printf("%d %d\n", p+1, j+1);}//////////////3
}
}
return ;
}
struct Node{
int x, y;
}node[maxn];
ll d2(int x1, int y1, int x2, int y2){
ll ans = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
return ans;
}
int main(){
int n, m;
scanf("%d", &n);
int p = 0;
for(int i = 0; i < n; i++){
scanf("%d %d", &node[p].x, &node[p].y);
p++;
}
scanf("%d", &m);
int n1, n2;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cost[i][j] = d2(node[i].x, node[i].y,node[j].x, node[j].y);
}
}
for(int i = 0; i < m; i++){
scanf("%d %d", &n1, &n2);
n1--;n2--;
cost[n1][n2] = 0;
cost[n2][n1] = 0;
}
//printf("\n");
/*for(int i = 0; i < n; i++){
printf("no[%d] = %d \n", i, no[i]);
//for(int j = 0; j < n; j++)printf("[%d][%d]:%d ", i, j, cost[i][j]);
printf("\n");
}
printf("\n");*/
prim(cost, n);
return 0;
}
POJ 1751 Highways(最小生成树Prim普里姆,输出边)的更多相关文章
- ACM第四站————最小生成树(普里姆算法)
对于一个带权的无向连通图,其每个生成树所有边上的权值之和可能不同,我们把所有边上权值之和最小的生成树称为图的最小生成树. 普里姆算法是以其中某一顶点为起点,逐步寻找各个顶点上最小权值的边来构建最小生成 ...
- 经典问题----最小生成树(prim普里姆贪心算法)
题目简述:假如有一个无向连通图,有n个顶点,有许多(带有权值即长度)边,让你用在其中选n-1条边把这n个顶点连起来,不漏掉任何一个点,然后这n-1条边的权值总和最小,就是最小生成树了,注意,不可绕成圈 ...
- hdu 1233:还是畅通工程(数据结构,图,最小生成树,普里姆(Prim)算法)
还是畅通工程 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- 查找最小生成树:普里姆算法算法(Prim)算法
一.算法介绍 普里姆算法(Prim's algorithm),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值之 ...
- JS实现最小生成树之普里姆(Prim)算法
最小生成树: 我们把构造连通网的最小代价生成树称为最小生成树.经典的算法有两种,普利姆算法和克鲁斯卡尔算法. 普里姆算法打印最小生成树: 先选择一个点,把该顶点的边加入数组,再按照权值最小的原则选边, ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 图的普里姆(Prim)算法求最小生成树
关于图的最小生成树算法------普里姆算法 首先我们先初始化一张图: 设置两个数据结构来分别代表我们需要存储的数据: lowcost[i]:表示以i为终点的边的最小权值,当lowcost[i]=0说 ...
- 最小生成树 Prim(普里姆)算法和Kruskal(克鲁斯特尔)算法
Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...
- 图解最小生成树 - 普里姆(Prim)算法
我们在图的定义中说过,带有权值的图就是网结构.一个连通图的生成树是一个极小的连通子图,它含有图中全部的顶点,但只有足以构成一棵树的n-1条边.所谓的最小成本,就是n个顶点,用n-1条边把一个连通图连接 ...
随机推荐
- K8S集群搭建
K8S集群搭建 摘要 是借鉴网上的几篇文章加上自己的理解整理得到的结果,去掉了一些文章中比较冗余的组件和操作,力争做到部署简单化. K8S组件说明 Kubernetes包含两种节点角色:master节 ...
- Go中的Package和Module分析
Package 所谓package(包)其实就是代码的一种组织管理方式,代码多了就需要放入文件,文件多了就需要归类放入文件夹,就好比我们在给电脑装软件时会进行归类安装,其实也是有意无意对电脑软件安装的 ...
- [MariaDB]MHA高可用部署-实验
目录 一.简介 1.1MHA角色 二.MHA的工具 三.MHA部署过程 3.1.1 配置 3.1.2 环境规划 3.1.3 配置一主多从 3.2 MHA配置 3.2.1 master权限授予 3.2. ...
- linux下安装cmake方法(1)---下载压缩包
OpenCV 2.2以后的版本需要使用Cmake生成makefile文件,因此需要先安装cmake:还有其它一些软件都需要先安装cmake 1.在linux环境下打开网页浏览器,输入网址:http:/ ...
- react 表单(受控组件和非受控组件)
我们知道表单元素与其他的普通DOM元素来说是不一样的,它们保存了自己的一些状态. 我们主要说的就是表单元素中的受控组件和非受控组件. 受控组件就是这个组件的状态是我们(react)控制的,这个组件的行 ...
- 《C++Primer》第五版习题解答--第四章【学习笔记】
[C++Primer]第五版习题解答--第四章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/11 第四章:表达式 练习4. ...
- Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import ...
- Faster Rcnn训练自己的数据集过程大白话记录
声明:每人都有自己的理解,动手实践才能对细节更加理解! 一.算法理解 此处省略一万字.................. 二.训练及源码理解 首先配置: 在./lib/utils文件下....运行 p ...
- 机器学习——提升方法AdaBoost算法,推导过程
0提升的基本方法 对于分类的问题,给定一个训练样本集,求比较粗糙的分类规则(弱分类器)要比求精确的分类的分类规则(强分类器)容易的多.提升的方法就是从弱分类器算法出发,反复学习,得到一系列弱分类器(又 ...
- 第7节class与style绑定
方法一 效果图: 方法二 效果图: 方法三 效果图: 代码: <!DOCTYPE html> <html lang="en" xmlns:v-bind=&qu ...