标签:
题目大意:有两只青蛙,一只青蛙要调到第二只青蛙,它可以借助任意石头
要求的是青蛙1到青蛙2所有路径中,该路径下的最大边,但该边比其他路径下的最大边要小
算法:Prim
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int maxs = 200+5; 7 const float INF = (1<<30)-1; 8 int N; 9 float edge[maxs][maxs]; 10 int point[maxs][2]; 11 void init() 12 { 13 for(int i=1;i<=N;i++) 14 for(int j=1;j<=N;j++) 15 edge[i][j]=sqrt((point[i][0]-point[j][0])*(point[i][0]-point[j][0])+(point[i][1]-point[j][1])*(point[i][1]-point[j][1])); 16 } 17 float prim() 18 { 19 init(); 20 bool vis[maxs]; 21 memset(vis,false,sizeof(vis)); 22 vis[1]=true; 23 float lowcost[maxs],ans=0.0; 24 for(int i=1;i<=N;i++) 25 lowcost[i]=edge[1][i]; 26 for(int i=2;i<=N;i++) 27 { 28 float mins = INF; 29 int k=0; 30 for(int j=1;j<=N;j++) 31 if(!vis[j]&&lowcost[j]<mins) 32 { 33 mins=lowcost[j]; 34 k=j; 35 } 36 if(mins>ans) 37 ans=mins; 38 if(k==2)//k=2时已经到达终点不需再找,自己也是在这个地方wa了n次,继续找就有可能更新掉答案 39 break; 40 vis[k]=true; 41 for(int j=1;j<=N;j++) 42 if(!vis[j]&&edge[k][j]<lowcost[j]) 43 lowcost[j]=edge[k][j]; 44 } 45 return ans; 46 } 47 int main() 48 { 49 freopen("in.txt","r",stdin); 50 int T=1; 51 while(scanf("%d",&N)!=EOF&&N) 52 { 53 memset(point,0,sizeof(point)); 54 memset(edge,0,sizeof(edge)); 55 for(int i=1;i<=N;i++) 56 scanf("%d%d",&point[i][0],&point[i][1]); 57 getchar(); 58 printf("Scenario #%d\n",T++); 59 printf("Frog Distance = %.3f\n\n",prim()); 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/wt20/p/5729970.html