poj 3771 http://poj.org/problem?id=3771

wiki Prim http://zh.wikipedia.org/wiki/%E6%99%AE%E6%9E%97%E5%A7%86%E7%AE%97%E6%B3%95

prim 算法水题

之前写过类似的题,一直以为直接暴力,从遍历所有的点为起点,寻找距离起点最短的点然后再在改点上寻找最短距离 这摆明了是划线嘛……根本不是图论啊啊啊!!!!

这样真是萌萌哒……

正确的做法是先枚举所有的点作为起点,然后把距离长的点更新

这是主要的思路代码

int lowcost[MAXN];
int prim(int v0){
for(int i = ;i < n;i++){ //初始化
lowcost[i] = cost[v0][i];
}
int ans = ;
for(int i = ;i < n-;i++){
int mindis = INF;
int minone; //用来标记最小距离
for(j = ;j < n;j++){
if(lowcost[j] && mindis > lowcost[j]){
mindis = lowcost[j];
minone = j;
}
ans += lowcost[minone];
lowcost[minone] = ;
for(int j = ;j < n;j++){ //进行更新
if(cost[minone][j] < lowcost[j]){
lowcost[j] = cost[j][minone];
}
}
}
}
return ans;
}

代码:非原创……!!!!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib> const int MAXN = + ;
const double ESP = 10e-;
const double Pi = atan(1.0) * ;
const int INF = 0xffffff;
const int MOD = ; using namespace std;
struct Point{
int x;
int y;
};
Point a[MAXN];
bool vis[MAXN];
int n;
double graph[MAXN][MAXN];
double getl(int i,int j){
int x = a[i].x - a[j].x;
int y = a[i].y - a[j].y;
return sqrt(1.0*x*x+1.0*y*y);
}
double prim(int x){
int mark;
bool vis[MAXN];
memset(vis,,sizeof(vis));
vis[x] = ;
double dis[MAXN];
for(int i = ;i < n;i++){
dis[i] = graph[i][];
}
vis[] = ;
if(!x){
for(int i = ;i < n;i++){
dis[i] = graph[i][];
}
vis[] = ;
}
double MIN,SUM = ;
for(int k = ;k < n-;k++){
MIN = INF;
for(int i = ;i < n;i++){
if(!vis[i] && MIN > dis[i]){
MIN = dis[i];
mark = i;
}
}
SUM += MIN;
vis[mark] = ;
for(int i = ;i < n;i++){
if(!vis[i] && dis[i] > graph[mark][i]){
dis[i] = graph[mark][i];
}
}
}
return SUM;
}
int main(){
// freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = ;i < n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
}
for(int i = ;i < n;i++){
for(int j = ;j <= i;j++){
graph[i][j] = graph[j][i] = getl(i,j);
}
}
double h,g = INF;
for(int i = ;i < n;i++){
h = prim(i);
g = min(g,h);
}
printf("%.2lf\n",g);
}
return ;
}

Poj 3771 hdu 3405的更多相关文章

  1. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  2. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  3. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  4. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  5. POJ 1308&&HDU 1272 并查集判断图

      HDU 1272 I - 小希的迷宫 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  6. POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3

    http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...

  7. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  8. POJ 1904 HDU 4685

    这两道题差不多,POJ这道我很久以前就做过,但是比赛的时候居然没想起来.. POJ 这道题的题意是,N个王子每个人都有喜欢的公主,当他们选定一个公主结婚时,必须是的剩下的人也能找到他喜欢的公主结婚. ...

  9. 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms

    题目链接: http://poj.org/problem?id=1006 http://acm.hdu.edu.cn/showproblem.php?pid=1370 题目大意: (X+d)%23=a ...

随机推荐

  1. ceph之crush map

    编辑crush map: 1.获取crush map: 2.反编译crush map: 3.至少编辑一个设备,桶, 规则: 4.重新编译crush map: 5.重新注入crush map: 获取cr ...

  2. git 使用过程(三、文件的添加 修改)

    1.库中添加文件 在目录下新建一个文件 如 testfile.txt .输入命令:① git add testfile.txt  ②git commit -m "这里是你提交的说明" ...

  3. asp.net mvc 下载文件 txt doc xsl 等等

      不废话,直接上代码,就是这么简单 public FileStreamResult StreamFileFromDisk() { // string path = AppDomain.Current ...

  4. GIT在windows下搭建

    /*********工具准备********* *copSSH *msysgit *TortiseGIT *putty * 安装比较简单,此处省略... *********************** ...

  5. 【 D3.js 入门系列 — 3 】 做一个简单的图表!

    图1. 柱形图 1. 柱形图 前几章的例子,都是对文字进行处理.本章中将用 D3 做一个简单的柱形图.制作柱形图有很多种方法,比如用 HTML 的 <div> 标签,或在 SVG 上绘制 ...

  6. 我的Python成长之路---第四天---Python基础(14)---2016年1月23日(寒风刺骨)

    一.生成器和迭代器 1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退. ...

  7. 使用 getopt() 进行命令行处理

    引言 在早期的 UNIX® 中,其命令行环境(当时的唯一用户界面)包含着数十种小的文本处理工具.这些工具非常小,通常可很好地完成一项工作.这些工具通过较长的命令管道链接在一起,前面的程序将其输出传递给 ...

  8. 鹅厂揭秘——高端大气的App电量測试

    怎样评价我们开发出来的应用是耗电还是不耗电,怎样測试?这就是我们今天讨论的主题--电量測试,一个在移动应用中新出现的測试类型. 作者简单介绍 watermark/2/text/aHR0cDovL2Js ...

  9. arm:jlink调试和直接烧写运行的不同 [mdk s3c2440]

    1.对全局变量的初始化. 2.还没发现的事例. /*************************************************/ 先上连接文件sct LR_ROM1 0x3000 ...

  10. Creating Spatial Indexes(mysql 创建空间索引 The used table type doesn't support SPATIAL indexes)

    For MyISAM tables, MySQL can create spatial indexes using syntax similar to that for creating regula ...