hdu 3832 Earth Hour bfs

Earth Hour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)


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
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-6
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e3+10,M=1e6+10,inf=1e9+10;
const LL INF=5e17+10,mod=1e9+7;

vector<int>edge[N];
int x[N],y[N],r[N];
int vis[N],dis[N];
int check(int i,int j)
{
    if((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])<=(r[i]+r[j])*(r[i]+r[j]))
        return 1;
    return 0;
}
int bfs(int s,int t)
{
    queue<int>q;
    q.push(s);
    memset(vis,0,sizeof(dis));
    dis[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0;i<edge[u].size();i++)
        {
            int v=edge[u][i];
            if(vis[v])continue;
            dis[v]=dis[u]+1;
            vis[v]=1;
            q.push(v);
        }
    }
    if(vis[t])return dis[t];
    return 10000;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d%d%d",&x[i],&y[i],&r[i]),edge[i].clear();
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(check(i,j))
                {
                    edge[i].push_back(j);
                    edge[j].push_back(i);
                }
            }
        }
        int ans=inf;
        for(int i=1;i<=n;i++)
        {
            int d1=bfs(i,1);
            int d2=bfs(i,2);
            int d3=bfs(i,3);
            ans=min(ans,d1+d2+d3+1);
        }
        if(ans>n)printf("-1
");
        else printf("%d
",n-ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/6937828.html