最短路,dijkstra算法。

本文介绍了一个基于 Earth Hour 活动背景的算法问题,探讨如何在保证图书馆、自习室和宿舍区域照明相连的前提下,尽可能多地关闭校园内的街灯。通过分析点之间的连接性和最短路径算法实现解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题目大意;题目大意,有n个灯,编号为1到n,给出每个灯的坐标和灯光照的范围,要求,1号2号3号必须开着,求让它们3个连在一起的情况下可以关闭的最多的灯是多少?

题意也转化一下为要用灯将1和2和3连接在一起,并且要最少的灯将他们连接在一起。

这道题就是找出一个点作为桥梁,让目标三个点到该点的最短路之和最小,就是需要最小的值把1 2 3 连接在一起,包括(1,2,3)3个点,用n减去得到结果。

一开始看题看醉了,根本不知道该干嘛。后来搜了下题解才知道。。。。。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const int N = 210;
int n,map[N][N],vis[N];
struct node
{
    double x,y;
    double r;
} a[N];
int G(int x,int y)
{
    double len=sqrt((a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y));
    if(len<=a[x].r+a[y].r)
        return 1;
    else
        return 0;
}
void DJ(int x,int d[])
{
    int i,j;
    memset(vis,0,sizeof(vis));
    for(i=0; i<N; i++)
        d[i]=INF;
    d[x]=0;
    vis[x]=1;
    for(i=0; i<n; i++)
    {
        int temp=INF;
        for(j=0; j<n; j++)
            if(!vis[j]&&d[j]<temp)
                temp=d[x=j];
        vis[x]=1;
        for(j=0; j<n; j++)
            if(!vis[j]&&map[x][j]&&d[x]+map[x][j]<d[j])
                d[j]=d[x]+map[x][j];

    }
}
int main()
{
    int T,d1[N],d2[N],d3[N];
    scanf("%d",&T);
    while(T--)
    {
        memset(map,0,sizeof(map));
        memset(d1,0,sizeof(d1));
        memset(d2,0,sizeof(d2));
        memset(d3,0,sizeof(d3));
        int i,j;
        scanf("%d",&n);
        for(i=0; i<n; i++)
            scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r);
        for(i=0; i<n; i++)
            for(j=i+1; j<n; j++)
            {
                if(G(i,j))
                    map[i][j]=map[j][i]=1;
            }
        DJ(0,d1);
        DJ(1,d2);
        DJ(2,d3);
        int mm=INF,sum=0;
        for(i=0; i<n; i++)
        {
            if(d1[i]!=INF&&d2[i]!=INF&&d3[i]!=INF)//这个和3个点都能连通
            {
                sum=d1[i]+d2[i]+d3[i];//此点到3个点的最短路之和
                if(sum<mm)
                    mm=sum;
            }
        }
        if(mm==INF)
            printf("-1\n");
        else
            printf("%d\n",n-mm-1);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值