hdu3832 Earth Hour

Earth Hour


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
 
题意是给n个圆,编号1-n,每个圆有其圆心的坐标和它的半径,问最多去掉多少个圆之后,编号为1,2,3的圆是直接或者间接相连着的。(两个圆连着当且仅当他们相交。)

这道题实际上是要求使3个点联通的最小代价,一开始一看有点像MST,但是又只要满足3个点即可。所以这个最小代价应该是某个点出发到这3个点的最短路。由于点n是200,我一开始想做n遍dijkstra,但是TLE了。后来分析一下实际上只要做3次最短路即可。从3个目标点出发做3次最短路,然后min(dis1[i] +dis2[i] +dis3[i])(1<= i <=n)就是答案了。
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<set>
#include<ctype.h>
#include<fstream>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const LONG     INF=0x3f3f3f3f;
const LONG MOD=10000;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define writeln(x) cout<<x<<endl
#define readln(x) cin>>x
#define rep(a,b,c)  for(LONG a=b;a<=c;a++ )
#define rrep(a,b,c)  for(LONG a=b;a>=c;a--)
#define EPS 1e-10
struct Point {
    int x , y , r;
}point[300];
bool judge ( int a , int b)
{
    int z = (point[a].x - point[b].x)*(point[a].x - point[b].x) + (point[a].y - point[b].y)*(point[a].y - point[b].y) ;
    if( z <= (point[a].r+point[b].r)*(point[a].r+point[b].r) )
        return true ;
    else return false;
}
int matr[220][220] ;
int dis[300] ;
bool visit[300];
int sum [300];
void Init()
{
    clr1( dis );
    clr0( visit);
    clr1( matr ) ;
}
int solve(int s, int n)
{
    Init();
    visit[s] = 1;
    dis[s] = 0;
        for(int i =1 ;i <= n ;++ i)
        {
            for(int j = i +1 ;j <= n ;++ j)
                if(judge ( i , j))
                {
                    matr[i][j] = 1;
                    matr[j][i] = 1;
                }
            matr[i][i] = 0;
        }
    for(int i = 1; i<= n ;++ i)dis[i] = min(dis[i] , matr[s][i]) ;
    while(1)
    {
        int k ;
        int minx = INF ;
        for(int i =1 ; i<= n ;++ i)
        {
            if(i == s)continue ;
            if( dis[ i] < minx && (!visit[ i]))
            {
                minx = dis[i];
                k = i;
            }
        }
        if( minx == INF )break ;
        visit[k] = true ;

        for(int i = 1; i <=n ; ++ i)
            if(!visit [ i] )
            dis[i] = min( dis[i] , dis[k] + matr[k][ i]) ;
    }
    if(dis[1] >=100000000 && dis[2] >= 100000000 && dis[3] >= 100000000) return INF ;
    return dis[1]+ dis[2] + dis[3]  + 1;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n ;
        cin>>n;
        for(int i = 1; i <= n ;++i )
            scanf("%d%d%d", &point[i].x, &point[i].y , &point[i].r);
        int ans = INF ;
        for(int i = 1; i<= n ;++ i)sum[i] = 1;
        for(int i =1; i <= 3; ++ i)
        {
            solve( i , n);
            for(int j = 1 ; j <= n ;++ j)
            {
                if(dis[j] > 10000000)
                sum[j] += 10000000;
                else
                    sum[j] += dis[j];
            }
        }
        for(int i = 1; i<= n ;++ i)
            ans = min( ans , sum[i]);

        if(ans > 10000000)
            printf("-1\n");
        else printf("%d\n", n - ans );
//        printf("%d \n" ,ans );
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值