Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 1518 Accepted Submission(s): 607
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 const int INF=0x3f3f3f3f; 7 const int MAXN=200+10; 8 int dis[4][MAXN],w[MAXN][MAXN],vis[MAXN]; 9 struct point 10 { 11 int x,y; 12 int r; 13 }a[MAXN]; 14 int n; 15 16 int distans(int x1,int y1,int x2,int y2) 17 { 18 return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); 19 } 20 21 void input() 22 { 23 // for(int i=0;i<n;i++) 24 // for(int j=0;j<n;j++) 25 // w[i][j]=INF; 一直没有想明白为什么这样初始化得不到正确结果 26 memset(w,INF,sizeof(w)); 27 scanf("%d",&n); 28 for(int i=0;i<n;i++) 29 scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].r); 30 for(int i=0;i<n;i++) 31 { 32 for(int j=0;j<n;j++) 33 { 34 int temp=distans(a[i].x,a[i].y,a[j].x,a[j].y); 35 if(temp<=(a[i].r+a[j].r)*(a[i].r+a[j].r))//如果两点的距离小于两个圆半径之和,则赋值1 36 w[i][j]=1; 37 } 38 } 39 } 40 41 void dijkstra(int s) 42 { 43 memset(vis,0,sizeof(vis)); 44 for(int i=0;i<n;i++) 45 dis[s][i]=INF; 46 dis[s][s]=0; 47 for(int i=0;i<n;i++) 48 { 49 int x,temp=INF; 50 for(int j=0;j<n;j++) 51 { 52 if(!vis[j]&&dis[s][j]<temp) 53 { 54 temp=dis[s][j]; 55 x=j; 56 } 57 } 58 vis[x]=1; 59 for(int i=0;i<n;i++) 60 dis[s][i]=min(dis[s][i],dis[s][x]+w[x][i]); 61 } 62 } 63 64 int main() 65 { 66 //freopen("in.txt","r",stdin); 67 int kase; 68 scanf("%d",&kase); 69 while(kase--) 70 { 71 input(); 72 dijkstra(0); 73 dijkstra(1); 74 dijkstra(2); 75 int ans=INF; 76 for(int i=0;i<n;i++) 77 { 78 if(dis[0][i]!=INF&&dis[1][i]!=INF&&dis[2][i]!=INF)//这个条件还是得要,如果不要的话INF相加会超int 79 ans=min(ans,dis[0][i]+dis[1][i]+dis[2][i]); 80 } 81 //printf("n=%d\nans=%d\n",n,ans); 82 if(ans==INF) 83 printf("-1\n"); 84 else 85 printf("%d\n",n-1-ans); 86 } 87 return 0; 88 }
HDU 3832 Earth Hour (最短路),布布扣,bubuko.com
原文:http://www.cnblogs.com/clliff/p/3905691.html