Four school test push ice cubes

Source: Internet
Author: User

2<=n,m<=10^9,0<=k<=50000.

We find that useful lattices are not many, and after a detailed discussion of the classifications, only these squares are useful:

Four angles, as well as obstacles (or deceleration zone) itself and the upper and lower left and right four directions, as well as the obstacles in the row (and ±1) of the tail two.

Then we just have to sort all the obstacles and deceleration bands by X-Y and y-x, for each useful lattice two points to find left and right will push to where, even the end of the violent BFS can.

#include <iostream>#include<stdio.h>#include<math.h>#include<string.h>#include<time.h>#include<stdlib.h>#include<string>#include<bitset>#include<vector>#include<Set>#include<map>#include<queue>#include<algorithm>#include<sstream>#include<stack>#include<iomanip>using namespacestd;#definePB Push_back#defineMP Make_pairtypedef pair<int,int>Pii;typedefLong LongLl;typedefDoubleld;typedef Vector<int>VI;#defineFi first#defineSe Second#defineFe first#defineFO (x) {freopen (#x ".", "R", stdin), Freopen (#x ". Out", "w", stdout);}#defineEDG int m=0,fst[sz],vb[sz],nxt[sz];void ad_de (int a,int b) {++m;nxt[m]=fst[a];fst[a]=m;vb[m]=b;} void Adde (int a,int b) {Ad_de (A, b); Ad_de (b,a);}#defineEDGC int m=0,fst[sz],vb[sz],nxt[sz],vc[sz];void ad_de (int a,int b,int c) {++m;nxt[m]=fst[a];fst[a]=m;vb[m]=b;vc[m]=c;} void Adde (int a,int b,int c) {ad_de (a,b,c); Ad_de (b,a,c);}#defineES (x,e) (int e=fst[x];e;e=nxt[e])#defineVIZ {printf ("digraph g{\n"); for (int. i=1;i<=n;i++) for ES (i,e) printf ("%d->%d;\n", I,vb[e]); Puts ("}");#ifdef LOCAL#defineTIMER Cerr<<clock () << "ms\n"#else#defineTIMER#endif#defineSZ 1234567intn,m,k,n=0;intXs[sz],ys[sz],ts[sz];typedef pair<int,int>Pii;map&LT;PII,int>STA;BOOLCMP2 (PII a,pii b) {if(a.se!=b.se)returna.se<b.se; returna.fi<b.fi;} PII Ps[sz]; intpn=0;p II Ss[sz],rs[sz];intdx[4]={1,-1,0,0},dy[4]={0,0,-1,1};voidRecintAintb) {    if(Sta[pii (A, b)]==1)return; if(a<1|| b<1|| a>n| | B&GT;M)return; ps[++pn]=PII (A, b);}intLS (PII a) {returnLower_bound (ps+1, ps+1+PN,A)-PS;}ints,t;intQs[sz];intDist[sz];#undefSZ#defineSZ 1234567EDGvoidConn (PII A,pii b) {if(a==b)return; Ad_de (LS (a), LS (b));voidSPFA () {intH=0, t=0; Qs[t++]=s; dist[s]=1;  while(h^t) {intx=qs[h++];  fores (x,e) {intb=Vb[e]; if(Dist[b])Continue; DIST[B]=dist[x]+1; Qs[t++]=C; }    }}intMain () {FO (ICE) scanf ("%d%d%d",&n,&m,&k);  for(intI=1; i<=k;i++) {scanf ("%d%d%d", xs+i,ys+i,ts+i); Sta[pii (Xs[i],ys[i])=ts[i]+1; } rec (1,1);    Rec (N,M); Rec (1, m); Rec (n,1);  for(intI=1; i<=k;i++) {rec (xs[i],1);        Rec (XS[I],M); Rec (1, Ys[i]);        Rec (n,ys[i]); Rec (Xs[i]+1,1); Rec (xs[i]+1, M); Rec (Xs[i]-1,1); Rec (xs[i]-1, M); Rec (1, ys[i]+1); Rec (n,ys[i]+1); Rec (1, ys[i]-1); Rec (n,ys[i]-1);        Rec (xs[i],ys[i]);  for(intj=0;j<4; j + +) Rec (xs[i]+dx[j],ys[i]+Dy[j]); } sort (ps+1, ps+1+PN); PN=unique (ps+1, ps+1+PN)-ps-1; intsn=0, rn=0;  for(intI=1; i<=k;i++)    {        if(ts[i]==0) ss[++sn]=PII (Xs[i],ys[i]); Elsers[++rn]=PII (Xs[i],ys[i]); } ss[++SN]=PII (-2000000000,-2000000000); ss[++SN]=PII (2000000000,2000000000); rs[++RN]=PII (-2000000000,-2000000000); rs[++RN]=PII (2000000000,2000000000); Sort (ss+1, ss+1+SN); Sort (rs+1, rs+1+RN); //handle the left and right first     for(intI=1; i<=pn;i++)    {        intx=ps[i].fi,y=ps[i].se; PII cur=PII (x, y); if(y!=1)//L        {        intmaxn=1; intId=lower_bound (rs+1, rs+1+rn,cur)-rs-1; PII NS=Rs[id]; if(ns.fi==x) maxn=Max (maxn,ns.se); ID=lower_bound (ss+1, ss+1+sn,cur)-ss-1; NS=Ss[id]; if(ns.fi==x) Maxn=max (maxn,ns.se+1);        Conn (Cur,pii (X,MAXN)); }        if(y!=m)//R        {        intminn=m; intId=upper_bound (rs+1, rs+1+rn,cur)-rs; PII NS=Rs[id]; if(ns.fi==x) minn=min (minn,ns.se); ID=upper_bound (ss+1, ss+1+sn,cur)-SS; NS=Ss[id]; if(ns.fi==x) Minn=min (minn,ns.se-1);        Conn (Cur,pii (X,minn)); }} sort (ss+1, ss+1+SN,CMP2); Sort (rs+1, rs+1+RN,CMP2);  for(intI=1; i<=pn;i++)    {        intx=ps[i].fi,y=ps[i].se; PII cur=PII (x, y); if(x!=1)//U        {        intmaxn=1; intId=lower_bound (rs+1, rs+1+RN,CUR,CMP2)-rs-1; PII NS=Rs[id]; if(ns.se==y) maxn=Max (maxn,ns.fi); ID=lower_bound (ss+1, ss+1+SN,CUR,CMP2)-ss-1; NS=Ss[id]; if(ns.se==y) Maxn=max (maxn,ns.fi+1);        Conn (Cur,pii (maxn,y)); }        if(x!=n)//R        {        intminn=N; intId=upper_bound (rs+1, rs+1+RN,CUR,CMP2)-rs; PII NS=Rs[id]; if(ns.se==y) minn=min (minn,ns.fi); ID=upper_bound (ss+1, ss+1+SN,CUR,CMP2)-SS; NS=Ss[id]; if(ns.se==y) Minn=min (minn,ns.fi-1);        Conn (Cur,pii (minn,y)); }} S=ls (PII (1,1)); t=ls (PII (n,m)); SPFA (); cout<<dist[t]-1<<"\ n";}

Four school test push ice cubes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.