题目描述
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.输入格式
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.
输出格式
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”.
输入样例
4 10
17 0
27 0
37 0
45 0
1 10
20 30输出样例
42.50 5
can’t be saved
分析
题目大意是一个人从直径为15米的圆形小岛上通过踩鳄鱼来跳离湖水,这个湖的大小是一个100*100的正方形,圆形小岛在湖中心,湖的左右下角坐标分别为(-50,-50),(50,50)。题目给定鳄鱼数n和最长能跳距离d以及这些鳄鱼的坐标,现在要求人的离开湖水的最短距离和在这个情况下的最小步长。
由于是多汇点,自然而然就会想到建立超级汇点,那么把岛记为0号,终点记为n+1,然后计算这n+2个点的最短距离。注意如果起点0与点v存在边,那么e(0,v)的大小要在两点间距的距离上再减去7.5,当然要保证这个大小是≥0的。而如果点v与终点n+1存在边,那么e(v,n+1)的大小取决于点v的坐标到湖边界的最小距离。
建完图后,套用模板求最短路和最短路的最短步数即可,以下分别是dijkstra、spfa和spfa(SLF优化)的源码。
源程序
Dijkstra算法
#include <bits/stdc++.h>
#define MAXN 105
#define INF 1e9
#define k 1e-8 //精度
using namespace std;
struct Edge{
int v;
double w;
bool operator <(const Edge a)const{
return w>a.w;
};
};
int n,x[MAXN],y[MAXN],step[MAXN];
double d,dis[MAXN],g[MAXN][MAXN];
bool used[MAXN];
void dijkstra(int s,int t)
{
priority_queue<Edge> q;
for(int i=0;i<=n+1;i++)
dis[i]=step[i]=INF,used[i]=false;
dis[s]=step[s]=0;
q.push(Edge{s,0});
while(!q.empty()){
int u=q.top().v;q.pop();
if(used[u])continue;
used[u]=true;
for(int v=0;v<=n+1;v++){
if(dis[v]>dis[u]+g[u][v]){
dis[v]=dis[u]+g[u][v];
step[v]=step[u]+1;
q.push(Edge{v,dis[v]});
}
else if(fabs(dis[v]-dis[u]-g[u][v])<k){ //找到相同最短路径
if(step[v]>step[u]+1)step[v]=step[u]+1;
}
}
}
if(dis[t]>=INF)printf("can't be saved\n");
else printf("%.2lf %d\n",dis[t],step[t]);
}
int main()
{
while(~scanf("%d%lf",&n,&d)){
if(n==0){ //没有鳄鱼
if(d>=42.5) printf("42.50 1\n");
else printf("can't be saved");
continue;
}
for(int i=1;i<=n;i++) //读入数据
scanf("%d%d",&x[i],&y[i]);
x[0]=y[0]=0; //起点
for(int i=0;i<=n;i++) //建图
for(int j=i;j<=n;j++){
double tmp=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); //距离
if(i==0)tmp-=7.5; //从中心岛出发
if(tmp>d) g[i][j]=g[j][i]=INF;
else g[i][j]=g[j][i]=max(tmp,0.0); //岛上可能存在鳄鱼
}
for(int i=0;i<=n;i++){ //到终点距离
int mmin=min(50-x[i],50-y[i]);
mmin=min(mmin,min(50+x[i],50+y[i]));
if(mmin>d)g[i][n+1]=g[n+1][i]=INF;
else g[i][n+1]=g[n+1][i]=mmin;
}
dijkstra(0,n+1);
}
}
SPFA算法
#include <bits/stdc++.h>
#define MAXN 105
#define INF 1e9
#define k 1e-8 //精度
using namespace std;
int n,x[MAXN],y[MAXN],step[MAXN],ven[MAXN];
double d,dis[MAXN],g[MAXN][MAXN];
void spfa(int s,int t)
{
queue<int> q;
for(int i=0;i<=n+1;i++)dis[i]=step[i]=INF,ven[i]=0; //初始化
dis[s]=step[s]=0;
ven[s]=1; //标记s在队列中
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
ven[u]=0;
for(int v=0;v<=n+1;v++){
if(dis[v]>dis[u]+g[u][v]){ //更新最短路
dis[v]=dis[u]+g[u][v];
step[v]=step[u]+1;
if(!ven[v]){ //数据保证不存在负环
q.push(v);
ven[v]=1;
}
}
else if(fabs(dis[v]-dis[u]-g[u][v])<k){ //相同最短路步长最短
if(step[v]>step[u]+1){
step[v]=step[u]+1;
if(!ven[v]){ //数据保证不存在负环
q.push(v);
ven[v]=1;
}
}
}
}
}
if(dis[t]!=INF)printf("%.2lf %d\n",dis[t],step[t]);
else printf("can't be saved\n");
}
int main()
{
while(~scanf("%d%lf",&n,&d)){
if(n==0){ //没有鳄鱼
if(d>=42.5) printf("42.50 1\n");
else printf("can't be saved");
continue;
}
for(int i=1;i<=n;i++) //读入数据
scanf("%d%d",&x[i],&y[i]);
x[0]=y[0]=0; //起点
for(int i=0;i<=n;i++) //建图
for(int j=i;j<=n;j++){
double tmp=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); //距离
if(i==0)tmp-=7.5; //从中心岛出发
if(tmp>d) g[i][j]=g[j][i]=INF;
else g[i][j]=g[j][i]=max(tmp,0.0); //岛上可能存在鳄鱼
}
for(int i=0;i<=n;i++){ //到终点距离
int mmin=min(50-x[i],50-y[i]);
mmin=min(mmin,min(50+x[i],50+y[i]));
if(mmin>d)g[i][n+1]=g[n+1][i]=INF;
else g[i][n+1]=g[n+1][i]=mmin;
}
spfa(0,n+1);
}
}
SPFA算法之SLF优化
#include <bits/stdc++.h>
#define MAXN 105
#define INF 1e9
#define k 1e-8 //精度
using namespace std;
int n,x[MAXN],y[MAXN],step[MAXN],ven[MAXN];
double d,dis[MAXN],g[MAXN][MAXN];
void spfa(int s,int t)
{
deque<int> q;
for(int i=0;i<=n+1;i++)dis[i]=step[i]=INF,ven[i]=0; //初始化
dis[s]=step[s]=0;
ven[s]=1; //标记s在队列中
q.push_back(s);
int cnt;
while(cnt=q.size()){
int u=q.front();q.pop_front();
ven[u]=0;
for(int v=0;v<=n+1;v++){
if(dis[v]>dis[u]+g[u][v]){ //更新最短路
dis[v]=dis[u]+g[u][v]; //比队首更小,优先更新
step[v]=step[u]+1;
if(!ven[v]){ //数据保证不存在负环
if(cnt>1&&dis[v]>dis[q.front()])q.push_front(v);
else q.push_back(v);
ven[v]=1;
}
}
else if(fabs(dis[v]-dis[u]-g[u][v])<k){ //相同最短路步长最短
if(step[v]>step[u]+1){
step[v]=step[u]+1;
if(!ven[v]){ //数据保证不存在负环
if(cnt>1&&dis[v]>dis[q.front()])q.push_front(v); //比队首更小,优先扩展
else q.push_back(v);
ven[v]=1;
}
}
}
}
}
if(dis[t]!=INF)printf("%.2lf %d\n",dis[t],step[t]);
else printf("can't be saved\n");
}
int main()
{
while(~scanf("%d%lf",&n,&d)){
if(n==0){ //没有鳄鱼
if(d>=42.5) printf("42.50 1\n");
else printf("can't be saved");
continue;
}
for(int i=1;i<=n;i++) //读入数据
scanf("%d%d",&x[i],&y[i]);
x[0]=y[0]=0; //起点
for(int i=0;i<=n;i++) //建图
for(int j=i;j<=n;j++){
double tmp=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); //距离
if(i==0)tmp-=7.5; //从中心岛出发
if(tmp>d) g[i][j]=g[j][i]=INF;
else g[i][j]=g[j][i]=max(tmp,0.0); //岛上可能存在鳄鱼
}
for(int i=0;i<=n;i++){ //到终点距离
int mmin=min(50-x[i],50-y[i]);
mmin=min(mmin,min(50+x[i],50+y[i]));
if(mmin>d)g[i][n+1]=g[n+1][i]=INF;
else g[i][n+1]=g[n+1][i]=mmin;
}
spfa(0,n+1);
}
}