题目链接: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的更多相关文章

  1. (最小生成树 Prim) Highways --POJ --1751

    链接: http://poj.org/problem?id=1751 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1150 ...

  2. H - Highways - poj 1751(prim)

    某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就 ...

  3. POJ 1751 Highways (最小生成树)

    Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  4. POJ 1751 Highways (最小生成树)

    Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...

  5. POJ 1751 Highways (kruskal)

    题目链接:http://poj.org/problem?id=1751 题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树. 这里用kruskal不会超时 ...

  6. POJ 1751 Highways (ZOJ 2048 ) MST

    http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...

  7. (poj) 1751 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...

  8. POJ 1751 Highways 【最小生成树 Kruskal】

    Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23070   Accepted: 6760   Speci ...

  9. POJ 1751 Highways(最小生成树Prim普里姆,输出边)

    题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...

随机推荐

  1. 23.Java基础_ArrayList类

    ArrayList类的构造和使用方法 import java.util.ArrayList; public class test { public static void main(String[] ...

  2. Vue项目开发前的准备工作,node的安装,vue-cli的安装

    一.安装node 1-  点击这里进入node官网下载 2- 3- 下载完成是这样的 4-  双击打开进行安装,一路next,安装完成是这样 5-  打开cmd进入安装node的文件夹,输入node ...

  3. Spring Boot 自定义Intercepter

    在 SpringBoot2.X 中 ,WebMvcConfigurerAdapter 被deprecated , 更好的做法是 implements WebMvcConfigurer 一.自定义拦截器 ...

  4. 用arthas查看JVM已加载的类及方法信息

    1.sc:“Search-Class” 的简写,这个命令能搜索出所有已经加载到 JVM 中的 Class 信息,这个命令支持的参数有 [d].[E].[f] 和 [x:]. [d] 输出当前类的详细信 ...

  5. Redis与python

    一.Redis介绍 Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库(非关系型数据库). 本质:将数据保存在内存中. 用途:缓存.消息队列. 1.Redis的特点 R ...

  6. 从应用到内核,分析top命令显示的进程名包含中括号"[]"的含义

    背景 在执行top/ps命令的时候,在COMMAND一列,我们会发现,有些进程名被[]括起来了,例如 PID PPID USER STAT VSZ %VSZ %CPU COMMAND 1542 928 ...

  7. System.gc()和Runtime.gc()的区别?

    java.lang.System.gc()只是java.lang.Runtime.getRuntime().gc()的简写,两者的行为没有任何不同 System.gc()和runtime.gc()用于 ...

  8. Oracle中TIMESTAMP时间的显示格式

    Oracle中的TIMESTAMP数据类型很多人用的都很少,所以即使最简单的一个查询返回的结果也会搞不清楚到底这个时间是什么时间点. 例如: 27-1月 -08 12.04.35.877000 上午 ...

  9. HTML+CSS基础 css的尺寸

    css的尺寸 width 宽   height高 Line-light   行高    行高是由三部分构成,上间距.文本高度.下间距. 且上下间距相等.所以文字居中 行高.一旦设置行高了,元素内部必须 ...

  10. redis延迟队列

    异步消息队列 Redis 的 list(列表) 数据结构常用来作为异步消息队列使用,使用rpush/lpush操作入队列, 使用 lpop 和 rpop 来出队列. > rpush notify ...