hdu 3832 Earth Hour (最短路变形)

hdu 3832 Earth Hour (最短路变形)

大家好,又见面了,我是全栈君。

Earth Hour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 1516    Accepted Submission(s): 606




Problem Description
Earth Hour is an annual international event created by the WWF (World Wide Fund for Nature/World Wildlife Fund), held on the last Saturday of March, that asks households and businesses to turn off their non-essential lights and electrical appliances for one hour to raise awareness towards the need to take action on climate change. 

To respond to the event of this year, the manager of Hunan University campus decides to turn off some street lights at night. Each street light can be viewed as a point in a plane, which casts flash in a circular area with certain radius.

What’s more, if two illuminated circles share one intersection or a point, they can be regarded as connected.

Now the manager wants to turn off as many lights as possible, guaranteeing that the illuminated area of the library, the study room and the dormitory are still connected(directly or indirectly). So, at least the lights in these three places will not be turned off.

 


Input
The first line contains a single integer T, which tells you there are T cases followed.

In each case:

The first line is an integer N( 3<=N<=200 ), means there are N street lights at total.

Then there are N lines: each line contain 3 integers, X,Y,R,( 1<=X,Y,R<=1000 ), means the light in position(X,Y) can illuminate a circle area with the radius of R. Note that the 1st of the N lines is corresponding to the library, the 2nd line is corresponding to the study room, and the 3rd line is corresponding to the dorm.

 


Output
One case per line, output the maximal number of lights that can be turned off.

Note that if none of the lights is turned off and the three places are still not connected. Just output -1.

 


Sample Input
   
   
3 5 1 1 1 1 4 1 4 1 1 2 2 1 3 3 1 7 1 1 1 4 1 1 2 4 1 1 3 1 3 1 1 3 3 1 4 3 1 6 1 1 1 5 1 1 5 5 1 3 1 2 5 3 2 3 3 1

 


Sample Output
   
   
-1 2 1

每一个灯看做一个点,能互相照亮的点连边。从0、1、2三个源点求最短路。然后枚举这3个点到每一个点的最短距离,得到答案。

