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 ...
- [PHP] Laravel5.5 使用 laravel-cors 实现 Laravel 的跨域配置
Laravel5.5 使用 laravel-cors 实现 Laravel 的跨域配置 最开始的时候,我使用的是路由中间件的方式,但是发现中间件不起作用 这是之前使用的方式: 'cros' => ...
- Vue v-for操作对象与数值
<!doctype html> <html lang="en"> <head id="head"> <meta cha ...
- 机器学习之感知器和线性回归、逻辑回归以及SVM的相互对比
线性回归是回归模型 感知器.逻辑回归以及SVM是分类模型 线性回归:f(x)=wx+b 感知器:f(x)=sign(wx+b)其中sign是个符号函数,若wx+b>=0取+1,若wx+b< ...
- vue 路由跳转到本页面,ts 监听路由变化
@Watch('$route') routechange(to: any, from: any) { //参数不相等 if (to.query.name!=from.query.name) { //t ...
- 在macOS苹果电脑上安装Azure DevOps Server(TFS)代理
1. 概述 MacOS是一套运行于苹果Macintosh系列电脑上的操作系统,是首个在商用领域成功的图形用户界面操作系统.Iphone应用软件的开发人员,都使用运行macOS的电脑或mini盒子进行软 ...
- 【Easyexcel】java导入导出超大数据量的xlsx文件 解决方法
解决方法: 使用easyexcel解决超大数据量的导入导出xlsx文件 easyexcel最大支持行数 1048576. 官网地址: https://alibaba-easyexcel.github. ...
- SqlServer 创建数据库两种方式
一个SqlServer 数据库实例大概可以创建三万多个数据库. 创建数据库的第一种方式:SqlServer Management Studio管理工具进行可视化创建. 1).打开数据库管理工具,在&q ...
- 一篇文章,带你玩转MVVM,Dapper,AutoMapper
一.背景 由于现在做的项目都是采用WPF来进行UI设计,开发过程中都是基于MVVM来进行开发,但是项目中的MVVM并不是真正的把实体和视图进行解耦,而是将实体和视图完全融合起来,ViewModel只是 ...
- Winform中设置ZedGraph多条Y轴时与多条曲线一一对应
场景 Winform中实现ZedGraph的多条Y轴(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1001322 ...