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年1月22日 上午11:00
下一篇 2022年1月22日 下午12:00


相关推荐

  • java多线程-学习总结(完整版)

    java多线程-学习总结(完整版)这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML图表FLowchart流程图导出与导入导出导入欢迎使用Markdown编辑器你好!这是你第一次使用Markdown编辑器所展示的欢迎页。如果你想学习如何使用Mar

    2022年7月7日
    20
  • nginx 优雅重启_查看nginx是否启动

    nginx 优雅重启_查看nginx是否启动#1.检测修改过后的配置文件是否有错误 /usr/local/nginx/sbin/nginx-t #如果没问题会返回: #nginx:theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisok #nginx:configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful#2.平滑重启 /usr/local/nginx/s

    2022年8月13日
    10
  • Navicat Premium相关注册码「建议收藏」

    Navicat Premium相关注册码「建议收藏」–NavicatforSQLServerV10.0.10NAVD-3CG2-6KRN-IEPMNAVL-NIGY-6MYY-XWQENAVI-C3UU-AAGI-57FW–NavicatPremium注册码NAVJ-E6YF-JULL-KKIGNAVE-BOCL-CE3X-TAGYNAVC-KAIA-NU5I-SPOXNAVL-FE27-KNTQ-YJXCNAVK-LXKO-3XHL…

    2022年10月13日
    4
  • 富士通MB95F636H输出PWM

    富士通MB95F636H输出PWMPPS 寄存器用来配置 PWM 周期 PDS 寄存器用来配置 PWM 的占空比 即占空比 PDS PPS PPGS 和 REVC 的每一位都代表一个输出 即 bit0 表示 PPG00 bit1 表示 PPG01 以此类推 当 PPGS 对应的位为 1 时启动该输出的计数 即开启 PWM 模式 REVC 表示输出是否反相 对应位为 1 则该输出反相 否则不反相 PC 寄存器是最重要的寄存器 PCn1 和 PCn0 的配置略有不同 毕竟 PCn1 是能成为 16 位 PPG 高位

    2026年3月26日
    2
  • dedecms后台怎么添加发布软件?织梦后台软件内容管理

    dedecms后台怎么添加发布软件?织梦后台软件内容管理

    2021年9月25日
    43
  • Cholesky分解法

    Cholesky分解法Cholesky 分解法又叫平方根法 是求解对称正定线性方程组最常用的方法之一 对于一般矩阵 为了消除 LU 分解的局限性和误差的过分积累 采用了选主元的方法 但对于对称正定矩阵而言 选主元是不必要的 nbsp 定理 若对称正定 则存在一个对角元为正数的下三角矩阵 使得成立 nbsp 假设现在要求解线性方程组 其中为对称正定矩阵 那么可通过下面步骤求解 nbsp 1 求的 Cholesky 分解 得到

    2026年3月19日
    2

发表回复

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

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