SCU-4498
Given a grid chart, where there are some unreachable points and some time reset devices
Runningphoton from the beginning, with a time bomb on his body, and when it's 0 o'clock, he'll die.
But it can reset the time by hitting the time reset device before 0.
Ask if Runningphoton can reach the finish line
If you can, then the output of the shortest time, if not, then output "Poor Runningphoton"
Although the map is 600*600, there are no more than 150 reset devices
Ordinary BFS will explode, because you have to save every device has been used
The correct solution is as follows:
Because we only care about resetting the device, the starting point, the end point position.
So with each reset device and start point, the end point as the beginning all run once BFS,
Calculate the shortest distance between 22 and construct a graph, and connect the distance less than K
And then run it one time from the beginning SPFA.
But this data seems to be very water, with ordinary bfs also fascinated by the
#pragma COMMENT (linker, "/stack:102400000,102400000")#include <cstdio>#include <iostream>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <map>#include <set>#include <queue>using namespace STD;typedefpair<int,int> Pii;typedef Long LongLL;typedef unsigned Long LongULL;typedef DoubleDBL;typedef Long DoubleLdbl;#define MST (A, B) memset (A,b,sizeof (a) )#define CLR (a) MST (a,0)#define SQR (a) (a*a)Const intmaxn= -+Ten, inf=0x3f3f3f3f;Const intdir[4][2]={0,1,0,-1,1,0,-1,0};structdata{intX,y,t; DataintTxintTyintTT): X (TX), Y (ty), T (TT) {}};structedge{intU,V,W,NXT;};intn,m,k,ncnt;CharMAP[MAXN][MAXN];intVIS[MAXN][MAXN];intposi[ $][2];intEDN,LAST[MAXN]; Edge edge[ the* the+Ten];intdist[ $];BOOLinq[ $];voidAdde (int,int,int);voidInitint,int,int);intSPFA (int,int);intMain () {intTscanf("%d", &t); for(intck=1; ck<=t; ck++) {edn=0; MST (last,-1); Ncnt=0;intSx,sy,tx,ty;scanf("%d%d%d", &n, &m, &k); for(intI=1; i<=n; i++) {scanf('%s ', map[i]+1); for(intj=1; j<=m; J + +) {if(map[i][j]==' R ') {ncnt++;p osi[ncnt][0]=i;posi[ncnt][1]=j;}if(map[i][j]==' S ') {sx=i;sy=j;}if(map[i][j]==' E ') {tx=i;ty=j;} }} ncnt++; posi[0][0]=SX; posi[0][1]=sy; posi[ncnt][0]=TX; posi[ncnt][1]=ty; for(intI=0; i<=ncnt; i++) init (i, posi[i][0], posi[i][1]);//for (int i=0; i<=ncnt; i++) printf ("%d:%d%d\n", I, posi[i][0], posi[i][1]);//for (int i=0; i<edn; i++) printf ("%d,%d:%d\n", edge[i].u, EDGE[I].V, EDGE[I].W); intANS=SPFA (0, ncnt);if(Ans<inf)printf("%d\n", ans);Else puts("Poor runningphoton!"); }return 0;}voidInitintNpintSxintSY) {MST (Vis,0x3f); queue<data>Que Que.push (Data (SX, SY,0)); vis[sx][sy]=0; while(Que.size ()) {Data &u=que.front (); for(intD=0; d<4; d++) {intx=u.x+dir[d][0];inty=u.y+dir[d][1];if(x<=0|| y<=0|| x>n| | y>m| | u.t+1>=vis[x][y]| | map[x][y]==' W ')Continue; Que.push (Data (x,y,u.t+1)); vis[x][y]=u.t+1; } que.pop (); } for(intI=0; i<=ncnt; i++) {intx=posi[i][0], y=posi[i][1];if(i==np| | VIS[X][Y]>=K)Continue; Adde (Np,i,vis[x][y]); }}voidAdde (intUintVintW) {edge[edn].u=u; Edge[edn].v=v; Edge[edn].w=w; Edge[edn].nxt=last[u]; last[u]=edn++;}intSPFA (intSintT) {MST (dist,0x3f); CLR (INQ); Queue<int>Que Que.push (S); dist[s]=0; inq[s]=1; while(Que.size ()) {intU=que.front (); for(intE=last[u]; ~e; E=EDGE[E].NXT) {intV=EDGE[E].V, W=EDGE[E].W;if(DIST[V]>DIST[U]+W) {dist[v]=dist[u]+w;if(!inq[v]) {Que.push (v); inq[v]=1; }}} inq[u]=0; Que.pop (); }returnDIST[T];}
[SCU 4498] Runningphoton ' s Nightmare (BFS pretreatment +SPFA)