POJ - 3037-Skiing(邻接表+Dijkstra)
Bessie starts out travelling at a initial speed V (1 <= V <= 1,000,000). She has discovered a remarkable relationship between her speed and her elevation change. When Bessie moves from a location of height A to an adjacent location of eight B, her speed is multiplied by the number 2^(A-B). The time it takes Bessie to travel from a location to an adjacent location is the reciprocal of her speed when she is at the first location.
Find the both smallest amount of time it will take Bessie to join her cow friends.
Input
* Lines 2..R+1: C integers representing the elevation E of the corresponding location on the grid.
Output
Sample Input
1 3 3
1 5 3
6 3 5
2 4 3
Sample Output
29.00
Hint
Start at 1,1 time 0 speed 1
East to 1,2 time 1 speed 1/16
South to 2,2 time 17 speed 1/4
South to 3,2 time 21 speed 1/8
East to 3,3 time 29 speed 1/4
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define Inf 0x3f3f3f3f const int maxn=1e5+;
typedef long long ll;
using namespace std;
ll ksm(ll x,ll y)
{
ll ans=;
while(y)
{
if(y&)
{
ans=ans*x;
}
x*=x;
y>>=;
}
return ans;
}
struct node
{
int to;
double w;
bool friend operator < (node x,node y)
{
return x.w>y.w;
}
};
int Map[]; vector<node>vec[];
double dis[];
int vis[];
int V,R,C;
int dir[][]={{,},{,-},{-,},{,}};
bool check(int x,int y)
{
if(x>=&&x<=R&&y>=&&y<=C)
{
return true;
}
else
{
return false;
}
}
void init()
{
for(int t=;t<=R*C;t++)
{
dis[t]=;
}
} void Dijkstra(int s)
{
node st;
st.to=s;
st.w=;
priority_queue<node>q;
q.push(st);
dis[s]=;
while(!q.empty())
{
node now=q.top();
q.pop();
if(vis[now.to])continue;
vis[now.to]=; int len=vec[now.to].size();
for(int t=;t<len;t++)
{
node tto=vec[now.to][t]; if(vis[tto.to]==&&tto.w+dis[now.to]<dis[tto.to])
{
tto.w=tto.w+dis[now.to];
dis[tto.to]=tto.w;
q.push(tto);
}
}
}
} int main()
{
// std::ios::sync_with_stdio(false);
scanf("%d%d%d",&V,&R,&C);
init();
for(int t=;t<=R;t++)
{
for(int j=;j<=C;j++)
{
scanf("%d",&Map[(t-)*C+j]);
}
}
for(int t=;t<=R;t++)
{
for(int j=;j<=C;j++)
{
for(int k=;k<;k++)
{
int xx=t+dir[k][];
int yy=j+dir[k][];
if(check(xx,yy))
{
node s;
s.to=(xx-)*C+yy;
s.w=1.0/V*ksm(,Map[(t-)*C+j]-Map[]);
vec[(t-)*C+j].push_back(s);
}
}
}
}
Dijkstra();
printf("%.2f\n",dis[R*C]); return ;
} AC的是邻接表的
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e4+;
typedef long long ll;
using namespace std; struct edge
{
int u,v;
double w;
int next;
}edge[maxn*]; struct node
{
int pos;
double w;
node(int x,double y)
{
pos=x;
w=y;
}
bool friend operator <(node x,node y)
{
return x.w>y.w;
}
};
int n,m,s,x,y,z,tot = ,V;
bool check(int x,int y)
{
if(x>=&&x<=n&&y>=&&y<=m)
{
return true;
}
else
{
return false;
}
}
int head[];
double d[];
int vis[];
int a[];
int dist[][] = {{-,},{,-},{,},{,}}; void add(int u,int v,double w)
{
edge[++tot].u=u;
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot;
return ;
} void Dijkstra(int s)
{
priority_queue<node>q;
d[s]=;
q.push(node(s,));
while(!q.empty())
{
node now=q.top();
q.pop();
//cout<<now.pos<<endl;
if(vis[now.pos])continue;
vis[now.pos]=; for(int i=head[now.pos];i!=-;i=edge[i].next)
{
int ne=edge[i].v;
double ww=edge[i].w;
if(d[now.pos]+ww<d[ne])
{
d[ne]=d[now.pos]+ww;
q.push(node(ne,d[ne]));
}
}
}
return ;
}
int main()
{
scanf("%d%d%d",&V,&n,&m);
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis)); for(int t=;t<=n*m;t++)
{
d[t]=;
}
for(int i = ;i <= n; ++i)
for(int j = ;j <= m; ++j)
scanf("%d",&a[(i-)*m+j]);
for(int i = ;i <= n; ++i)
for(int j = ;j <= m; ++j)
for(int k = ;k < ; ++k)
{
int x = i + dist[k][];
int y = j + dist[k][];
if(check(x,y))
{
double v = 1.0 / V * pow(2.0 , a[(i-)*m+j] - a[]);
add((i-)*m+j,(x-)*m+y,v);
}
}
Dijkstra();
printf("%.2f\n",d[n * m]);
return ;
}
POJ - 3037-Skiing(邻接表+Dijkstra)的更多相关文章
- POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化
昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...
- poj 1511(SPFA+邻接表)
题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...
- USACO 2008 January Silver Telephone Lines /// 二分最短路 邻接表dijkstra oj22924
题目大意: 一共有N (1 ≤ N ≤ 1,000)个电线杆,有P P (1 ≤ P ≤ 10,000)对电线杆是可以连接的, 用几条线连接在一起的电线杆之间都可相互通信,现在想要使得电线杆1和电线杆 ...
- POJ - 3255 SPFA+邻接表求次短路径
题意:给出m条边 , n个顶点,u [ i ]到v [ i ] 的距离w [ i ],求除了最短路的那条最短的边的长度. 思路:之前有做过相似的题,使用迪杰斯特拉算法求单源最短路径,并且记录路径,枚举 ...
- POJ 3037 Skiing(Dijkstra)
Skiing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4668 Accepted: 1242 Special ...
- POJ 3037 Skiing(如何使用SPFA求解二维最短路问题)
题目链接: https://cn.vjudge.net/problem/POJ-3037 Bessie and the rest of Farmer John's cows are taking a ...
- POJ - 3037 Skiing SPFA
Skiing Bessie and the rest of Farmer John's cows are taking a trip this winter to go skiing. One day ...
- Poj(2679),SPFA,邻接表(主流写法)
题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...
- hdu1839之二分+邻接表+Dijkstra+队列优化
Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- AsyncTask被废弃了,换Coroutine吧
本文主要是学习笔记,有版权问题还请告知删文 鸣谢:guolin@第一行代码(第三版) 你是否也在最近的代码中看见了 AsyncTask 被一条横杠划掉了 这表明--他要被Google放弃了 Googl ...
- python3.3while循环
#while循环与for循环不同的是,while循环的停止条件是自己设置! i=0#初始值while i<=10:#循环条件 print(i)#while循环嵌套if条件 if i==5: pr ...
- 说说Spring中的 @RestController 和 @Controller
Spring MVC执行流程已是JAVA面试中老生常谈的问题,相信各位小伙伴也是信手拈来.今天我们来谈谈另一个面试中必会必知的问题: @RestController和@Controller的区别? S ...
- Java三大特性与实战
三大特性: 封装,集成,多态 编程思想 类和对象: 方法的重载 this关键字 static关键字 静态代码块 package import Object 抽象类 接口 lambda表达式 字符串St ...
- 【java】解决java compiler level does not match the version of the installed java project facet
翻译内容:java编译器jdk版本与安装的java项目方面的版本不匹配 修改编译器jdk版本 项目右键选择->properties 如果项目的开发版本为,jdk1.8 ,选择修改为1.8 ,点击 ...
- C#LeetCode刷题之#389-找不同(Find the Difference)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4062 访问. 给定两个字符串 s 和 t,它们只包含小写字母. ...
- python基本数据类型(—)
数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值范围为-2** ...
- ybt1107题解和方法总结
今天花了三个小时的时间刷了些基础题,虽说是简单题,但是有一些还是有点难度的 比如ybt1107,我死嗑了半个小时. [题目描述] 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米. ...
- linux 部署jar包开机自启
1.用xShell将jar包上传到linux上(jar包上传到 /root/java) 输入rz命令,看是否已经安装了lrzsz,如果没有安装则执行 yum -y install lrzsz ...
- Jmeter 常用函数(11)- 详解 __TestPlanName
如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 返回测试计划名称 语法格式 ${__T ...