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题目大意; 题目大意,有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); } }