[HDU] 1875 畅通工程再续 每对点都有连线的prime ,建好图后确不是每对定点都要有边

题目链接:

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

方法:在往优先队列中加入定点的时候,判断当前队列出来的点探寻到其时到其的距离是否满足限制条件,不满足,该顶点就不进优先队列,相当于该边在图里不存在。

感想:简单题。

代码:

View Code
#include<iostream>
#include<queue>
#include<math.h>
using namespace std;
int const MAX =0x3f3f3f3f;
struct Node 
{
    int x;
    int 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];
   int tc=0,t;
   cin>>t;
    while(tc<t)
    {    
        int i =0;
        scanf("%d",&count);
        while(i<count)
        {
            scanf("%d %d",&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])
                    {                         
                        double dist = sqrt( (double)(temp.x-Nodes[j].x)*(temp.x-Nodes[j].x) + (double)(temp.y-Nodes[j].y)*(temp.y-Nodes[j].y));
                        if(dist<=1000.0 && dist>=10.0 ) //注意限制条件在这里加  相当于这两点没有边
                        {
                            Nodes[j].distance  = dist;
                            q.push(Nodes[j]);
                        }
                    }
                }
            }
     
        }
        if(q.empty() && st <count)
            printf("oh!\n");
        else
            printf("%.1lf\n", sum*100);
        tc++;
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/kbyd/p/3019305.html