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


相关推荐

  • 计算机网络谢希仁第八版 课后答案第七版课后答案

    计算机网络谢希仁第八版 课后答案第七版课后答案谢希仁计算机网络第七版课后答案第一章概述1-01计算机网络向用户可以提供那些服务?答:连通性和共享1-02简述分组交换的要点。答:(1)报文分组,加首部(2)经路由器储存转发(3)在目的地合并1-03试从多个方面比较电路交换、报文交换和分组交换的主要优缺点。答:(1)电路交换:端对端通信质量因约定了通信资源获得可靠保障,对连续传送大量数据效率高。(2)报文交换:无须预约传输带…

    2022年6月17日
  • 大数据:数据采集平台之Apache Flume

    大数据:数据采集平台之Apache Flume大数据:数据采集平台之ApacheFlume官网:https://flume.apache.org/Flume是Apache旗下的一款开源、高可靠、高扩展、容易管理、支持客户扩展的数据采集系统。Flume使用JRuby来构建,所以依赖Java运行环境。Flume最初是由Cloudera的工程师设计用于合并日志数据的系统,后来逐渐发展用于处理流数据事件。Flume设计成一个分布式…

    2022年6月10日
  • 二维数组初始化规则

    二维数组初始化规则二维数组初始化的形式为:  数据类型数组名[整常量表达式][整常量表达式]={初始化数据};  在{}中给出各数组元素的初值,各初值之间用逗号分开。把{}中的初值依次赋给各数组元素。  有如下几种初始化方式:  ⑴分行进行初始化  inta[2][3]={{1,2,3},{4,5,6}};  在{}内部再用{}把各行分开,第一对{}中的初值1,2,3是0行的3个元素的初值。第…

    2022年7月18日
  • AppCompatActivity.setContentView如何装载视图到AppCompatActivity上

    AppCompatActivity.setContentView如何装载视图到AppCompatActivity上a.Activity中在onCreate中调用setContentView(R.layout.main)是做什么的;b.PhoneWindow;c.DecorView;1.setContentView()调用流程ActivityextendsAppCompatActivity@OverrideprotectedvoidonCreate(BundlesavedInst…

    2022年6月26日
  • Java中XML运用总结

    Java中XML运用总结XML 简介:可扩展标记语言(eXtensibleMarkupLanguage)。特性:1.xml具有平台无关性,是一门独立的标记语言.2.xml具有自我描述性为什么学习XML?1.网络数据传输.2.数据存储3.配置文件XML文件.XML文件是保存XML数据的一种方式XML数据也可以以其他的方式存在(如在内存中构建XML数据)。不要将XML语言狭隘的理解成XML文件。XML语法格式1.XML文档声明<?xmlversio..

    2022年7月7日
  • MATLAB函数fir1「建议收藏」

    MATLAB函数fir1「建议收藏」fir1基于窗函数的fir滤波器设计;主要形式:b=fir1(n,Wn)b=fir1(n,Wn,ftype)b=fir1(___,window)b=fir1(___,scaleopt)系数的含义n—滤波器阶数。Wn—截止频率,0≤Wn≤1,Wn=1对应于采样频率的一半。当设计带通和带阻滤波器时,Wn=[W1W2],W1≤…

    2022年7月17日

发表回复

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

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