Topic Links:
http://www.lydsy.com/JudgeOnline/problem.php?id=1295
Exercises
For each point of violence ran BFS, see can reach the farthest position, here if there is an obstacle distance of 1, if there is no obstacle, the distance is 0, with BFS running distance <=t All points and update the answer.
Code:
#include <iostream>#include<cstring>#include<cstdio>#include<utility>#include<queue>#include<cmath>using namespacestd;DoubleGet_dis (intXintYintAintb) { returnsqrt1.0* (x-a) * (X-A) +1.0* (y-b) * (Y-b)); }structnode{intx,y,d; Node (intXintYintd): X (x), Y (y), d (d) {} Node () {}BOOL operator< (Constnode& tmp)Const{ returnD>TMP.D; }};Const intmaxn= -;intMP[MAXN][MAXN];intn,m,t;CharSTR[MAXN];Doubleans;intD[MAXN][MAXN],VIS[MAXN][MAXN];Const intdx[]={-1,1,0,0};Const intdy[]={0,0,-1,1};voidBFsintSxintSyintt) { if(mp[sx][sy]==1) t--; if(t<0)return; memset (d,0x7f,sizeof(d)); memset (Vis,0,sizeof(VIS)); Priority_queue<Node>Q; Q.push (Node (Sx,sy,0)), d[sx][sy]=0, vis[sx][sy]=1; while(!Q.empty ()) { intX=q.top (). x,y=q.top (). Y; Ans=Max (Ans,get_dis (sx,sy,x,y)); Q.pop (); for(intI=0;i<4; i++){ inta=x+dx[i],b=y+Dy[i]; if(a<0|| a>=n| | b<0|| B>=M)Continue; if(vis[a][b]| | MP[A][B]+D[X][Y]>T)Continue; D[A][B]=d[x][y]+mp[a][b],vis[a][b]=1, Q.push (Node (a,b,d[a][b)); } } }intMain () { while(SCANF ("%d%d%d", &n,&m,&t) = =3){ for(intI=0; i<n;i++) {scanf ("%s", str); for(intj=0; j<m;j++) {Mp[i][j]=str[j]-'0'; }} ans=-1; for(intI=0; i<n;i++){ for(intj=0; j<m;j++) {BFS (i,j,t); }} printf ("%.6lf\n", ans); } return 0;}
Bzoj 1295: [SCOI2009] longest distance violence +bfs shortest