思路:

一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim。

Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是每次找到B中的一个点,满足这个点到A的权值是B到A的权值中最小的,然后我们把这个点加入到A,再更新B中的点到A的最小距离。

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int N = 750+5;
int mp[N][N]; //i j的距离
int vis[N];
int dis[N]; //点到集合的最小距离
int lian[N];
int n;
struct node{
int x,y;
}p[N];
void init(){
for(int i = 1;i <= n;i++){ //建图
scanf("%d%d",&p[i].x,&p[i].y);
for(int j = 1;j <= i;j++){
mp[i][j] = mp[j][i] = (p[i].x - p[j].x)*(p[i].x - p[j].x) + (p[i].y - p[j].y)*(p[i].y - p[j].y);
}
}
int Q,a,b;
scanf("%d",&Q);
while(Q--){
scanf("%d%d",&a,&b);
mp[a][b] = mp[b][a] = 0;
}
memset(vis,0,sizeof(vis)); //标记该点是否连接
for(int i = 1;i <= n;i++){
dis[i] = mp[i][1];
lian[i] = 1; //初始都和1的距离最小
}
vis[1] = 1;
}
void Prim(){
for(int i = 1;i < n;i++){ //找n-1条边
int MIN = 100000005;
int point;
for(int j = 1;j <= n;j++){
if(!vis[j] && dis[j] < MIN){
MIN = dis[j];
point = j;
}
}
vis[point] = 1; //找到了
if(mp[point][lian[point]] != 0) //为零是已经有路的
printf("%d %d\n",point,lian[point]); //点和他相连的点
for(int j = 1;j <= n;j++){
if(!vis[j] && mp[j][point] < dis[j]){
lian[j] = point;
dis[j] = mp[j][point];
}
}
}
}
int main(){
scanf("%d",&n);
init();
Prim();
return 0;
}

POJ 1751 Highways(最小生成树&Prim)题解的更多相关文章

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

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

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

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

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

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

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

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

  5. POJ 1751 Highways (kruskal)

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

  6. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

  7. Building a Space Station POJ 2031 【最小生成树 prim】

    http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...

  8. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

  9. (poj) 1751 Highways

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

随机推荐

  1. python修饰器各种实用方法

    This page is meant to be a central repository of decorator code pieces, whether useful or not <wi ...

  2. 【Python】xpath中为什么粘贴进去代码后老报错?如何在定位元素的时候准确找到定位切入点?

    1. xpath后()中双引号("")里面不能套用双引号(""),把里面的双引号改成单引号('')报错就没有了. 2.如何在定位元素的时候准确找到定位切入点? ...

  3. python len() 函数

    Python len() Python len() 方法返回对象(字符.列表.元组等)长度或项目个数. len(obj) 方法语法 obj -- 对象(字符串.列表.元组.字典等) 字符串长度 > ...

  4. java获取屏幕密度

    方法1: float xdpi = getResources().getDisplayMetrics().widthPixels;float ydpi = getResources().getDisp ...

  5. Python3学习之路~2.6 集合操作

    集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 常用操作 >>> list1 = ...

  6. 9 jmeter之检查点

    jmeter有类似loadrunner检查点的功能,就是断言中的响应断言. 1.响应断言(对返回文字结果进行相应的匹配)右击请求-->添加-->断言-->响应断言-->添加“要 ...

  7. javascript-js模拟form页面提交跳转

    window.location.href跳转到另外一个界面.但直接传递get方法会暴露数据 下面可以实现跳转的效果,却又能够通过post传递方法隐藏数据. 有一个不足就是,在跳转到新页面后,点击“返回 ...

  8. Iterator源码解读

    //继承关系 public interface Inteator { boolean hasNext(); Object next(); } public interface Iterable { I ...

  9. PAT Waiting in Line[转载]

    //转自:https://blog.csdn.net/apie_czx/article/details/45537627 1014 Waiting in Line (30)(30 分)Suppose ...

  10. MySQL从删库到跑路_高级(五)——触发器

    作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.触发器简介 1.触发器简介 触发器是和表关联的特殊的存储过程,可以再插入,删除或修改表中的数据时触发执行,比数据 ...