hdoj1162 Eddy's picture

不知道杭电上什么时候能出题的时候把英语水平提高一下~

prim,直接贴代码:

/*
Hdoj: 1162 Eddy's picture
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

#define MaxPoint 105
#define MaxDis 10000000.0

using namespace std;

struct Point {
    double x, y;
}point[MaxPoint];

double map[MaxPoint][MaxPoint];
double dis[MaxPoint];
bool visited[MaxPoint];
int n;

double CalDis(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()
{
    visited[0] = true;
    dis[0] = .0;
    for(int i = 1; i < n; i++) {
        visited[i] = false;
        dis[i] = map[0][i];
    }
    
    double min_dis = .0;
    for(int i = 1; i < n; i++) {
        double tmp = MaxDis;
        int tmp_index = 0;
        for(int j = 0; j < n; j++) {
            if(!visited[j] && dis[j] < tmp) {
                tmp = dis[j];
                tmp_index = j;
            }
        }
        min_dis += tmp;
        visited[tmp_index] = true;
        for(int k = 0; k < n; k++) {
            if(!visited[k] && map[k][tmp_index] < dis[k]) {
                dis[k] = map[k][tmp_index];
            }
        }
    }

    return min_dis;
}

int main()
{
    //freopen("data.in", "rb", stdin);
    while(scanf("%d", &n) != EOF) {
        for(int i = 0; i < n; i++) {
            scanf("%lf%lf", &point[i].x, &point[i].y);
        }
        
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                map[i][j] = map[j][i] = CalDis(i, j);
            }
        }
        //cout << n << endl;
        printf("%.2f\n", Prim());
    }
    
    return 0;
}


你可能感兴趣的:(hdoj1162 Eddy's picture)