hdu1162

#include<cstdio>
#include<cmath>
#include<climits>
#include<algorithm>
#define INF 1000000000
using namespace std;
struct p
{
    double x,y;
}spot[110];
double cost[110][110];
double mincost[110];
bool used[110];
int n;

double prim()
{
    for(int i=0;i<n;i++)
    {
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[0]=0;
    double res=0;

    while(true){
        int v=-1;
        for(int i=0;i<n;i++)
            if(!used[i]&&(v==-1||mincost[i]<mincost[v])) v=i;
        if(v==-1) break;
        used[v]=true;
        res+=mincost[v];
        for(int i=0;i<n;i++)
            mincost[i] = min(mincost[i], cost[v][i]);

    }
    return res;
}
double dis(p a,p b)
{
    double fa = (a.x-b.x);
    double fb = (a.y-b.y);
    return sqrt(fa*fa+fb*fb);
}
int main()
{
//    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&spot[i].x,&spot[i].y);
        for(int i=0;i<n;i++)
        for(int j=i;j<n;j++){
            if(i==j) cost[i][j]=INF;
            else { cost[i][j]=cost[j][i]=dis(spot[i],spot[j]); }
        }
//        for(int i=0;i<n;i++){
//        for(int j=0;j<n;j++){
//            printf("%lf ",cost[i][j]);
//        }
//        printf("\n");
//        }

        printf("%.2lf\n",prim());
    }
//    printf("%lf",dis(spot[0],spot[1]));
    return 0;
}
时间: 2024-10-11 13:06:08

hdu1162的相关文章

【HDU1162】Eddy&#39;s picture(MST基础题)

很基础的点坐标MST,一不留神就AC了, - - !! 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <numeric> 9 #include &

hdu1162 Eddy&#39;s picture 基础最小生成树

1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <algorithm> 5 #include <string> 6 using namespace std; 7 8 int n, m; 9 int f[510]; 10 11 struct node 12 { 13 int u, v; 14 double w; 15 }map[10010]; 16 17

HDU1162 Eddy&#39;s picture 【最小生成树Prim】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6723    Accepted Submission(s): 3391 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

HDU1162 Eddy&#39;s picture【Prim】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7231    Accepted Submission(s): 3656 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

hdu-1162 Eddy&#39;s picture---浮点数的MST

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 题目大意: 给n个点,求MST权值 解题思路: 直接prim算法 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 100 + 10; 5 const int INF = 1e9 + 7; 6 double Map[maxn][maxn]; 7 double

hdu1162(最小生成树 prim or kruscal)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 意义:给出一些点,用线问使所有点直接或间接连通,需要多长: 思路:裸最小生成树: 法1: prim算法:MST(Minimum Spanning Tree,最小生成树)问题有两种通用的解法,Prim算法就是其中之一,它是从点的方面考虑构建一颗MST,大致思想是:设图G顶点集合为U,首先任意选择图G中的一点作为起始点a,将该点加入集合V,再从集合U-V中找到另一点b使得点b到V中任意一点的权值最