地址:http://acm.hdu.edu.cn/showproblem.php?pid=3832
Earth Hour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 1264 Accepted Submission(s): 507
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.
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.
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.
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
思路:处理灯与灯之间相连问题比较简单,就不说了。将相连的灯之间距离设为1,不相连的灯之间距离设为M。然后对1、2、3三个点进行最短路处理,就可以得出每个点到1、2、3点的最短距离,让这3个距离相加,找出最小的距离。答案就是n-1-最小距离。(总有一盏灯到1、2、3的三条最短路不会经过同一盏灯)
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
//#include<algorithm>
using namespace std;
#define M 200
struct node
{
int x,y,r;
} po[210];
int map1[210][210],n,la[210];
void DJS(int a) //DJ斯特拉算法,求a点到每个点的最短路
{
int dist[210];
int s[210];
int i,j,minn,k;
for(i=1; i<=n; i++)
{
dist[i]=map1[a][i];
s[i]=0;
}
s[a]=1;
for(i=1; i<n; i++)
{
minn=M;
k=1;
for(j=1; j<=n; j++)
{
if(s[j]==0&&minn>=dist[j])
{
minn=dist[j];
k=j;
}
}
s[k]=1;
for(j=1; j<=n; j++)
{
if(s[j]==0)
if(map1[k][j]<M&&dist[k]+map1[k][j]<dist[j])
dist[j]=dist[k]+map1[k][j];
}
}
for(i=1;i<=n;i++)
la[i]+=dist[i]; //在这里将路径相加
}
int main()
{
int t,i,j,k,minn;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
minn=M;
for(i=1; i<=n; i++)
scanf("%d%d%d",&po[i].x,&po[i].y,&po[i].r);
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
double ll=sqrt((po[i].x-po[j].x)*1.0*(po[i].x-po[j].x)+(po[i].y-po[j].y)*1.0*(po[i].y-po[j].y));
if(ll<po[i].r+po[j].r||fabs(ll-po[i].r-po[j].r)<10e-6)
map1[i][j]=map1[j][i]=1;
else map1[i][j]=map1[j][i]=M;
if(i==j) map1[i][j]=0;
}
}
memset(la,0,sizeof(la));
DJS(1); //三次DJ斯特拉
DJS(2);
DJS(3);
for(i=1; i<=n; i++)
if(la[i]<minn) minn=la[i]; //求最小值
if(minn>=M) printf("-1\n");
else printf("%d\n",n-minn-1);
}
return 0;
}