题目链接:http://poj.org/problem?id=2253
就是求所有路径的最大边权值的最小值
处理时每次找出距离当前的已选的节点的最短距离,然后更新每个未选节点的值
代码:
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 using namespace std; 7 #define maxn 210 8 #define INF 1010101010 9 int n; 10 double G[maxn][maxn]; 11 double dis[maxn]; 12 int s[maxn]; 13 class point 14 { 15 public: 16 double x; 17 double y; 18 }; 19 point p[maxn]; 20 double dist( point a ,point b) 21 { 22 return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 23 } 24 void Dijkstra() 25 { 26 27 for(int i=1;i<=n;i++) 28 { 29 dis[i]=G[1][i]; 30 s[i]=0; 31 } 32 s[1]=1; 33 34 for(int i=1;i<n;i++) 35 { 36 int Min=INF; 37 int u=-1; 38 for(int j=2;j<=n;j++) 39 { 40 if(s[j]==0 && dis[j]<Min) 41 { 42 Min=dis[j]; 43 u=j; 44 } 45 } 46 if(u==-1) break; 47 s[u]=1; 48 for(int i=1;i<=n;i++) 49 { 50 if(s[i]==0 && max(dis[u],G[u][i])<dis[i]) 51 dis[i]=max(dis[u],G[u][i]); 52 } 53 } 54 } 55 int main() 56 { 57 int iCase=0; 58 while(scanf("%d",&n)!=EOF && n) 59 { 60 61 for(int i=1;i<=n;i++) 62 { 63 scanf("%lf%lf",&p[i].x,&p[i].y); 64 } 65 for(int i=1;i<=n;i++) 66 for(int j=1;j<=n;j++) 67 { 68 G[i][j]=G[j][i]=dist(p[i],p[j]); 69 } 70 71 Dijkstra(); 72 cout<<"Scenario #"<<++iCase<<endl; 73 printf("Frog Distance = %.3f ",dis[2]); 74 75 } 76 }