题目链接:
hdu4198
题意:给一个地图,"."代表水,"#"表示陆地,"S"表示起始位置,"@"表示桥。只有"."和"@"可以走,但是走“@”的时候有一个打开桥的时间d,过桥还需要1分钟。问从S出发到边界最短时间是多少。
这个和普通的最短路的区别就是,这个相当于是一个图,上面的点有权重,一般的最短路是边上有权重。这里每个点最多只需要进队列一次就能找到最优解了,而spfa每个点可能进队列多次。
#include<iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; int R,C,d,sx,sy,ans; char grid[505][505]; bool vis[505][505]; int mov[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; struct node{ int r,c,step; friend bool operator < (node a, node b) { return a.step > b.step; } }; void bfs(){ priority_queue<node> pq; node t1,t2; int i; t1.r = sx; t1.c = sy; t1.step = 1; pq.push(t1); vis[sx][sy] = true; while(!pq.empty()){ t1 = pq.top(); pq.pop(); if(t1.r == 0 || t1.r == R-1 || t1.c == 0 || t1.c == C-1){ ans = t1.step; return; } for(i=0;i<4;i++){ t2.r = t1.r + mov[i][0]; t2.c = t1.c + mov[i][1]; if(t2.r>=0 && t2.r<R && t2.c>=0 && t2.c<C && grid[t2.r][t2.c]!='#' && (!vis[t2.r][t2.c])){ t2.step = t1.step + 1; if(grid[t2.r][t2.c]=='@'){ t2.step += d; } pq.push(t2); vis[t2.r][t2.c] = true; } } } } int main(){ int T,i,j; scanf("%d",&T); while(T--){ bool f = false; scanf("%d%d%d",&R,&C,&d); for(i=0;i<R;i++){ scanf("%s",grid[i]); if(f) continue; for(j=0;j<C;j++){ if(grid[i][j]=='S'){ sx = i; sy = j; f = true; } } } memset(vis,false,sizeof(vis)); ans = 0; bfs(); printf("%d\n",ans); } return 0; }