hdu 3832 Earth Hour
http://acm.hdu.edu.cn/showproblem.php?pid=3832
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <cstring> 5 #include <vector> 6 #include <algorithm> 7 #include <queue> 8 #define maxn 1001 9 using namespace std; 10 const int inf=1<<28; 11 12 int g[maxn][maxn]; 13 bool vis[maxn]; 14 int dis1[maxn],dis2[maxn],dis3[maxn]; 15 int n; 16 struct node 17 { 18 int x,y,r; 19 }p[maxn]; 20 int sqr(int x) 21 { 22 return x*x; 23 } 24 25 void spfa(int src,int dis[]) 26 { 27 queue<int>q; 28 memset(vis,false,sizeof(vis)); 29 for(int i=0; i<=n; i++) dis[i]=inf; 30 dis[src]=0; 31 vis[src]=true; 32 q.push(src); 33 while(!q.empty()) 34 { 35 int u=q.front(); q.pop(); 36 vis[u]=false; 37 for(int i=0; i<n; i++) 38 { 39 if(dis[i]>dis[u]+g[u][i]&&g[u][i]!=inf) 40 { 41 dis[i]=dis[u]+g[u][i]; 42 if(!vis[i]) 43 { 44 q.push(i); 45 vis[i]=true; 46 } 47 } 48 } 49 } 50 } 51 52 int main() 53 { 54 int t; 55 scanf("%d",&t); 56 while(t--) 57 { 58 scanf("%d",&n); 59 for(int i=0; i<=n; i++) 60 { 61 for(int j=0; j<=n; j++) 62 { 63 if(i==j) g[i][j]=0; 64 else g[i][j]=inf; 65 } 66 } 67 for(int i=0; i<n; i++) 68 { 69 cin>>p[i].x>>p[i].y>>p[i].r; 70 } 71 for(int i=0; i<n; i++) 72 { 73 for(int j=i+1; j<n; j++) 74 { 75 if(sqrt(sqr(p[i].x-p[j].x)+sqr(p[i].y-p[j].y))<=(p[i].r+p[j].r)) 76 { 77 g[i][j]=g[j][i]=1; 78 } 79 } 80 } 81 spfa(0,dis1); 82 spfa(1,dis2); 83 spfa(2,dis3); 84 int min1=inf; 85 for(int i=0; i<n; i++) 86 { 87 //printf("%d %d %d\n",dis1[i],dis2[i],dis3[i]); 88 min1=min(min1,dis1[i]+dis2[i]+dis3[i]); 89 } 90 if(min1==inf) printf("-1\n"); 91 else printf("%d\n",n-min1-1); 92 } 93 return 0; 94 }