题目衔接:http://poj.org/problem?id=2253
题解:求出图中两点所有路径中权值最大数中的最小值,即可简化为求最小生成树中的距离最大值
AC代码:
Memory: 584K Time: 16MS
Language: C++ Result: Accepted
#include <iostream>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <cstdio>
using namespace std;
#define maxn 210
#define max 1000000
typedef struct stone{
int x;
int y;
}stone;
stone data[maxn];
double dis[maxn][maxn];
bool is_in_set[maxn];
int main()
{
int stones,cases=1;
while(cin>>stones && stones){
memset(data,0,sizeof(data));
memset(is_in_set,false,sizeof(is_in_set));
int n = stones;
stone start,end,temp;
cin>>start.x>>start.y;
cin>>end.x>>end.y;
n -= 2;
data[0] = start;
data[1] = end;
int i=2;
while(n){
cin>>temp.x>>temp.y;
data[i] = temp;
i++,n--;
}
for(int i=0;i<stones;i++){ //计算每两个节点之间的距离
int a,b,c,d;
a = data[i].x,b = data[i].y;
for(int j=0;j<stones;j++){
c = data[j].x,d = data[j].y;
dis[i][j] = sqrt(double((a-c)*(a-c)+(b-d)*(b-d)));
}
}
set<int> jumped;
double max_jump;
max_jump = 0;
is_in_set[0] = true;
jumped.insert(0);
int start_node,next_node;
while(1){
set<int>::iterator set_it = jumped.begin();
double min = 1<<20;
while(set_it != jumped.end()){ //寻找下一个离集合最近的点
for(int i=0;i<stones;i++){
if( !is_in_set[i] && dis[*set_it][i] < min){
start_node = *set_it;
min = dis[*set_it][i];
next_node = i;
}
}
set_it++;
}
is_in_set[next_node] = true;
jumped.insert(next_node);
if( dis[start_node][next_node] > max_jump )
max_jump = dis[start_node][next_node];
if(next_node == 1) //到达目的节点,退出循环
break;
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n",cases,max_jump);
cases++;
}
}