Frogger--POJ 2253

1、题目类型:图论、最小生成树、Prim算法。

2、解题思路:Prim算法生成最小生成树过程中寻找最长Edge。

3、注意事项:每个case结束后,输出空行;pow()函数中需强制转换double。

4、实现方法:

  
    
#include < iostream >
#include
< cmath >
#include
< algorithm >
using namespace std;
#define Max 210
#define INF 9999999.9

struct TNode{
int x,y;
};
TNode Node[Max];
int n,casenum,cnt;
double map[Max][Max];
bool vis[Max];

double Prim()
{
int i,j,pos;
double dis[Max],ans = 0 ;
memset(vis,
0 , sizeof (vis));
for (i = 0 ;i < n;i ++ )
dis[i]
= map[ 0 ][i];
vis[
0 ] = 1 ;
for (i = 0 ;i < n - 1 ;i ++ )
{
pos
= min_element(dis + 1 ,dis + n) - dis;
if (ans < dis[pos])
ans
= dis[pos];
if (pos == 1 )
break ;
else
{
vis[pos]
= 1 ;
dis[pos]
= INF;
for (j = 0 ;j < n;j ++ )
{
if ( ! vis[j] && dis[j] > map[pos][j])
dis[j]
= map[pos][j];
}
}
}
return ans;
}

int main()
{
int i,j;
while (cin >> n && n)
{
casenum
++ ;
for (i = 0 ;i < n;i ++ )
{
cin
>> Node[i].x >> Node[i].y;
}
for (i = 0 ;i < n;i ++ )
{
map[i][i]
= INF;
for (j = i + 1 ;j < n;j ++ )
{
map[i][j]
= map[j][i] = sqrt(pow(( double )(Node[i].x - Node[j].x), 2 ) + pow(( double )(Node[i].y - Node[j].y), 2 ));
}
}
cout
<< " Scenario # " << casenum << endl;
printf(
" Frog Distance = %.3f\n " ,Prim());
cout
<< endl;
}
return 1 ;
}

 

你可能感兴趣的:(poj)