Dij二级最短路
Saving James Bond
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1877 Accepted Submission(s): 356
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.
that x and y are both integers, and no two crocodiles are staying at the same position.
4 10
17 0
27 0
37 0
45 0
1 10
20 30
42.50 5
can't be saved
题意:
二级最短路算法:
方法一
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"math.h"
#define M 40
#define eps 1e-10
#define inf 99999999
#define mod 1000000000
using namespace std;
struct st
{
int u,v,next;
double w;
}edge[30000];
int head[111],use[111],n,t,time[111];
double dis[111];
struct node
{
double x,y;
}p[111];
double pow(double x)
{
return x*x;
}
double Len(node a,node b)
{
return sqrt(pow(a.x-b.x)+pow(a.y-b.y));
}
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,double w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void bfs(int S)
{
int i;
queue<int>q;
memset(use,0,sizeof(use));
for(i=0;i<=n;i++)
{
time[i]=dis[i]=inf;
}
dis[S]=time[S]=0;
q.push(S);
while(!q.empty())
{
int u=q.front();
q.pop();
use[u]=1;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
time[v]=time[u]+1;
}
if(fabs(dis[v]-dis[u]-edge[i].w)<eps)
{
if(time[v]>time[u]+1)
time[v]=time[u]+1;
}
if(!use[v])
q.push(v);
}
}
}
int judge(double x,double y)
{
if(x>=-50&&x<=50&&y>=-50&&y<=50&&x*x+y*y>=7.5*7.5)
return 1;
return 0;
}
int main()
{
int m,i,j;
double d;
while(scanf("%d%lf",&m,&d)!=-1)
{
p[0].x=p[0].y=0;
for(i=1;i<=m;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
init();
for(i=1;i<=m;i++)
{
for(j=i+1;j<=m;j++)
{
if(judge(p[i].x,p[i].y)&&judge(p[j].x,p[j].y))
{
double L=Len(p[i],p[j]);
if(L<=d)
{
add(i,j,L);
add(j,i,L);
}
}
}
}
n=m+1;
for(i=1;i<=m;i++)
{
if(judge(p[i].x,p[i].y))
{
double L=Len(p[0],p[i]);
if(L-7.5<=d)
{
add(0,i,L-7.5);
add(i,0,L-7.5);
}
double L1=min(50-p[i].x,50+p[i].x);
double L2=min(50-p[i].y,50+p[i].y);
L=min(L1,L2);
if(L<=d)
{
add(i,n,L);
add(n,i,L);
}
}
}
if(d>=50-7.5)
{
add(0,n,50-7.5);
add(n,0,50-7.5);
}
bfs(0);
if(dis[n]<inf)
printf("%.2lf %d\n",dis[n],time[n]);
else
printf("can't be saved\n");
}
return 0;
}
方法二:dij
#include"stdio.h"
#include"string.h"
#include"math.h"
#define M 111
#define inf 99999999
#define eps 1e-8
#include"iostream"
using namespace std;
struct node
{
double x,y;
}p[M];
int use[M],time[M][M],mint[M],n;
double dis[M],G[M][M];
double pow(double x)
{
return x*x;
}
double Len(node a,node b)
{
return sqrt(pow(a.x-b.x)+pow(a.y-b.y));
}
void dij(int s)
{
int i;
memset(use,0,sizeof(use));
for(i=0;i<=n;i++)
{
dis[i]=G[s][i];
mint[i]=time[s][i];
}
mint[s]=0;
dis[s]=0;
use[s]=1;
int w=n;
while(w--)
{
double min=inf;
int tep=-1;
for(i=0;i<=n;i++)
{
if(!use[i]&&dis[i]<min)
{
min=dis[i];
tep=i;
}
}
if(tep==-1)
return;
use[tep]=1;
for(i=0;i<=n;i++)
{
if(!use[i]&&dis[i]>dis[tep]+G[tep][i])
{
dis[i]=dis[tep]+G[tep][i];
mint[i]=mint[tep]+time[tep][i];
}
else if(!use[i]&&fabs(dis[i]-dis[tep]-G[tep][i])<eps)
{
if(mint[i]>mint[tep]+time[tep][i])
mint[i]=mint[tep]+time[tep][i];
}
}
}
}
int ok(double x,double y)
{
if(x>=-50&&y>=-50&&x<=50&&y<=50&&x*x+y*y>=7.5*7.5)
return 1;
return 0;
}
int main()
{
int m,i,j;
double d;
while(scanf("%d%lf",&m,&d)!=-1)
{
n=m+1;
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
time[i][j]=G[i][j]=inf;
G[i][i]=time[i][i]=0;
}
p[0].x=p[0].y=0;
for(i=1;i<=m;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
if(ok(p[i].x,p[i].y)&&ok(p[j].x,p[j].y))
{
double L=Len(p[i],p[j]);
if(d>=L)
{
G[i][j]=G[j][i]=L;
time[i][j]=time[j][i]=1;
} }
}
}
for(i=1;i<=m;i++)
{
if(ok(p[i].x,p[i].y))
{
double L1=min(50-p[i].x,50+p[i].x);
double L2=min(50-p[i].y,50+p[i].y);
double L=min(L1,L2);
if(d>=L)
{
G[i][n]=G[n][i]=L;
time[i][n]=time[n][i]=1;
} L=Len(p[i],p[0])-7.5;
if(d>=L)
{
G[0][i]=G[i][0]=L;
time[0][i]=time[i][0]=1;
} }
}
if(d>=42.5)
{
G[0][n]=G[n][0]=42.5;
time[0][n]=time[n][0]=1;
} dij(0);
if(dis[n]<inf)
printf("%.2lf %d\n",dis[n],mint[n]);
else
printf("can't be saved\n");
}
return 0;
}
Dij二级最短路的更多相关文章
- PAT 1087【二级最短路】
二级最短路+二级最短路,就是DP过程吧. 代码稍微注释一些,毕竟贴代码不好.. #include<bits/stdc++.h> using namespace std; typedef l ...
- 51nod1459【二级最短路】
标签说的是BFS... 太菜,不知道怎么BFS...是不是spfa写,就叫BFS...感觉不是.... 只是二级最短路的写法,直接搞就很容易了,简单题: #include <bits/stdc+ ...
- 链式前向星实现的堆优化dij求最短路模板
#include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...
- UVA 10801 Dij最短路(改模板)
题意:有n个电梯,目的地是第K层(起点是第0层),给出每个电梯的速度,以及每个电梯能到达的层数,如果中途需要换电梯的话,时间需要+60,求到达目的地的最短时间: 思路:Dij求最短路.如果是另一条路比 ...
- Codeforces 545E. Paths and Trees 最短路
E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...
- vijos 1423 最短路or环(有向图)
最佳路线 描述 年久失修的赛道令国际汽联十分不满.汽联命令主办方立即对赛道进行调整,否则将取消其主办权.主办方当然必须马上开始行动. 赛道测评人员经过了三天三夜的数据采集,选出了若干可以使用的道路和各 ...
- 【BZOJ2622】[2012国家集训队测试]深入虎穴 次短路
[BZOJ2622][2012国家集训队测试]深入虎穴 Description 虎是中国传统文化中一个独特的意象.我们既会把老虎的形象用到喜庆的节日装饰画上,也可能把它视作一种邪恶的可怕的动物,例如“ ...
- Luogu3953 NOIP2017逛公园(最短路+拓扑排序+动态规划)
跑一遍dij根据最短路DAG进行拓扑排序,按拓扑序dp即可.wa了三发感觉非常凉. #include<iostream> #include<cstdio> #include&l ...
- DIJ的优化,和spfa的优化
SPFA和DIJ求最短路的算法的坑点一直是很多的.经常会让人搞不懂. 易错案例: 用重载运算符来排序,如: struct cmp { bool operator ()(int x, int y) co ...
随机推荐
- 执行大数据量SQL文件
sqlserver2008中需要执行大文件的脚本,查询分析器中打不开,需要用到sql命令,开始使用osql命令 使用sqlcmd可以执行:在DOS中,调用sqlcmd命令,并使用对应选项 sql ...
- jquery实现简单瀑布流代码
测试环境:ie8 ff13.0.1 chrome22 可以将分页获取的内容依次填入四个div中,瀑布流的分页可以以多页(比如5页)为单位二次分页,这样可以减少后台算法的复杂度 <!DOCTYP ...
- 关于Cocos2d-x头文件的引用
cocos2d-x 3.10的G:\cocoshome\Cocos2d-x\cocos2d-x-3.10\extensions\GUI\CCControlExtension\CCScale9Sprit ...
- 配置 -- PHPstorm+Xdebug断点调试PHP
运行环境: PHPSTORM版本 : 8.0.1 PHP版本 : 5.6.2 xdebug版本:php_xdebug-2.2.5-5.6-vc11-x86_64.dll ps : php版本和xdeb ...
- jquery-创建元素和添加子元素
一.创建新元素 1.使用$函数创建新元素 var $newElement=$('<div><p>段落</p></div>');//创建元素,返回jQue ...
- c++ list 合并list
1.参考 http://www.cplusplus.com/reference/list/list/ 2.合并 主要有两个函数:splice()和merge()splice()有三种调用形式:第一种: ...
- Linux下安装subversion1.6.5和apache2
以下安装是在RHEL5.5默认安装的情况下,以root身份进行安装!这个实验我安装了n次,最后总是不成功,因为涉及到略多的软件和配置.下面是安装步骤和配置,自己记下来.希望给下次配置的时候不要像以前那 ...
- 怎样设置easyui中datagrid行高
$('#face_table2').datagrid({ title: '信息', iconCls: 'icon-save', url: 'callro ...
- EasyTouch的使用官方文档操作步骤
对于移动平台上的RPG类的游戏,我们常用虚拟摇杆来控制人物角色的行走和一些行为,相信我们对它并不陌生,之前尝试了EasyTouch2.5,发现并没有最新版的3.1好用,2.5版本的对于自适应没有做的很 ...
- Chrome浏览器无法观看视频,一直提示“adobe flash player 已过期” ?
很多新用户在安装了Chrome浏览器或者更新过的的时候,经常提示“ adobe flash player 已过期”的问题,反复提示,导致无法观看视频.于是从网上也找了很多办法都没有解决.这里给大 ...