Highways POJ - 1751
题目链接:https://vjudge.net/problem/POJ-1751
思路:
最小生成树板子,只需要多记录每个dis[x]的权值是从哪个点到x这个点的。
#include <stdio.h>
#include <iostream>
#include <queue>
#include <math.h>
#include <algorithm>
using namespace std; const int N = (int)1e3 + ;
const int inf = (int)1e9;
double px[N];
double py[N];
double g[N][N];
bool vis[N];
int n,m;
int s; struct info{
int from;
double w;
}dis[N]; struct node{
int loc;
double w;
bool friend operator<(const node & a,const node& b){
return a.w > b.w;
}
};
priority_queue<node > que; inline double dist(int a,int b){
return sqrt((px[a]-px[b])*(px[a]-px[b])+(py[a]-py[b])*(py[a]-py[b]));
} void build_map(){ for(int i = ; i <= n; i++)
for(int j = i + ; j <= n; j++)
g[i][j] = g[j][i] = dist(i,j); for(int i = ; i <= n; i++) g[i][i] = ;
} void prime(){ for(int i = ; i <= n; i++){
vis[i] = ;
dis[i].w = inf;
} if(!s) s = ; que.push(node{s,});
dis[s].from = ;
dis[s].w = ; while(!que.empty()){
int u = que.top().loc;
que.pop();
vis[u] = ; for(int v = ; v <= n; v++){
if(!vis[v] && dis[v].w > g[u][v]){
dis[v].w = g[u][v];
dis[v].from = u;
que.push(node{v,dis[v].w});
}
}
} for(int i = ; i <= n; i++){
if(dis[i].w == ) continue;
printf("%d %d\n",dis[i].from,i);
}
} int main(){ scanf("%d",&n); for(int i = ; i <= n; i++)
scanf("%lf%lf",&px[i],&py[i]); build_map(); scanf("%d",&m);
int u,v; for(int i = ; i <= m ;i++){
scanf("%d%d",&u,&v);
s = u;
g[u][v] = g[v][u] = ;
} prime();
return ;
}
Highways POJ - 1751的更多相关文章
- (最小生成树 Prim) Highways --POJ --1751
链接: http://poj.org/problem?id=1751 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1150 ...
- H - Highways - poj 1751(prim)
某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就 ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- POJ 1751 Highways (kruskal)
题目链接:http://poj.org/problem?id=1751 题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树. 这里用kruskal不会超时 ...
- 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 ...
- 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 ...
随机推荐
- 201271050130-滕江南-《面向对象程序设计(java)》第十六周学习总结
201271050130-滕江南-<面向对象程序设计(java)>第十六周学习总结 博文正文开头格式:(2分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.c ...
- 创建testng.xml文件
简单介绍 运行TestNG测试脚本有两种方式:一种是直接通过IDE运行(例如使用eclipse中的“Run TestNG tests”),另一种是从命令行运行(通过使用xml配置文件).当我们想执行某 ...
- ASP.NET开发实战——(十)ASP.NET MVC 与数据库之MySQL&EF
之前介绍EF时介绍了provider这个配置项,而且也介绍了在ASP.NET访问MySQL数据仍然是通过ADO.NET,不同的地方仅仅是更换了MySQL的数据提供器,那么在EF中是否也只需更换“提供器 ...
- Programming | 获取图像频域并分解为高低频
1. DCT变换 参考:https://stackoverflow.com/questions/22322427/decomposing-an-image-into-two-frequency-com ...
- Spring的工具类StringUtils使用
我们经常会对字符串进行操作,spring已经实现了常用的处理功能.我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串. 工具类整理如下: ...
- ros局部路径规划-DWA学习
ROS的路径规划器分为全局路径和局部路径规划,其中局部路径规划器使用的最广的为dwa,个人理解为: 首先全局路径规划会生成一条大致的全局路径,局部路径规划器会把全局路径给分段,然后根据分段的全局路径的 ...
- LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words in a String
公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 1: 输入: ...
- Wamp 下运行 CGI 笔记
虽然假期的余额不足了,但是仔细想想放假又有多少事情可以做呢?休息的差不多了,还是上班的好,长时间的休息人就废了.同意的举手,不同意的就算了. httpd.conf 的配置 我这里使用的是 Wamp 的 ...
- conda基本知识
卸载anaconda: rm -rf anaconda3 (anaconda文件夹名称) conda删除虚拟环境 在终端执行:conda remove -n your_env_name(虚拟环境名称) ...
- SqlHelper发布——比你期望的还要多的多(例如比MyBatis-Pagehelper性能更高)
SqlHelper发布——比Mybatis-PageHelper性能更高 起源 前段时间开启了一个新的项目,在选择分页插件时,发现github上很流行的一个是pagehelper,在百度上搜索了一下, ...