POJ 2253

题目链接:http://poj.org/problem?id=2387
题目说青蛙选择任意一条路径从第一块石头跳到第二块石头,计算这个路径里面的最远的两块石头之间的最小值。
这次首先选择floyd算法来更新每两个点之间最小跳跃距离

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*************************************************************************
> File Name: 2253.cpp
> Author: xudian
> Email: xudianc@gmail.com
> Created Time: 四 1/11 00:24:34 2018
************************************************************************/

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <queue>
#include <cstring>

using namespace std;
const int inf = 0x3f3f3f3f;
pair<int, int> pr[210];
int mp[210][210];
int n;
int duo(int x){
return x * x;
}
int get_dis(int a, int b){
return duo(pr[a].first - pr[b].first) + duo(pr[a].second - pr[b].second);
}
void floyd(){
// 注意,一定是k,i,j
for (int k = 0; k < n; ++k){
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
mp[i][j] = min(mp[i][j], max(mp[i][k], mp[k][j]));
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int kase = 0;
while (cin >> n and n){
for (int i = 0; i < n; ++i){
cin >> pr[i].first >> pr[i].second;
for (int j = 0; j < i; ++j){
mp[i][j] = mp [j][i] = get_dis(i, j);
}
mp[i][i] = 0;
}
floyd();
cout << "Scenario #" << ++kase << endl;
cout << "Frog Distance = " << setiosflags(ios::fixed) << setprecision(3) << sqrt(mp[0][1]) << endl << endl;
}
return 0;
}

然后再用SPFA做一下,SPFA如果发现当前点更新的话,就放入队列里面进行下次更新。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*************************************************************************
> File Name: 2253.cpp
> Author: xudian
> Email: xudianc@gmail.com
> Created Time: 四 1/11 00:24:34 2018
************************************************************************/

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <queue>
#include <cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
pair<int, int> pr[210];
int mp[210][210];
int ans[210];
int vis[210];
int n;
int duo(int x){
return x * x;
}
int get_dis(int a, int b){
return duo(pr[a].first - pr[b].first) + duo(pr[a].second - pr[b].second);
}
void spfa(){
queue<int> Q;
memset(ans, 0x3f, sizeof ans);
memset(vis, 0, sizeof vis);
ans[0] = 0;
vis[0] = 1;
Q.push(0);
while (!Q.empty()){
int fnt = Q.front();
vis[fnt] = 0;
Q.pop();
for (int i = 0; i < n; ++i){
if (max(ans[fnt], mp[fnt][i]) < ans[i]){
ans[i] = max(ans[fnt], mp[fnt][i]);
if (vis[i] == 0){
Q.push(i);
vis[i] = 1;
}
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int kase = 0;
while (cin >> n and n){
for (int i = 0; i < n; ++i){
cin >> pr[i].first >> pr[i].second;
for (int j = 0; j < i; ++j){
mp[i][j] = mp [j][i] = get_dis(i, j);
}
mp[i][i] = 0;
}
spfa();
cout << "Scenario #" << ++kase << endl;
cout << "Frog Distance = " << setiosflags(ios::fixed) << setprecision(3) << sqrt(ans[1]) << endl << endl;
}
return 0;
}