POJ 1751 Highways(最小生成树&Prim)题解
思路:
一开始用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)题解的更多相关文章
- POJ 1751 Highways(最小生成树Prim普里姆,输出边)
题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...
- 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】
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23070 Accepted: 6760 Speci ...
- POJ 1751 Highways (kruskal)
题目链接:http://poj.org/problem?id=1751 题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树. 这里用kruskal不会超时 ...
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- 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 ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- (poj) 1751 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...
随机推荐
- webpack学习三——output
output的两个参数filename,path 一.path输出路径,输出路径要绝对路径,否则报错.做法如下: path:__dirname + 'path' 二.filename 输出文件命,相对 ...
- 使用PHP创建一个REST API(译)
最近API在网络领域有些风靡,明确的说是REST的影响力.这实在没什么好惊讶的,因为在任何编程语言中,消费REST API都是非常的容易.构建它也非常的简单,因为本质上你不会用到任何那些已存在很久的H ...
- Knight Moves(hdu1372 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others) ...
- hdu1671Phone List(字典树)
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h&g ...
- openstack 部署笔记--dashboard
控制节点 # yum install openstack-dashboard # vim /etc/openstack-dashboard/local_settings OPENSTACK_HOST ...
- 3.keras实现-->高级的深度学习最佳实践
一.不用Sequential模型的解决方案:keras函数式API 1.多输入模型 简单的问答模型 输入:问题 + 文本片段 输出:回答(一个词) from keras.models import M ...
- Postman + newman + jenkins 的API自动化测试应用
一.环境配置 Postman postman 的具体使用可以参考另外一篇文章:postman 做接口测试之学习笔记 Newman 第一步,安装nodejs. 第二步,在nodejs命令行安装newma ...
- jmeter 测试websocket接口(一)
jmeter 测试websocket接口时,需要对jmeter添加测试websocket的jar包. 下载地址: https://download.csdn.net/download/qq_14913 ...
- jQ live用法
我们给元素绑定单击事件.用得最多的都是$("#id").click(function(){}); 但我们动态添加的元素.这样绑定是不行的.必须借助live $("#id ...
- How To View the HTML Source in Google Chrome
Whether you are new to the web industry or a seasoned veteran, viewing the HTML source of different ...