Saving James Bond
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3601 Accepted Submission(s): 769
Problem Description
This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100×100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether he could escape.If he could,tell him the shortest length he has to jump and the min-steps he has to jump for shortest length.
Assume that the lake is a 100×100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether he could escape.If he could,tell him the shortest length he has to jump and the min-steps he has to jump for shortest length.
Input
The input consists of several test cases. Each case starts with a line containing n <= 100, the number of crocodiles, and d > 0, the distance that James could jump. Then one line follows for each crocodile, containing the (x, y) location of the crocodile. Note that x and y are both integers, and no two crocodiles are staying at the same position.
Output
For each test case, if James can escape, output in one line the shortest length he has to jump and the min-steps he has to jump for shortest length. If it is impossible for James to escape that way, simply ouput "can't be saved".
Sample Input
4 10 17 0 27 0 37 0 45 0 1 10 20 30
Sample Output
42.50 5 can't be saved
Author
weigang Lee
Recommend
把岛看做点1,湖外看做点n+1,先计算出这n+2个点之间的距离,若超过D则设为不连通,然后再用Floyd求最短路。(主要卡精度)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define EPS 10e-6
#define N 200
int steps[N][N];
double dis[N][N];
double X[N], Y[N];
int n;
double m;
double min(double a, double b)
{
return a < b ? a : b;
}
double distance(int x1, int y1, int x2, int y2)
{
return sqrt(1.0 * ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
}
void floyd()
{
for(int k = 1; k <= n+1; k++) {
for(int i = 1; i <= n+1; i++) {
for(int j = 1; j <= n+1; j++) {
if(dis[i][j] > dis[i][k] + dis[k][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
steps[i][j] = steps[i][k] + steps[k][j];
}
}
}
}
}
int main()
{
while(scanf("%d%lf",&n, &m) != EOF) {
int k = 2;
for(int i = 1; i <= n; i++) { //数据合法
double x,y;
scanf("%lf%lf",&x,&y);
if(fabs(x)-7.5 <= EPS && abs(y)-7.5 <= EPS)
continue;
if(fabs(x)-50 >= EPS && fabs(y) - 50 >= EPS)
continue;
X[k] = x;
Y[k++] = y;
}
n = k - 1;
if(n == 1) {
if(m - 42.5 < EPS)
printf("42.50 1\n");
else
printf("can't be saved\n");
continue;
}
X[1] = 0,Y[1] = 0;
memset(steps, 0, sizeof(steps));
for(int i = 1;i <= n+2; i++)
for(int j = 1;j <= n + 2; j++) {
if(i == j) {
dis[i][j] = 0;
continue;
}
dis[i][j] = INF;
}
for(int i = 1; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
double t = distance(X[i], Y[i], X[j], Y[j]);
if(i == 1)
t -= 7.5;
if(t - m > EPS)
continue;
dis[i][j] = dis[j][i] = t;
steps[i][j] = steps[j][i] = 1;
}
}
for(int i = 1; i <= n; i++) {//岛外点距离
double t = min(50 - X[i], 50 - Y[i]);
t = min(t, min(50 + X[i], 50 + Y[i]));
if(t - m > EPS)
continue;
dis[i][n+1] = dis[n+1][i] = t;
steps[i][n+1] = steps[n+1][i] = 1;
}
floyd();
//printf("%lf\n",dis[1][n+1] - INF);
if(steps[1][n+1]==0)
printf("can't be saved\n");
else
printf("%.2lf %d\n", dis[1][n+1],steps[1][n+1]);
}
return 0;
}