#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
#include"algorithm"
using namespace std;
#define N 205
const int inf=1000000;
int min(int a,int b)
{
    return a<b?a:b;
}
struct node
{
    int x,y,r;
}p[N];
int g[N][N];
int d[3][N];
int judge(int i,int j)   //推断两个灯是否相交
{
    int x,y,d,r;
    x=p[i].x-p[j].x;
    y=p[i].y-p[j].y;
    d=x*x+y*y;
    r=p[i].r+p[j].r;
    if(r*r>=d)
        return 1;
    return 0;
}
void spfa(int s,int n,int *dis)
{
    int i,mark[N];
    memset(mark,0,sizeof(mark));
    for(i=0;i<n;i++)
        dis[i]=inf;
    dis[s]=0;
    queue<int>q;
    q.push(s);
    mark[s]=1;
    while(!q.empty())
    {
        s=q.front();
        q.pop();
        mark[s]=0;
        for(i=0;i<n;i++)
        {
            if(dis[i]>dis[s]+g[s][i])
            {
                dis[i]=dis[s]+g[s][i];
                if(!mark[i])
                {
                    mark[i]=1;
                    q.push(i);
                }
            }
        }
    }
}
int main()
{
    int T,i,j,x,y,r,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&r);
            p[i].x=x;p[i].y=y;p[i].r=r;
        }
        for(i=0;i<n;i++)
        {
            g[i][i]=0;
            for(j=0;j<i;j++)
            {
                if(judge(i,j))
                    g[i][j]=g[j][i]=1;
                else
                    g[i][j]=g[j][i]=inf;
            }
        }
        spfa(0,n,d[0]);
        spfa(1,n,d[1]);
        spfa(2,n,d[2]);
        int ans=inf;
        for(i=0;i<n;i++)
        {
            ans=min(ans,d[0][i]+d[1][i]+d[2][i]);
        }
        if(ans<inf)
            printf("%d\n",n-ans-1);
        else
            printf("-1\n");
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116368.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 序列化和反序列化的底层实现原理是什么?

    序列化和反序列化的底层实现原理是什么?序列化和反序列化作为Java里一个较为基础的知识点,大家心里也有那么几句要说的,但我相信很多小伙伴掌握的也就是那么几句而已,如果再深究问一下Java如何实现序列化和反序列化的,就可能不知所措了!遥记当年也被问了这一个问题,自信满满的说了一大堆,什么是序列化、什么是反序列化、什么场景的时候才会用到等,然后面试官说:那你能说一下序列化和反序列化底层是如何实现的吗?一脸懵逼,然后回家等通知!一、…

    2022年6月15日
    26
  • Spring通过SchedulerFactoryBean实现调度任务的配置(定时器)「建议收藏」

    Spring通过SchedulerFactoryBean实现调度任务的配置(定时器)「建议收藏」execSyncNextWeekPlan–>…

    2022年6月30日
    27
  • 测试用例设计常用方法有哪些_软件测试用例包括什么

    测试用例设计常用方法有哪些_软件测试用例包括什么目录一、测试用例二、黑盒测试2.1、等价类划分法2.1.1、定义2.1.2、等价类划分分类2.1.3、等价类划分原则2.2.4、等价类方法设计测试用例步骤2.2、边界值方法2.2.1、边界值的概念2.2.2、边界值选择遵循的原则2.2.3、边界值方法设计测试用例2.3、判定表方法2.3.1、判定表结构2.3.2、判定表设计测试用例2.4、因果图方法2.4.1、因果图法设计测试用例一、测试用例测试用例: 将要进行的测试工

    2022年10月9日
    3
  • 奉劝那些想把编程学好的学弟学妹们!呕心沥血,袒露心声,掏心掏肺

    奉劝那些想把编程学好的学弟学妹们!呕心沥血,袒露心声,掏心掏肺CSDN的小伙伴们,大家好,我是沉默王二。作为CSDN的前排博主(18万+关注,有点飘了哈),我接触了太多太多想学编程、想把编程学好的人,有从别的专业转过来的,有零基础自学的,有科班出身的。他们当中的一部分人,学着学着就放弃了,或者还在放弃的路上。所以真的想掏心掏肺给大家谈一谈,在学好编程这条路上,我们该做好哪些心理准备,该怎么去学。01、很遗憾我上大学那会,学校的计算机专业刚成立两年,也就是说,我们是第二批。据说,第一批做小白鼠的学长学姐们,很多在毕业的时候都没从事计算机专业方面的工作。倒

    2022年6月5日
    33
  • 如何设计单元测试用例「建议收藏」

    如何设计单元测试用例「建议收藏」如何编写单元测试用例(白盒测试)。一、单元测试的概念单元通俗的说就是指一个实现简单功能的函数。单元测试就是只用一组特定的输入(测试用例)测试函数是否功能正常,并且返回了正确的输出。测试的覆盖种类1.语句覆盖:语句覆盖就是设计若干个测试用例,运行被测试程序,使得每一条可执行语句至少执行一次。2.判定覆盖(也叫分支覆盖…

    2022年6月18日
    32
  • ps磨皮滤镜portraiture安装教程mac[通俗易懂]

    ps磨皮滤镜portraiture安装教程mac[通俗易懂]PortraitureforMac激活成功教程版是Photoshop上一款支持自动皮肤平滑、愈合和增强效果的磨皮插件,这款portraiture磨皮滤镜主要针对人像进行皮肤修饰、磨皮润色等处理,portraituremac激活成功教程版还可以平滑和去除缺陷,同时保留皮肤纹理和重要的人像细节,功能十分强大,这里为大家带来portraiture激活成功教程版,并附上激活成功教程补丁。portraiture激活成功教程方…

    2022年7月22日
    45

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号