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. 循环次数( M - 暴力求解、打表)

    循环次数 Description           我们知道,在编程中,我们时常需要考虑到时间复杂度,特别是对于循环的部分.例如,         如果代码中出现         for(i=1;i ...

  2. 我的cocos2d-x集成sharesdk之旅(转)

    链接地址:http://blog.csdn.net/yeungxuguang/article/details/18227153 本文出自:http://www.iteye.com/topic/1130 ...

  3. VS 2013--工程的创建,scanf报错,常用快捷键,行号设置

    一.创建一个工程(这里是C++,其他的一样的) 在vs页面上点击 文件-->新建-->项目: 会出现如下界面,自己改名字和存贮位置就可以了 确定,然后点击下一步: 这样就建好了一个工程,然 ...

  4. Executor框架

     Executor框架是指java5中引入的一系列并发库中与executor相关的功能类,包括Executor.Executors.ExecutorService.CompletionService. ...

  5. Kruscal 、 Prime Template

    Kruscal  Template : 很裸的Kruscal Template(求最小生成树中最长路,即最短路中最长路) //#pragma comment(linker, "/STACK: ...

  6. VirtualBox中的Ubuntu没有权限访问共享文件夹/media/sf_bak

    之前已经搞定可以自动共享文件夹了,但是现在发现无法去访问,非root用户下,使用“ls /media/sf_bak”提示没有权限,当然如果切换到root,是可以的. [解决过程]1.把普通用户名加入到 ...

  7. stdcall、cdecl、fastcall、thiscall 、naked call的汇编详解

    函数调用规范   当高级语言函数被编译成机器码时,有一个问题就必须解决:因为CPU没有办法知道一个函数调用需要多少个.什么样的参数.即计算机不知道怎么给这个函数传递参数,传递参数的工作必须由函数调用者 ...

  8. 动画api说明

    1.Animation的API参考文档:http://blog.csdn.net/harvic880925/article/details/40117115 2.动画插值器的参考: http://bl ...

  9. Datagridview列绑定数据

    属性最下面的Column项: 把每一列的字段绑定,更改显示的标题. 数据绑定代码: string sql = "select IncomeExpendTypeID , TypeName , ...

  10. Linux命令: chown

    touch auth.log root@ubuntu:/work# ls -l auth.log -rw-r--r-- 1 root root 0 Feb 18 19:27 auth.log chow ...