[HDU] 1162 Eddy's picture-每两个点间都有边

题目链接:

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

方法:由于没两个点都有边,且边的代价要算,因此用kruskal算的话光光是算边的代价实践复杂度就是o(v^2),所以用prime的.

感想:最简单基本赤裸裸的最小生成树。

代码:

View Code
#include<iostream>
#include<queue>
#include<math.h>
using namespace std;
int const MAX =0x3f3f3f3f;
struct Node 
{
    double x;
    double y;
    double distance;
    int index;
};
struct cmp
{
    bool operator ()(Node x, Node y)
    {
        return x.distance> y.distance; // x小的优先级高
    }
};
 
int main()
{
   int count;
   Node Nodes[100];
   bool visisted[100];
    while(scanf("%d",&count)!=EOF)
    {    
        int i =0;
        while(i<count)
        {
            scanf("%lf %lf",&Nodes[i].x,&Nodes[i].y);
            Nodes[i].distance = MAX;
            Nodes[i].index=i;
            i++;
        }
        Nodes[0].distance = 0;
        priority_queue<Node, vector<Node>, cmp>q;
        q.push(Nodes[0]);
        memset(visisted,false,sizeof(visisted));
        double sum=0.0;
        int st= 0;
        while(!q.empty() && st<count)
        {
            Node temp = q.top();
            q.pop();
            if(!visisted[temp.index])
            {
                sum+=temp.distance;
                st++;
                visisted[temp.index]=true;
                for(int j =0;j<count;j++)
                {
                    if(!visisted[j])
                    {
                        Nodes[j].distance = sqrt( (temp.x-Nodes[j].x)*(temp.x-Nodes[j].x) + (temp.y-Nodes[j].y)*(temp.y-Nodes[j].y));
                        q.push(Nodes[j]);
                    }
                }
            }
     
        }
        printf("%.2lf\n", sum);


    }
    return 0;
} 

 

posted @ 2013-04-13 21:19  kbyd  阅读(175)  评论(0编辑  收藏  举报