hdu 1245 Saving James Bond(dijkstra)

Saving James Bond

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3973    Accepted Submission(s): 838


 

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.

 

 

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

 

 

题意:以坐标(0,0)为中心,有一个100*100的正方形水域。以坐标(0,0)为中心,15为直径的圆形小岛。水域中有很多鳄鱼。我们被困在小岛上,但是我们可以不断地跳到鳄鱼的头上来逃出水域。我能跳的距离是固定的。如果我最后能够逃出水域,输出我跳的最短距离,以及对应的最少步数。

思路:求解最短路径,我们可以将我能够跳到鳄鱼头上的点集合存起来作为起点,也能够将鳄鱼距离边界小于等于我跳远的距离的点集合存起来作为终点,用dijkstra算法求解最短路,距离相等时保存更小的步数。还要考虑特殊的情况,如果你的跳远的距离能够使你从小岛跳到边界,直接输出42.50 1。

注意:浮点型数据判断相等时使用fabs(a-b)<1e-8。

AC代码:

#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
int N;
double M;
const int MAXN = 110;
const int INF = 0x3f3f3f3f;
struct coo{
	int x,y;
}a[MAXN];
struct edge{
	coo to;
	double cost;
};

//计算平面内两点的距离。
double dis(coo a,coo b){
	double ans=sqrt((a.x-b.x*1.0)*(a.x-b.x*1.0)+(a.y-b.y*1.0)*(a.y-b.y*1.0));
	return ans;
}
coo s[MAXN],e[MAXN];//起点坐标,终点坐标
double d[MAXN][MAXN];//到达某点的距离
int step[MAXN][MAXN];//到达某点的步数
vector<edge> G[MAXN][MAXN];//两点之间的距离小于等于跳的距离的边
//第一个元素代表到达coo点的最短距离,coo代表当前点。
struct P{
	double first;
	coo second;
	bool operator<(const P &a)const{
		return first>a.first;//最小值优先
	}
};
priority_queue<P> q;//优先队列,每次取当前的最短距离和定点的值对
void dijkstra(coo s){
	for(int i=0;i<MAXN;i++){
		for(int j=0;j<MAXN;j++){
			d[i][j]=INF;
			step[i][j]=0;
		}
	}
	d[s.x][s.y]=dis(s,(coo){50,50})-7.5;//跳的第一步
	step[s.x][s.y]=1;
	q.push((P){d[s.x][s.y],s});
	while(!q.empty()){
		P now = q.top();q.pop();
		coo v=now.second;
		if(now.first>d[v.x][v.y])
			continue;
		for(int i=0;i<G[v.x][v.y].size();i++){
			edge e=G[v.x][v.y][i];
			if(fabs(d[e.to.x][e.to.y]-(d[v.x][v.y]+e.cost))<1e-8){
				step[e.to.x][e.to.y]=min(step[e.to.x][e.to.y],step[v.x][v.y]+1);
			}
			else if(d[e.to.x][e.to.y]>d[v.x][v.y]+e.cost){
				d[e.to.x][e.to.y]=d[v.x][v.y]+e.cost;
				step[e.to.x][e.to.y]=step[v.x][v.y]+1;
				q.push((P){d[e.to.x][e.to.y],e.to});
			}
		}
	}
}
int main(){
	while(~scanf("%d%lf",&N,&M)){
		for(int i=0;i<MAXN;i++){
			for(int j=0;j<MAXN;j++){
				G[i][j].clear();
			}
		}
		while(!q.empty()){
			q.pop();
		}
		int cnts=0;//记录多少个起点
		int cnte=0;//记录多少个终点
		for(int i=0;i<N;i++){
			scanf("%d%d",&a[i].x,&a[i].y);
			a[i].x+=50;a[i].y+=50;
			double res=dis(a[i],(coo){50,50});
			if(res<=7.5+M){
				s[cnts++]=a[i];
			}
			double len=min((100-a[i].x),(a[i].x));
			double len2=min((100-a[i].y),(a[i].y));
			len=min(len,len2);
			if(len<=M){
				e[cnte++]=a[i];
			}
		}
		if(7.5+M>=50){
			printf("42.50 1\n");
			continue;
		}
		if(cnts==0||cnte==0){
			printf("can't be saved\n");
			continue;
		}
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				if(i!=j){
					//我跳地比两个鳄鱼之间的距离远
					double x=dis(a[i],a[j]);
					if(x<=M){
						G[a[i].x][a[i].y].push_back((edge){a[j],x});
					}
				}
			}
		}
		double ans=INF;
		int ansStep=INF;
		for(int i=0;i<cnts;i++){
			dijkstra(s[i]);	
			for(int j=0;j<cnte;j++){
				double len=min((100-e[j].x),(e[j].x));
				double len2=min((100-e[j].y),(e[j].y));
				len=min(len,len2);
				if(fabs(d[e[j].x][e[j].y]+len-ans)<1e-8){
					ansStep=min(ansStep,step[e[j].x][e[j].y]);
				}
				else if(d[e[j].x][e[j].y]+len<ans){
					ans=d[e[j].x][e[j].y]+len;
					ansStep=step[e[j].x][e[j].y];
				}
			}
		}
		if(ans!=INF){
			printf("%.2lf %d\n",ans,ansStep+1);
		}else{
			printf("can't be saved\n");
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值