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 ...
随机推荐
- Apache(基于IP地址)
1.Apache的安装 (1)先用命令“cat /etc/passwd|grep apache”,查看有有没有Apache ①如果有Apache,我们就不用再安装 ②如果没有,我们就安装一下 (2)使 ...
- Pwn-Tell Me Something
题目地址 https://dn.jarvisoj.com/challengefiles/guestbook.d3d5869bd6fb04dd35b29c67426c0f05 先看看位数 64位ELF ...
- oracle 数据库下所有表结构、数据量及缺失值统计
表结构 SELECT t1.TABLE_NAME, t1.COLUMN_NAME, t1.DATA_TYPE || '(' || t1.DATA_LENGTH || ')', t2.COMMENTS ...
- 面向对象软件构造 (Bertrand Meyer 著)
Part A: The Issues 议题 第一章 软件品质 第二章 面向对象的标准 Part B: The Road To Object Orientation 通向面向对象之路 第三章 模块性 第 ...
- Paper | Making a "Completely Blind" Image Quality Analyzer
目录 1. 技术细节 1.1 NSS特征 1.2 选择锐利块来计算NSS 1.3 一张图像得到36个特征 1.4 用MVG建模这36个特征 1.5 NIQE指标 2. 实验 质量评估大佬AC Bovi ...
- Istio Routing极简教程
官网文档: https://istio.io/docs/reference/config/networking/#VirtualService 在学习像Istio这样的新技术时,看一下示例应用程序总是 ...
- 基于Django的Rest Framework框架的版本控制
本文目录 一 作用 二 内置的版本控制类 三 局部使用 四 全局使用 五 示例 源码分析 回到目录 一 作用 用于版本的控制 回到目录 二 内置的版本控制类 from rest_framework.v ...
- 卡尔曼滤波C++代码
#include <ros/ros.h> #include <string> #include <stdlib.h> #include <iostream&g ...
- Linux网络编程基础API
第5章 Linux网络编程基础API 探讨Linux网络编程基础API与内核中TCP/IP协议族之间的关系,并未后续章节提供编程基础.从3个方面讨论Linux网络API. socket地址API.so ...
- 面试官:说说Mysql数据库分库分表,并且会有哪些问题?
之前一篇文章已经谈到了数据库集群之主从集群也就是读写分离,也提到了读写分离其实只是分担了访问的压力,但是存储的压力没有解决. 存储的压力说白了就是随着系统的演化,需求的增加,可能表的数量会逐渐增多,比 ...