zoj 1203 Swordfish prim算法
#include "stdio.h".
#include <iostream>
#include<math.h>
using namespace std;
double dis[105][105];
double p[105][2];//点的坐标
double ans;//最短距离
int n;
void makedis()//算出两个点之间的距离
{
int i, j; double r;
for (i = 0; i < n; i++)
for (j = i; j <n; j++)
{
r = sqrt((p[i][0] - p[j][0])*(p[i][0] - p[j][0]) + (p[i][1] - p[j][1])*((p[i][1] - p[j][1])));
dis[i][j] = r;// i 点到j 点 的距离
dis[j][i] = r;
}
} void prim()
{
int temp[105]; //存放已经加入的结点
int size; // 已加入的结点个数
int i, j, k;
int curnode, pos2;
double min; temp[0] = 0;
size = 1; dis[0][0] = 1; for (i = 0; i < n - 1; i++)//执行n-1次将所有的点访问完
{
min = 32767; // 极大值
for (j = 0; j < size; j++)
{
curnode = temp[j];
for (k =0; k < n; k++)
if (dis[curnode][k] <= min && dis[k][k] == 0) //min 为当前最小值,为0表示没有访问过,如果新加入结点后有再小的边就将对应的点加入
{
min = dis[curnode][k];
pos2 = k;
}
}
ans += min;
dis[pos2][pos2] = 1;
temp[size] = pos2; size++; }
} int main()//主程序
{
int step;
int i;
step = 0;
while (scanf("%d", &n))//输入点的个数,为0时结束
{
step++;
ans = 0;//距离总和
if (n == 0) break;
if (step != 1)printf("\n");
for (i = 0; i <n; i++)//输入n 个点的坐标
{
scanf("%lf%lf", &p[i][0], &p[i][1]);
}
makedis();
prim();
printf("Case #%d:\n", step);
printf("The minimal distance is: %.2lf\n", ans);
}
return 0;
}
zoj 1203 Swordfish prim算法的更多相关文章
- ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法
主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...
- ZOJ 1203 Swordfish(Prim算法求解MST)
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- ZOJ 1203 Swordfish
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- ZOJ 1203 Swordfish MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1203 大意: 给出一些点,求MST 把这几天的MST一口气发上来. kru ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- 最小生成树のprim算法
Problem A Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。
//归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...
- 最小生成树——prim算法
prim算法是选取任意一个顶点作为树的一个节点,然后贪心的选取离这棵树最近的点,直到连上所有的点并且不够成环,它的时间复杂度为o(v^2) #include<iostream>#inclu ...
- 洛谷 P3366 【模板】最小生成树 prim算法思路 我自己的实现
网上有很多prim算法 用邻接矩阵 加什么lowcost数组 我觉得不靠谱 毕竟邻接矩阵本身就不是存图的好方法 所以自己写了一个邻接表(边信息表)版本的 注意我还是用了优先队列 每次新加入一个点 ...
随机推荐
- postfix config
smtp auth success,other not sure service saslauthd start [root@localhost ~]# cat /etc/sysconfig/sas ...
- Silverlight TreeView 动态绑定Xml 文件
随着应用程序的不断升级,客户的需求不断增多,程序员不得不对自己的应用程序做出相应的修改,如果修改的内容较多,那么就必须找出一种简便方法,下面就为大家介绍一下在SilverLight 中左边导航栏T ...
- git的那点事---
HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id. 穿梭前,用git log可以查看提交历史,以便确定要回退到哪个 ...
- jQuery的扩展与noConflict
jQuery的扩展 noConflict
- Qt信号与槽应用实例一
..... connect(m_pGlobelWidget,signal(globeControlClick(object,object)),this,slot(globeControlClick(o ...
- [Effective JavaScript 笔记]第63条:当心丢弃错误
管理异步编程的一个是错误处理.同步代码中只要使用try语句块包装一段代码很容易一下子处理所有的错误. try{ f(); g(); h(); } catch(e){ //这里用来下得出现的错误 } t ...
- Python—模块
一.模块 模块,是用一堆代码实现了某个功能的代码集合,模块分为三种:自定义模块(自己定义).内置模块(python自带).开源模块 导入模块 (1).导入一个py文件,解释器解释该py文件 (2).导 ...
- 移动web前端之meta标签
最近这段时间忙着做web移动端,东西跟pc端还是有区别的.这个月也学到了不少东西,太多了就从头开始,先总结meta标签吧. 主要标签内容和注释如下: <meta charset="UT ...
- WampServer中MySQL中文乱码解决
1.修改mysql的my.ini文件: 在 [client] 下面增加 default-character-set=utf8 在 [mysqld] 下面增加: character_set_server ...
- asp.net 之 购物车
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...