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 ...
随机推荐
- 图Lasso求逆协方差矩阵(Graphical Lasso for inverse covariance matrix)
图Lasso求逆协方差矩阵(Graphical Lasso for inverse covariance matrix) 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/ka ...
- SQL必知必会|SQL基础篇
了解SQL DBMS的前世今生 SQL是如何执行的 DDL语法 关于外键的性能问题? 是否使用外键确实会有一些争议.关于外键的使用: 首先,外键本身是为了实现强一致性,所以如果需要正确性>性能的 ...
- 简述ECMAScript6新增特性
1.变量 var 可以重复声明.只有函数级的作用域.存在变量提升 let 不能重复声明.有块级作用域.没有变量提升.变量 const 不能重复声明.具有块级作用域.常量 2.箭头函数 a.为了方便而存 ...
- 解决Django-Error: That port is already in use
Error: That port is already in use. 1.使用python manage.py runserver 8001 开一个新的端口. 2.kill掉原来的端口(在root条 ...
- [C3W1] Structuring Machine Learning Projects - ML Strategy 1
第一周:机器学习策略(1)(ML Strategy(1)) 为什么是ML策略?(Why ML Strategy) 大家好,欢迎收听本课,如何构建你的机器学习项目也就是说机器学习的策略.我希望通过这门课 ...
- 题解:swj社会摇基础第一课
题目链接 思路:dp,f[i]表示构成i所需要的最小步数 //swj么么哒 #include<bits/stdc++.h> using namespace std; int n; cons ...
- 小测试整理(含T1 T2)
这次测试规模较小,前两题也较水,但需要整理 T1(Jelly的男♂难题1): 从一个点出发,以四连通的方式扩散,可以走#,不能走o,走过的格子每单位时间会增加1点高度,问扩散完整间屋子需要的时间,以及 ...
- Rabbitmq 实现延时任务
1.需要用到插件 rabbitmq_delayed_message_exchange 来实现,插件下载地址:https://www.rabbitmq.com/community-plugins.htm ...
- 记录几篇PM文章
今日阅读了几篇关于 PM 的文章感觉还不错,整理.摘录于此: 1.[项目经理和产品经理:https://www.cnblogs.com/joylee/p/3541141.html] ——关于二者的异同 ...
- centos6系统资源限制
1.限制用户CPU使用资源 /etc/security/limits.conf #主配置文件 /etc/security/limits.d/ #子模块配置文件 新建一个普通用户,并查看默认资源限制 [ ...