1295: [SCOI2009] Maximum distance time limit:10 Sec Memory limit:162 MB
submit:1018 solved:538
[Submit] [Status] [Discuss] Descriptionwindy has a rectangular land, is divided into n*m block 1*1 small lattice. Some grids contain obstructions. If you can walk from lattice A to lattice B, then the distance of two squares is the Euclidean distance from the center of the two lattice. If from lattice a can not go to lattice B, there is no distance. If lattice x and lattice y have common edges, and x and y do not contain obstructions, you can go from X to Y. If the windy can remove the T block obstacle, the maximum distance between all the lattice is obtained. Ensure that there is at least one lattice that does not contain obstructions after removing the T block barrier. Input file maxlength.in The first line contains three integers, N M T. Next there are n lines, each line a string of length m, ' 0 ' for the empty lattice, ' 1 ' indicates that the lattice contains obstructions. The output file Maxlength.out contains a floating-point number that retains 6 decimal places. Sample Input"Input Sample One"
3 3 0
001
001
110
"Input Sample Two"
4 3 0
001
001
011
000
"Input Sample Three"
3 3 1
001
001
001
Sample Output"Output Example One"
1.414214
"Output Example II"
3.605551
"Output Example three"
2.828427
HINT
20% of the data, meet 1 <= n,m <= 0 <= T <= 0.
40% of the data, meet 1 <= n,m <= 0 <= T <= 2.
100% data, meet 1 <= n,m <= 0 <= T <= 30
Violence SPFA ... Start with each point and do a SPFA to find out the minimum number of obstacles that must elapse between any two points to reach each other. Then determine if it is less than or equal to T, otherwise it will not be OK. Then the maximum distance of the feasible scheme is calculated.
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#include<vector>#defineMOD 49999#defineINF 100000007using namespacestd;Doubleans;Chars[ to][ to];BOOLv[909];intdx[5]={0,-1,0,1,0};intdy[5]={0,0,-1,0,1};intn,m,t,tot,edge,num[ to][ to],map[909][909],dis[909],q[50000],next[50000],head[909],list[50000],key[50000];voidInsertintXintYintz) {next[++edge]=Head[x]; HEAD[X]=Edge; List[edge]=y; Key[edge]=Z;}voidbuild () {intXx,yy; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) for(intk=1; k<=4; k++) {xx=i+Dx[k]; YY=j+Dy[k]; if(xx<1|| xx>n| | yy<1|| YY>M)Continue; if(s[xx][yy-1]=='1') Insert (Num[i][j],num[xx][yy],1); ElseInsert (Num[i][j],num[xx][yy],0); }}voidSPFA (intSxintSy) { for(intI=1; i<=tot;i++) dis[i]=INF; memset (V,0,sizeof(v)); V[num[sx][sy]]=1; Dis[num[sx][sy]]=0; intx,t=0, w=1; q[1]=Num[sx][sy]; while(t!=W) {T= (t+1)%MOD; X=Q[t]; for(intI=head[x];i;i=Next[i])if(dis[x]+key[i]<Dis[list[i]]) {Dis[list[i]]=dis[x]+Key[i]; if(!V[list[i]]) {W= (w+1)%MOD; Q[W]=List[i]; V[list[i]]=1; }} V[x]=0; } for(intI=1; i<=tot;i++) map[num[sx][sy]][i]=dis[i];} DoubleCalcintXinty) { intx1,x2,y1,y2,sum; X1= (X-1)/m+1; y1= (x1)%m+1; X2= (y1)/m+1; Y2= (y1)%m+1; Sum= (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1); returnsqrtDouble(sum));}intMain () {scanf ("%d%d%d",&n,&m,&t); for(intI=1; i<=n;i++) scanf ("%s", S[i]); Tot=0; Edge=0; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) Num[i][j]=++tot; Build (); for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) SPFA (I,J); for(intI=1; i<=tot;i++) for(intj=1; j<=tot;j++) { intx1= (I-1)/m+1, y1= (i-1)%m+1; if(Map[i][j]<=t-(s[x1][y1-1]=='1')) { Doublex=Calc (i,j); Ans=Max (ans,x); }} printf ("%.6LF", ans); return 0;}
Direct all output on TYVJ-0.00000 ... 0msWA ... Eight in A.
[SCOI2009] [BZOJ1295] the longest distance