HDU 1245

/* * 解法: 控制精度, 各种方法都能过. */ #include <iostream> #include <cmath> #include <queue> using namespace std; const double eps = 1e-8; const double inf = 1e8; const int maxn = 105; struct Position { double x, y; void input() { scanf("%lf %lf", &x, &y); } } croc[maxn]; int n; double d; double dis[maxn][maxn]; double Distance(const Position &a, const Position &b) { double dx = a.x - b.x; double dy = a.y - b.y; return sqrt(dx * dx + dy * dy); } double StartDis(const Position &a) { return fabs(sqrt(a.x * a.x + a.y * a.y) - 7.5); } double EndDis(const Position &a) { return min(fabs(50.0 - fabs(a.x)), fabs(50.0 - fabs(a.y))); } queue<int> Q; int step[maxn]; double cost[maxn]; void BFS() { int i, now, nxt; for (i = 1; i <= n + 1; i++) cost[i] = inf; while (!Q.empty()) Q.pop(); cost[0] = 0.0; step[0] = 0; Q.push(0); while (!Q.empty()) { now = Q.front(); Q.pop(); for (nxt = 1; nxt <= n + 1; nxt++) { if (dis[now][nxt] < d + eps && ((cost[nxt] > cost[now] + dis[now][nxt] + eps) || (fabs(cost[nxt] - (cost[now] + dis[now][nxt])) < eps && step[nxt] > step[now] + 1))) { cost[nxt] = cost[now] + dis[now][nxt]; step[nxt] = step[now] + 1; Q.push(nxt); } } } } int main() { int i, j; while (scanf("%d %lf", &n, &d) != EOF) { for (i = 1; i <= n; i++) croc[i].input(); // 判断是否能直接跳到岸上 if (d + eps > 42.50) { printf("42.50 1/n"); continue; } for (i = 1; i < n; i++) { dis[i][i] = 0.0; for (j = i + 1; j <= n; j++) { dis[i][j] = dis[j][i] = Distance(croc[i], croc[j]); } } for (i = 1; i <= n; i++) { dis[0][i] = StartDis(croc[i]); dis[i][0] = 0.0; dis[i][n+1] = EndDis(croc[i]); dis[n+1][i] = 0.0; } dis[0][n+1] = inf; BFS(); if (fabs(cost[n+1] - inf) < eps) printf("can't be saved/n"); else printf("%.2lf %d/n", cost[n+1], step[n+1]); } return 0; } 

你可能感兴趣的:(struct,input,distance)