hdu3832-Earth Hour-最短路dij

Earth Hour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 1508    Accepted Submission(s): 602


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
 

Source
 

Recommend
xubiao   |   We have carefully selected several similar problems for you:  3831 3834 3830 3835 3829


题目大意:给你n个点的坐标和半径表示路灯可以照亮的范围,只要两个圆相交或相切,就相当于是路灯联通的,
问你最多关掉几盏还是能让1,2,3个路灯是联通的。

思路:用dij求出1,2,3这三个点到其他所有点的最短路,然后如果最后三个路灯是联通的那么在这联通的所有点中肯定
存在一个点使1,2,3到这个点的最短路没有重复的路径,所以这时候能关掉的灯的个数就为
n-1-min(d[0][i]+d[1][i]+d[2][i])否则就是不存在输出-1

代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;

int maxn = 22222222;         //maxn不能开太大不然d[0][i] + d[1][i] + d[2][i]会超int
int n,m,w[300][300],d[3][300],vis[300];

struct P
{
    double x,y,r;
}p[300];
void dij(int begin ,int now)
{
    int i, j;
    memset(vis,0,sizeof(vis));
    for(i = 0; i < n;i++)
    {
        d[now][i] = (i == begin?0:maxn);
    }
    for(i = 0;i < n; i++)
    {
        int x, y =maxn;
        for(j = 0; j< n; j++)
        {
            if(vis[j] == 0&&d[now][j]<y)
            {
                x = j;
                y = d[now][j];
            }
        }
        vis[x] = 1;
        for(j = 0; j<n;j++)
        {
            d[now][j] = min(d[now][j],d[now][x]+w[x][j]);
        }
    }
}

int main()
{
    int t;
    cin>>t;
    while (t--)
    {
        cin>>n;
        int i;
        for(i = 0; i<n;i++)
        {
            scanf("%lf %lf %lf",&p[i].x,&p[i].y,&p[i].r);
        }
        int j;
        for(i = 0;i<n;i++)
        {
            for(j = 0;j<n;j++)
            {

                w[i][j] =maxn;
            }

        }
        for(i = 0;i<n;i++)//判断路灯是否联通
        {
            for(j = 0;j<n;j++)
            {

                double x = (p[i].x-p[j].x)*(p[i].x-p[j].x);
                double y = (p[i].y-p[j].y)*(p[i].y-p[j].y);
                double l = sqrt(x+y);
                if(l<p[i].r+p[j].r+0.0000001)
                {
                    w[i][j] = 1;
                    w[j][i] =1;
                }
            }
        }
        for(i =0;i<3;i++)
        {
            dij(i,i);
        }
        int minx = 9999;
        for(i =0; i< n; i++)
        {
            int ans = d[0][i] + d[1][i]+d[2][i];
            if(ans<minx)
                minx = ans;
        }
        if( minx == 9999)
        {
            cout<<"-1"<<endl;
        }
        else
            cout<<n-1-minx<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值