NEU 1222: Sweep the snow PRIM()

题目描述:

After the big big snow yesterday, NEU became a beautiful silver world.In the morning, poor michaelalan got a short message which told him to sweep the whole school snow and make the traffic unblocked. As you see, it needs so much time to do by one person.There are some points need clear,and these points must connected together(eg:If there are A point and B point, then sweep a line between A and B is enough). So he wants to know the minimum length of snow he needs to sweep to ensure the traffic unblocked (the width of snow is ignored).


PRIM()算法模板题




#include<stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#define MAX 500000.0
int N;
double s_V[1010];
int V[1010]; 
double map[1010][1010];
 
struct node{
    float x;
    float y;
}point[1010];
 
double cal(int i,int j){
    return sqrt((point[i].x - point[j].x)*(point[i].x - point[j].x) + (point[i].y - point[j].y) * (point[i].y - point[j].y));
}
 
double prim(){
    int i;
    int m,p,s;
    double min_s,ans;
    memset(V,0,sizeof(V));
    for(i = 0;i < N;i++)
        s_V[i] = MAX;
    m = 1; ans = 0; s = 0;
    while(m < N){
        min_s = MAX;
        for(i = 1;i < N;i++){
            if(!V[i] && s_V[i] > map[s][i])
                s_V[i] = map[s][i];
            if(!V[i] && min_s > s_V[i]){
                min_s = s_V[i];
                p = i;
            }
        }
        s = p;
        V[s] = 1;
        ans += min_s;
        m++;
    }
    return ans;
}
main()
{
    int i,j;
    while(scanf("%d",&N) != EOF){
        for(i = 0;i < N;i++){
            scanf("%f %f",&point[i].x,&point[i].y);
        }
        for(i = 0;i < N;i++)
            for(j = 0;j < N;j++)
            {
                map[i][j] = cal(i,j);   
            }
        printf("%.1lf\n",prim());
    }
    return 0;
}
/**************************************************************
    Problem: 1222
    User: ipqhjjybj
    Language: C
    Result: 正确
    Time:14 ms
    Memory:8716 kb
****************************************************************/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值