POJ 2253 C++ (图论)

POJ 2253 C++ (图论)

//前几天的题,别人都用floyd_warshall,我就用dijkstra;
//而这个题很多人都用dijkstra,我就用floyd_warshall;
//两个字,flody的代码敲起来就是简单,但效率比较的低,复杂度是(n^3)

#include<iostream>
#include<cmath>
#include <algorithm>
using namespace std;
int main()
{ int n,Case,i,j,k;
  int x[201],y[201];
  int map[201][201];
  freopen("in.txt","r",stdin);
  freopen("out.txt","w",stdout);
  Case=1;
  while(scanf("%d",&n),n!=0)
       {    memset(map,0,sizeof(map));
            for(i=1;i<=n;i++)
              scanf("%d%d",&x[i],&y[i]);
             for( i=1;i<=n;i++)
               for( j=i+1;j<=n;j++)
                   { map[i][j]=abs((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]));
                     map[j][i]=map[i][j];
                    }
          for( k=1;k<=n;k++)
              for( i=1;i<=n;i++)
                  for( j=i+1;j<=n;j++)
                      { if(map[i][k] && map[k][j] && map[i][k]<map[i][j] && map[k][j]<map[i][j])
                           { if(map[i][k]>map[k][j])
                                map[i][j]=map[i][k];
                             else
                                map[i][j]=map[k][j];
                           }  
                        
                            map[j][i]=map[i][j];
                       }          
        printf("Scenario #%d\n",Case++);
        printf("Frog Distance = %.3lf\n\n",sqrt(map[1][2]*1.0));
   }
  
   return 0;
}    

你可能感兴趣的:(POJ 2253 C++ (图论))