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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 你走后,我才真的爱上你

    你走后,我才真的爱上你你走后,我才真的爱上你

    2022年4月23日
    46
  • 为什么引入ReLU激活函数

    为什么引入ReLU激活函数https://blog.csdn.net/fredinators/article/details/79443386ReLu是神经网络中的一个激活函数,其优于tanh和sigmoid函数。1.为何引入非线性的激活函数?如果不用激活函数,在这种情况下每一层输出都是上层输入的线性函数。容易验证,无论神经网络有多少层,输出都是输入的线性组合,与没有隐藏层效果相当,这种情况就是最原始的感知机(…

    2022年6月20日
    32
  • 海量数据处理思路「建议收藏」

    海量数据处理思路「建议收藏」海量数据处理思路海量数据处理海量数据,不能一次加载到内存中海量数据topK(最大和最小k个数),第k大,第k小的数海量数据判断一个整数是否存在其中海量数据找出不重复的数字找出A,B两个海量url文件中共同的url海量数据topK最大K使用最小堆,最小K使用最大堆,这里以最大K为例海量数据hash分块维护最小堆的K个数据的数据容器堆中数据是topK大的数据,堆顶的数据是第K大数据先将海量数据hash再取模m,分成m个小文件,hash(num)%m,也可以直接取模在

    2022年6月23日
    20
  • mysql 在命令行和navicat 查出来的数据不一致,你遇到过吗?[通俗易懂]

    mysql 在命令行和navicat 查出来的数据不一致,你遇到过吗?

    2022年2月10日
    50
  • Pyinstaller打包exe太大,运行太慢

    Pyinstaller打包exe太大,运行太慢1.背景通过python使用pyqt编写了一个界面程序,并使用pyinstaller将其打包成exe文件2.问题打包的exe文件非常大,有280M,而且更无法忍受的是打开非常非常的慢!!!3.解决办法(1)将import改为from…import…尝试之后,并么有什么用,依然是非常非常的大,非常非常的慢。(2)anaconda的问题看网上有人说是anacon…

    2022年6月20日
    100
  • vue生命周期钩子函数(详解及使用场景)(什么是vue的生命周期)

    vue中生命周期钩子函数有哪些发布时间:2020-12-0713:07:03来源:亿速云阅读:94作者:小新这篇文章主要介绍vue中生命周期钩子函数有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Vue实例的生命周期钩子函数(8个)1、beforeCreate刚new了一个组件,无法访问到数据和真实的dom,基本上这个好像不能干啥2、createddata属性完成了…

    2022年4月12日
    154

发表回复

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

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