HDU 3832 Earth Hour (Shortest Path), hdu3832
Address: HDU 3832
I cannot prove this method. When I was inspired by this idea, I thought that if we wanted to cover at least the points, we should try to reuse them as much as possible, and then there should be two indirectly connected through another point, this will make full use of those points. Then I wrote it once, always WA .. Then I suddenly thought of a situation when I went to bed at noon. That is, there is a vertex that serves as the intermediate vertex and is connected to the three vertices. This situation is also consistent, but there will be repeated edges... But the opposite is true .. The more duplicate edges, the better .. Then we make full use of these points. So how should we find this point? Then you can enumerate them directly. However, it is obviously unscientific to calculate the shortest path for each enumeration point .. In any case, the shortest distance from the three points is used, and the short circuit from the other points to the three points is obtained only once. Then enumerate the sum of the distances between all vertices and three vertices to find the smallest one. Minus the minimum value with n is the maximum value to be closed.
The Code is as follows:
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int head[300], cnt, vis[300];int d[3][301];struct node1{ int x, y, r;} dian[1000000];struct node{ int u, v, w, next;} edge[1000000];void add(int u, int v, int w){ edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;}void spfa(int source, int x){ memset(d[x],INF,sizeof(d[x])); memset(vis,0,sizeof(vis)); d[x][source]=0; deque<int>q; q.push_back(source); while(!q.empty()) { int u=q.front(); q.pop_front(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(d[x][v]>d[x][u]+edge[i].w) { d[x][v]=d[x][u]+edge[i].w; if(!vis[v]) { vis[v]=1; if(!q.empty()&&d[x][v]<d[x][q.front()]) { q.push_front(v); } else { q.push_back(v); } } } } }}int main(){ int t, n, x, y, r, ans, i, j, min1; double z; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(head,-1,sizeof(head)); cnt=0; for(i=1; i<=n; i++) { scanf("%d%d%d",&dian[i].x,&dian[i].y,&dian[i].r); } for(i=1; i<=n; i++) { for(j=1; j<i; j++) { z=sqrt((dian[i].x-dian[j].x)*1.0*(dian[i].x-dian[j].x)+(dian[i].y-dian[j].y)*1.0*(dian[i].y-dian[j].y)); if(z<=dian[i].r+dian[j].r) { add(i,j,1); add(j,i,1); } } } spfa(1,0); spfa(2,1); spfa(3,2); min1=INF; if(d[0][2]==INF||d[0][3]==INF) { printf("-1\n"); continue ; } for(i=1;i<=n;i++) { if(d[0][i]!=INF&&d[1][i]!=INF&&d[2][i]!=INF) { if(min1>d[0][i]+d[1][i]+d[2][i]+1) { min1=d[0][i]+d[1][i]+d[2][i]+1; } } } printf("%d\n",n-min1); } return 0;}