传送阵(bfs)
Dima overslept the alarm clock, which was supposed to raise him to school.
Dima wonders if he will have time to come to the first lesson. To do this, he needs to know the minimum time it will take him to get from home to school.
The city where Dima lives is a rectangular field of n×m size. Each cell (i,j) on this field is denoted by one number aijaij:
- The number −1 means that the passage through the cell is prohibited;
- The number 0 means that the cell is free and Dima can walk though it.
- The number x (1≤x≤10^9) means that the cell contains a portal with a cost of xx. A cell with a portal is also considered free.
From any portal, Dima can go to any other portal, while the time of moving from the portal (i,j) to the portal (x,y)corresponds to the sum of their costs aij+axy.
In addition to moving between portals, Dima can also move between unoccupied cells adjacent to one side in time ww. In particular, he can enter a cell with a portal and not use it.
Initially, Dima is in the upper-left cell (1,1), and the school is in the lower right cell (n,m).
The first line contains three integers nn, mm and ww (2≤n,m≤2⋅10^3, 1≤w≤10^9), where nn and mm are city size, ww is time during which Dima moves between unoccupied cells.
The next nn lines each contain mm numbers (−1≤a[i][j]≤109) — descriptions of cells.
It is guaranteed that the cells (1,1) and (n,m) are free.
Output the minimum time it will take for Dima to get to school. If he cannot get to school at all, then output "-1".
5 5 1 0 -1 0 1 -1 0 20 0 0 -1 -1 -1 -1 -1 -1 3 0 0 0 0 -1 0 0 0 0
14
Explanation for the first sample:

给定一个n*m的距阵,-1表示不能走,其他都可以走,每走一步的代价为k,在距阵上有一些传送点,(只要a[i][j]>0)就是传送点,每个传送点之间可以互相传送,假设当前在传送点i,j,传送到x,y这个点的代价为a[i][j] + a[x][y] , 问从1,1这个点走到n,m这个点的最小代价
这个题就是先处理出来d1[x][y]代表的是从(1,1)到(x,y)的最小步数。d2[x1][y1]代表的是从(x1,y1)到(n,m)的最小距离,
然后你在枚举(x,y)和(x1,y1)这样然后就更新ans=min(ans,d1[x][y]+d2[x1][y1]+a[x][y]+a[x1][y1])就是当a[x][y]和a[x1][y1]不是-1的时候
就是这样
#include<iostream> #include<algorithm> #include<queue> using namespace std; typedef long long ll; const int maxn=3e3+100; int n,m,k; int a[maxn][maxn]; ll d1[maxn][maxn]; ll d2[maxn][maxn]; int vis[maxn][maxn]; int dx[]={0,0,1,-1}; int dy[]={-1,1,0,0}; struct node{ int x,y; }; void bfs1(int x,int y){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ vis[i][j]=0; d1[i][j]=1e18; } } queue<node>q; q.push({x,y}); vis[x][y]=1; d1[x][y]=0; node now; while(!q.empty()){ now=q.front(); q.pop(); for(int i=0;i<4;i++){ int xx=now.x+dx[i]; int yy=now.y+dy[i]; if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy]||a[xx][yy]==-1){ continue; } vis[xx][yy]=1; d1[xx][yy]=d1[now.x][now.y]+k; q.push({xx,yy}); } } } void bfs2(int x,int y){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ vis[i][j]=0; d2[i][j]=1e18; } } queue<node>q; q.push({x,y}); vis[x][y]=1; d2[x][y]=0; node now; while(!q.empty()){ now=q.front(); q.pop(); for(int i=0;i<4;i++){ int xx=now.x+dx[i]; int yy=now.y+dy[i]; if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy]||a[xx][yy]==-1){ continue; } vis[xx][yy]=1; d2[xx][yy]=d2[now.x][now.y]+k; q.push({xx,yy}); } } } int main(){ cin>>n>>m>>k; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d",&a[i][j]); } } bfs1(1,1); bfs2(n,m); ll mi1=1e18,mi2=1e18; ll ans=d1[n][m]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i][j]>0){ mi1=min(mi1,d1[i][j]+a[i][j]); mi2=min(mi2,d2[i][j]+a[i][j]); } } } ans=min(ans,mi1+mi2); if(ans==1e18){ ans=-1; } cout<<ans<<endl; }