POJ 2253 Frogger
[★★☆☆☆]图论 最短路
题目大意:
给出两只青蛙的坐标A、B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的。显然从A到B存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离。
现在要求求出所有通路的最大距离,并把这些最大距离作比较,把最小的一个最大距离作为青蛙的最小跳远距离。样例
输入:
2
0 0
3 43
17 4
19 4
18 50
输出:
Scenario #1
Frog Distance = 5.000Scenario #2
Frog Distance = 1.414解题思路:
水题,用dijkstra算法稍微改一下更新条件就A了。
但是,居然,printf没有%lf,涨知识了。。。就因为这个WA了半天,蛋疼、、代码
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
const int INF = 1e9 + 7;
struct ZB{
double x, y;
};
double cost[205][205];
double d[205];
bool used[205];
int V;
ZB zb[205];
double jl(int a, int b) {
ZB t1 = zb[a];
ZB t2 = zb[b];
double t = sqrt((t1.x - t2.x) * (t1.x - t2.x) + (t1.y - t2.y) * (t1.y - t2.y) );
return t;
}
int main() {
int ct = 1;
while ( (cin >> V) && V != 0) {
for (int i = 1; i <= V; i++) {
cin >> zb[i].x >> zb[i].y;
}
for (int i = 1; i <= V; i++) {
for (int j = 1; j <= V; j++) {
cost[i][j] = jl(i, j);
}
}
fill(d, d+205, INF);
fill(used, used+205, false);
d[1] = 0;
while (1) {
int v = -1;
for (int u = 1; u <= V; u++) {
if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
}
if (v == -1) break;
used[v] = true;
for (int u = 1; u <= V; u++) {
d[u] = min(d[u], max(d[v], cost[v][u]));
}
}
printf("Scenario #%d\n", ct++);
printf("Frog Distance = %.3f\n\n", d[2]);
}
return 0;
}