zoukankan      html  css  js  c++  java
  • [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;
    } 
  • 相关阅读:
    产品易用性
    优化Compress components with gzip 问题
    转:稳定性测试
    Xray CA证书
    转:获取WEB各阶段响应时间
    测试用例编写注意事项
    用dd把一个空硬盘写满
    转:linux终端命令使用cpu负载到100
    JMeter命令行执行+生成HTML报告
    防F12扒代码:按下F12关闭当前页面
  • 原文地址:https://www.cnblogs.com/kbyd/p/3019305.html
Copyright © 2011-2022 走看看