POJ 2253 Frogger

解题思路:最短路径dijkstra算法

  
    
#include < iostream >
#include
< cmath >
#include
< iomanip >
using namespace std;

#define MaxNum 1e8
int main()
{
int n, start, tempS;
bool visited[ 205 ];
double dist[ 205 ];
double minDist, tempDist;
int cases = 0 ;
struct
{
int x, y;
}p[
205 ];

while (cin >> n && n)
{
cases
++ ;
memset(visited,
0 , sizeof ( bool ) * 205 );
fill(
& dist[ 0 ], & dist[ 204 ], MaxNum);

for ( int i = 0 ; i < n; i ++ )
cin
>> p[i].x >> p[i].y;

visited[
0 ] = true ;
dist[
0 ] = 0 ;
start
= 0 ;

for ( int i = 0 ; i < n; i ++ )
{
minDist
= MaxNum;
for ( int j = 0 ; j < n; j ++ )
{
if ( ! visited[j])
{
tempDist
= pow( double (p[start].x - p[j].x), 2 );
tempDist
+= pow( double (p[start].y - p[j].y), 2 );
tempDist
= sqrt(tempDist);
tempDist
= max(tempDist, dist[start]);
dist[j]
= min(dist[j], tempDist);

if (minDist > dist[j])
{
minDist
= dist[j];
tempS
= j;
}
}
}
start
= tempS;
visited[start]
= true ;
if (start == 1 )
break ;
}
if (cases != 1 )
cout
<< " \n " ;
cout
<< " Scenario # " << cases << endl;
cout
<< " Frog Distance = " << fixed << setprecision( 3 ) << dist[ 1 ] << endl;
}
return 0 ;
}

 

你可能感兴趣的:(poj)