Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4668 | Accepted: 1242 | Special Judge |
Description
Input
Output
Sample Input
1 3 3 1 5 3 6 3 5 2 4 3
Sample Output
29.00
Dijkstra
用优先队列,否则可能会超时。还是比较直白的
#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include <math.h> #include <stdio.h> #include <queue> using namespace std; #define MAX 9000000000 struct Node { int x,y; double time; double v; Node (){}; Node (int x,int y,double time,double v) { this->x=x; this->y=y; this->time=time; this->v=v; } friend bool operator <(Node a,Node b) { if(a.time==b.time) return a.v<b.v; return a.time>b.time; } }; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int n,m; int v; int vis[105][105]; int a[105][105]; double s[105][105]; void Dijsktra() { priority_queue<Node> q; q.push(Node(1,1,0,v)); while(!q.empty()) { Node term=q.top(); q.pop(); if(vis[term.x][term.y]) continue; vis[term.x][term.y]=1; for(int i=0;i<4;i++) { int xx=term.x+dir[i][0]; int yy=term.y+dir[i][1]; if(xx<1||xx>n||yy<1||yy>m) continue; if(vis[xx][yy]) continue; if(s[xx][yy]>term.time+1.0/term.v) { s[xx][yy]=term.time+1.0/term.v; q.push(Node(xx,yy,s[xx][yy],term.v*pow(2.0,a[term.x][term.y]-a[xx][yy]))); } } } } int main() { while(scanf("%d%d%d",&v,&n,&m)!=EOF) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) {scanf("%d",&a[i][j]);s[i][j]=MAX;} memset(vis,0,sizeof(vis)); s[1][1]=0; Dijsktra(); printf("%.2f\n",s[n][m]); } return 0; }