题目链接:http://acm.hrbeu.edu.cn/index.php?act=problem&id=1223

 #include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector> #define maxn 115
using namespace std; const int INF = 0x3f3f3f; struct Point{
double x,y;
bool operator < (const Point & rh) const{
return y < rh.y || (y == rh.y && x < rh.x);
}
}p[maxn];
int n;
double ans;
double G[maxn][maxn]; double calculate(int i,int j){
double xx = p[j].x - p[i].x;
double yy = p[j].y - p[i].y;
return sqrt(xx*xx + yy*yy);
} void prim(){
bool vis[maxn];
double lowdist[maxn];
double mindist;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) lowdist[i]=INF;
int point;
int s=;
vis[s] =true;
int num=;
while(true){
if(num == n) break;
vis[s]=true;
mindist = INF;
for(int i=;i<=n;i++){
if(!vis[i] && lowdist[i] > G[s][i]){
lowdist[i] = G[s][i];
}
if(!vis[i] && mindist > lowdist[i]){
mindist=lowdist[i];
point = i;
}
}
s = point;
ans += mindist;
num++;
}
return;
}
int main()
{
// if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} while(scanf("%d",&n)== && n){
for(int i=;i<=n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++){
G[i][j] = G[j][i] = calculate(i,j);
}
ans = ;
//print();
prim();
printf("%.2lf\n",ans);
}
}

prim模板题的更多相关文章

  1. POJ 1258 Agri-Net 【Prime】模板题

    题目链接>>> 题目大意:     给你N*N矩阵,表示N个村庄之间的距离.FJ要把N个村庄全都连接起来,求连接的最短距离(即求最小生成树).解析如下: #include <c ...

  2. POJ 1258:Agri-Net Prim最小生成树模板题

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45050   Accepted: 18479 Descri ...

  3. poj 1679 The Unique MST (次小生成树模板题)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  4. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  5. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  6. POJ2774 & 后缀数组模板题

    题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...

  7. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  8. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  9. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

随机推荐

  1. Bootstrap Table的例子(转载)

    转载自:http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#classes-table 使用的API: data1.json da ...

  2. 用PHP实现一个高效安全的ftp服务器(一)

    摘要: 本文主要阐述使用PHP的swoole扩展实现ftp服务器,同时扩展ftp服务器个性化功能和安全性.真正实现一个自己完全掌控的ftp服务器,可以个性化定制的ftp服务器. 正文: FTP服务器想 ...

  3. Python:标准数据类型6种

    #!/usr/bin/python3 #python的基本语法和数据类型 #python3中 一行有多个语句,用分号分割(;) print("aaa") ;print(" ...

  4. SGU 142.Keyword

    时间限制:0.5s 空间限制:16M 题意 给出一个仅由'a',‘b’组成的字符串S,长度小于500 000,求一个由‘a’,‘b’组成的不是S子串的字符串T. 输出T的长度和T. Sample In ...

  5. Codeforces 475 D.CGCDSSQ

    题目说了a的范围小于10^9次方,可实际却有超过的数据...真是醉了 算出以f[i]结尾的所有可能GCD值,并统计: f[i]可以由f[i-1]得出. /* 递推算出所有GCD值,map统计 */ # ...

  6. java File delete 无法删除文件的原因。

    windows下使用java.io.File.delete()方法删除文件时,返回值为true. 但是本地文件仍然存在,也就是说没有删除成功. 这时候你要检查下你传进来的文件目录格式是否正确. 正确: ...

  7. php foreach 使用&(与运算符)引用赋值要注意的问题

    首先了解一下“引用赋值”,看一个例子: <?php <?php $a=123; $a=123; $b=$a; $b=&$a; $a=321; $a=321; echo"$ ...

  8. Unable to find the ncurses libraries or the required header files解决

    问题: 解决方法: sudo apt-get install ncurses-dev 参考:Unable to find the ncurses libraries or the required h ...

  9. [Usaco2006 Dec]Milk Patterns

    [Usaco2006 Dec]Milk Patterns Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天 产奶的质量,但连续的若干天的 ...

  10. GO:格式化代码

    http://www.ituring.com.cn/article/39380 Go 开发团队不想要 Go 语言像许多其它语言那样总是在为代码风格而引发无休止的争论,浪费大量宝贵的开发时间,因此他们制 ...