hdu 1875 Krustal最小生成树

http://acm.hdu.edu.cn/showproblem.php?pid=1875

题意:求链接所有岛屿的最小生成树。中文题就不说了。

分析:kruskal算法。

用sqrt的时候ce了一次。然后数组开小了re了一次。不应该犯这种低级错误了。反思己过。

View Code
// I'm lanjiangzhou
//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
//C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>
#include <list>
#include <queue>
#include <map>
#include <vector>
#include <deque>
#include <set>
using namespace std;

//*************************OUTPUT*************************
#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif

//**************************CONSTANT***********************
#define INF 0x3f3f3f3f

// aply for the memory of the stack
//#pragma comment (linker, "/STACK:1024000000,1024000000")
//end

const int maxn = 2010;
struct node{
    int u,v;
    double w;
}edges[maxn*4];
int pa[maxn];
int n,m;
double x[maxn],y[maxn];
double sumweight=0;

//初始化
void UFset(){
    for(int i=0;i<n;i++){
        pa[i]=-1;
    }
}

//查找
int findset(int x){
    int s;
    for(s=x;pa[s]>=0;s=pa[s]);
    while(s!=x){
        int tmp=pa[x];
        pa[x]=s;
        x=tmp;
    }
    return s;
}

//合并
void Union(int R1,int R2){
    int r1=findset(R1),  r2=findset(R2);
    int tmp=pa[r1]+pa[r2];
    if(pa[r1]>pa[r2]){
        pa[r1]=r2;
        pa[r2]=tmp;
    }
    else {
        pa[r2]=r1;
        pa[r1]=tmp;
    }
}

int cmp(const void*a,const void*b){
    node aa=*(const node*)a;
    node bb=*(const node*)b;
    if(aa.w>bb.w) return 1;
    else return -1;
}

void Kruskal(){
    int num=0;
    int u,v;
    UFset();
    for(int i=0;i<m;i++){
        u=edges[i].u;  v=edges[i].v;
        if(findset(u)!=findset(v)){
            sumweight+=edges[i].w; num++;
            Union(u,v);
        }
        if(num>=n-1) break;
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&x[i],&y[i]);
        }
        int mi=0;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                double d=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
                if(d>=10.0&&d<=1000.0){
                    edges[mi].u=i;  edges[mi].v=j;  edges[mi].w=d;
                    mi++;
                   // printf("edges[mi]=%lf\n",d);
                }
                else continue;
            }
        }
        m=mi;
        qsort(edges,m,sizeof(edges[0]),cmp);
        sumweight=0.0;
        Kruskal();
        if(sumweight==0.0) printf("oh!\n");
        else
            printf("%.1lf\n",sumweight*100);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值