Highways---poj1751最小生成树
http://poj.org/problem?id=1751
题意:有n个点,已知各点坐标,距离为权值,求最小生成树的边
但是这个最小生成树的m条边是已经确定的了,所以可以让已知边的权值为0;
在Prim算法中改一下就行
#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 1100
#define INF 0xfffffff using namespace std; int vis[N], n, pre[N];//pre[i]记录的是i的父节点;
double maps[N][N],dist[N]; void Prim(int start)
{
vis[start] = ;
for(int i=; i<=n; i++)
{
dist[i] = maps[start][i];
pre[i] = start;
}
for(int i=; i<=n; i++)
{
double Min = INF;
int index = -;
for(int j = ; j <= n; j++)
{
if(vis[j]== && Min>dist[j])
{
Min = dist[j];
index = j;
}
}
if(index == -) break;
if(Min != )
printf("%d %d\n", pre[index],index);
vis[index] = ;
for(int j=; j<=n; j++)
{
if(vis[j]== && dist[j] > maps[index][j])
dist[j] = maps[index][j], pre[j] = index;
}
}
} struct node
{
int x, y;
}a[N]; void Init()
{
for(int i=;i<=n;i++)
{
pre[i] = i;
vis[i] = ;
dist[i] = INF;
for(int j=; j<=n; j++)
maps[i][j] = INF;
maps[i][i] = ;
}
} int main()
{
int i, j, x, y, m;
double d;
while(scanf("%d", &n)!=EOF)
{
Init();
memset(a,,sizeof(a));
for(i=;i<=n;i++)
scanf("%d%d", &a[i].x, &a[i].y);
for(i=; i<=n; i++)
{
for(j=i+; j<=n; j++)
{
d = sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
maps[i][j] = maps[j][i] = d;
}
}
scanf("%d", &m);
for(i=; i<m; i++)
{
scanf("%d%d", &x,&y);
maps[x][y] = maps[y][x] = ; //把已知边的权值置为0;
}
Prim();
}
return ;
}
Highways---poj1751最小生成树的更多相关文章
- Highways POJ-1751 最小生成树 Prim算法
Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...
- C - Highways poj1751最小生成树
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of p ...
- POJ-1751 Highways(最小生成树消边+输出边)
http://poj.org/problem?id=1751 Description The island nation of Flatopia is perfectly flat. Unfortun ...
- POJ1751 Highways【最小生成树】
题意: 给你N个城市的坐标,城市之间存在公路,但是由于其中一些道路损坏了,需要维修,维修的费用与公路长成正比(公路是直的). 但现有M条公路是完整的,不需要维修,下面有M行,表示不需要维修的道路两端的 ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- POJ 2485:Highways(最小生成树&&prim)
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21628 Accepted: 9970 Descrip ...
- POJ 1751 Highways 【最小生成树 Kruskal】
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23070 Accepted: 6760 Speci ...
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 2485 Highways (求最小生成树中最大的边)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
随机推荐
- MongoDB 备份恢复
备份: mongodump --host -u admin -p -o /tmp/alldb/ // 备份所有的库 mongodump --host -u admin -p -d mydb -o /t ...
- Unity Shader 获取模型空间坐标
CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma ...
- Elasticsearch 配置同义词
配置近义词 近义词组件已经是elasticsearch自带的了,所以不需要额外安装插件,但是想要让近义词和IK一起使用,就需要配置自己的分析器了. 首先创建近义词文档 在config目录下 mkdir ...
- CentoOS6.6安装netcat
http://blog.csdn.net/u013673976/article/details/47084841 CentOS下安装netcat 使用zookeeper过程中,需要监控集群状态.在使用 ...
- UITableView-FDTemplateLayoutCell 学习笔记
本文转载至 http://www.tuicool.com/articles/I7ji2uM 原文 http://everettjf.github.io/2016/03/24/learn-uitabl ...
- 微信-苹果机无法播放声音Audio
今天同事测试活动的时候,背景音效(多个)苹果机无法发出声音,几个手机都试过,没什么用.不过安卓机上倒是没这个问题,正常. 最开始以为是js冲突问题,将页面上所有的东西进行加处理(清空,一个个加进来), ...
- IOS设计模式第五篇之装饰设计模式的代理设计模式
版权声明:原创作品,谢绝转载!否则将追究法律责任. 代理: 另一个装饰设计模式,代理,是一个代表或者协调另一个对象的行为机制.例如当你用一个tableView,你必须实现他里面的一个tableView ...
- mysqlint类型的长度值mysql在建表的时候int类型后的长度代表什么
详解mysql int类型的长度值 mysql在建表的时候int类型后的长度代表什么 是该列允许存储值的最大宽度吗 为什么我设置成int(1), 也一样能存10,100,1000呢. 当时我虽然知道i ...
- intellij2016.03激活
激活的时候采用server的方式 :http://jetbrains.tech
- 百度前端学院js课堂作业合集+分析(更新中...)
第一课:简陋的登录框 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...