ZZUOJ 10496: Cross the River (最短路 Dij 青蛙过河)
来源:互联网 发布:java 微信网页授权 编辑:程序博客网 时间:2024/06/08 06:03
郑州大学第十届ACM大学生程序设计竞赛正式赛 ——E题
10496: Cross the River
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 53 Solved: 6
[Submit][Status][Web Board]
Description
A frog wants to cross the river.The frog’s plane is determined by a rectangular coordinate system.The two sides of the river are respectively corresponding to the line y=0 and y=d (d>0).The frog is located in any coordinate which satisfies y<=0.The frog’s hop distance has a limit W.Now it wants to jump to the other side through the lotus leaves.Please select a reasonable route to minimize the journey.
Input
There are multiple test cases and the first line is the number of test cases T(1<=T<=10). For each test case,first line contains three integers d,w and n(0<d,w<=30000,0<n<=500,n is the number of lotus leaves), and in next n lines, the ith line contains the coordinate of the ith lotus leaf (xi,yi).(xi and yi are integers, and 0<yi<d )
Output
For each test case, output one line, the minimum of the distance.(The result keeps five decimal places)
If the frog can’t touch the other side,print “-1”.
Sample Input
1
6 3 2
1 2
2 4
Sample Output
6.23607
HINT
Source
#include<iostream>#include<string.h>#include<algorithm>#include<math.h>#include<stdio.h>#define INF 0x3f3f3f3fusing namespace std;#define N 1100int n,d,w;double maps[N][N],dist[N];int vis[N];struct node{ int x,y;} p[N];double Dist(int a,int b){ return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y));}void init(){ for(int i=0; i<=n+1; i++) { for(int j=0; j<=n+1; j++) { maps[i][j]=maps[j][i]=(i==j? 0 : INF); } vis[i]=0; }}void Dij(){ int index; for(int i=0;i<=n+1;i++) dist[i]=maps[0][i]; vis[0]=1; for(int i=0;i<=n+1;i++) { double MIN=INF; for(int j=0;j<=n+1;j++) { if(!vis[j]&&dist[j]<MIN) { MIN=dist[j]; index=j; } } vis[index]=1; for(int j=0;j<=n+1;j++) { if(!vis[j]&&dist[index]+maps[index][j]<dist[j]) dist[j]=dist[index]+maps[index][j]; } } if(dist[n+1]==INF) printf("-1\n"); else printf("%.5f\n",dist[n+1]);}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d%d",&d,&w,&n); init(); memset(p,0,sizeof(p)); for(int i=1; i<=n; i++) { scanf("%d%d",&p[i].x,&p[i].y); } if( d < w ) maps[0][n+1]=maps[n+1][0]=d; for(int i=0; i<=n+1; i++) { if(i==0) { for(int j=1; j<=n; j++) { if(w >= p[j].y) maps[i][j]=maps[j][i]=p[j].y; } } else if(i==n+1) { for(int j=1; j<=n; j++) { if(w >= (d-p[j].y)) maps[i][j]=maps[j][i]=d-p[j].y; } } else { for(int j=i+1; j<=n; j++) { if(Dist(i,j) <= w ) maps[i][j]=maps[j][i]=min(maps[i][j],Dist(i,j)); } } } Dij(); } return 0;}
- ZZUOJ 10496: Cross the River (最短路 Dij 青蛙过河)
- 【HDU 2544】最短路(dij)
- 【HDU 2544】最短路(Dij)
- 【POJ 2502】Subway(最短路dij)
- 1088: 最短路(SPFA算法 &dij)
- 最短路模板(dij+spfa)
- zoj2797_最短路dij
- Dij二级最短路
- dij最短路 变形
- POJ1062->最短路Dij
- hdu 3873 Invade the Mars(dij变体,带限制最短路)
- 【POJ 2387】Til the Cows Come Home(Dij最短路)
- POJ 1556 The Doors (计算几何+dij最短路)
- poj 3439(dij最短路)
- hdu 2544 最短路 (dij)
- HDU 2544 最短路 Dij
- 【jzoj4893】【过河】【最短路】
- hdu 2544 最短路(最短路径)(flody、dij、dij+priority queue、bellman、spfa)
- http1.0和1.1的区别
- 刘汝佳《算法竞赛入门经典》推荐题目
- [FUNC]ObjRegisterActive
- <<Linux内核完全剖析 --基于0.12内核>>学习笔记 第4章 80x86保护模式及其编程 4.5 保护
- Swift-计算属性
- ZZUOJ 10496: Cross the River (最短路 Dij 青蛙过河)
- html中图片的添加
- action获取request和session
- 最牛b的编码套路
- 关于Ubuntu下XXis not marked as executable. it may be dangerous to run. 程序无法执行问题
- MySQL数据库高可用性架构汇总
- 【矩阵分解】Python下基于Numpy的四种矩阵基本分解的实现
- 数据结构与算法(序)
- Java学习路线图