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


相关推荐

  • bWAPP练习

    bWAPP练习简介虚拟机下载地址: https://www.vulnhub.com/entry/bwapp-bee-box-v16,53/如果你想自己去部署环境:https://sourceforge.net/projects/bwapp/files/bee-box/bWAPP包含有100多个漏洞,包括OWASPTop10安全风险,很爽的PHPweb靶机。登录username:bee pas…

    2022年9月24日
    0
  • 区块链名词解释_区块链公司起什么名字好

    区块链名词解释_区块链公司起什么名字好区块链名词解释

  • c语言和vc的区别_c++是c语言的升级版吗

    c语言和vc的区别_c++是c语言的升级版吗returnx+y}fnsub(x,yint)int{returnx-y}就像在Go和C中一样,函数不能重载。这简化了代码并提高了可维护性和可读性。函数可以在声明之前使用:add和sub在main之后声明,但仍然可以从main调用。对于V中的所有声明都是如此,并且不需要头文件或考虑文件和声明的顺序。V的目标之一是向具有不同编译器开发经验的开发人员开放。作者希望编译器不再是让人捉摸不透、只…

    2022年8月12日
    4
  • 【elasticsearch系列】windows安装IK分词器插件[通俗易懂]

    【elasticsearch系列】windows安装IK分词器插件[通俗易懂]环境github下载:https://github.com/medcl/elasticsearch-analysis-ik/releases注意,IK分词器插件要与ES版本保持一致;有的小伙伴在GitHub上下载插件时,没有发现与ES相对应的版本,可以切换到Tags中选择分支版本;例如Branchs列表中仅可能存在主版本号;切换到右侧Tags中查找对应的版本即可;小编这里选择的7.8.0的版本;安装IK解压缩后拷贝到ElasticSearch安装目录的plugins文件夹下,默认情况该

    2022年6月18日
    23
  • 编码的奥秘_生活中运用数字编码的例子有哪些

    编码的奥秘_生活中运用数字编码的例子有哪些摩尔斯电码:由萨谬尔摩尔斯发明观察可得E,T:只有一个滴或哒2^1I,A,N,M:是有两个滴答组成2^2以此类推三个滴答可以组成8个字母2^3四个滴答可以组成16个字母2^4这样就

    2022年8月4日
    2
  • EnableEventValidation 是什麽東東?

    EnableEventValidation 是什麽東東?
    回发或回调参数无效。在配置中使用或在页面中使用<%@PageEnableEventValidation="true"%>启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用ClientScriptManager.RegisterForEventValidation方法来注册回发或回调数据以进行验证。
    说明:执行

    2022年7月26日
    3

发表回复

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

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