hdu 3832 Earth Hour(最短路变形)

Earth Hour

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


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,2,3,分别表示图书馆,宿舍,自习室,尽可能关掉灯使得3个区域连在一起还是亮着(每个点给定圆点和半径,两圆相切或相交则为两区域连在一起)。

要使三个区域连在一起并且亮着,那么三个区域之间必须要有灯,或者三个区域本来就连着。尽可能的关掉灯,就可以在所有的灯中找到一个灯,它到三个灯的路径最短(连着的灯最少)。

附上代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <cmath>
 6 #define INF 0x3f3f3f
 7 #define N 205
 8 #define M 1005
 9 using namespace std;
10 int n,m;
11 struct point
12 {
13     int x,y,r;
14 } p[N];
15 int map[M][M],dis[4][M];
16 bool vis[M];
17 
18 void spfa(int s)
19 {
20     for(int i=1;i<=n;i++)
21     {
22         dis[s][i]=INF;
23         vis[i]=false;
24     }
25     queue<int>q;
26     q.push(s);
27     dis[s][s]=0;
28     vis[s]=true;
29     while(!q.empty())
30     {
31         int u=q.front();
32         q.pop();
33         vis[u]=false;
34         for(int i=1; i<=n; i++)
35         {
36             if(dis[s][i]>dis[s][u]+map[u][i])
37             {
38                 dis[s][i]=dis[s][u]+map[u][i];
39                 if(!vis[i])
40                 {
41                     vis[i]=true;
42                     q.push(i);
43                 }
44             }
45         }
46     }
47 }
48 
49 double dist(point a,point b)
50 {
51     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
52 }
53 
54 int main()
55 {
56     int T,i,j;
57     scanf("%d",&T);
58     while(T--)
59     {
60         scanf("%d",&n);
61         for(i=1; i<=n; i++)
62             scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].r);
63         for(i=1; i<=n; i++)
64             for(j=1; j<=n; j++)
65             {
66                 double d=dist(p[i],p[j]);
67                 if((d-p[i].r-p[j].r)>0)
68                     map[i][j]=INF;
69                 else          ///相连记录为1
70                     map[i][j]=1;
71                 map[i][i]=0;
72             }
73         spfa(1);
74         spfa(2);
75         spfa(3);
76         if(dis[1][2]==INF||dis[2][3]==INF||dis[1][3]==INF)
77         {
78             printf("-1
");
79             continue;
80         }
81         int xmin=INF;
82         for(i=1; i<=n; i++)
83         {
84             if(dis[1][i]+dis[2][i]+dis[3][i]+1<xmin)
85                 xmin=dis[1][i]+dis[2][i]+dis[3][i]+1;
86         }
87         printf("%d
",n-xmin);
88     }
89     return 0;
90 }
原文地址:https://www.cnblogs.com/pshw/p/5764942.html