现在的位置: 首页 > 综合 > 正文

NEU 1222: Sweep the snow PRIM()

2013年10月10日 ⁄ 综合 ⁄ 共 1416字 ⁄ 字号 评论关闭

题目描述:

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
****************************************************************/

抱歉!评论已关闭.