HZOJ go plaid

Author of Positive Solutions:

For 100% of data: operations can be divided into two types:

1. Walking, a cost per unit time to an adjacent grid 4 to the Unicom.

2. Using the portal, a grid in front of a designated direction of the wall, to walk in front of a wall recently, the use of portal delivery. +1 time it takes to reach the nearest time spent in front of the wall.

Two kinds of combination can be composed of any action of action. BFS determined that the nearest wall, the front wall of the first grid of the left and right vertical pretreatment. Then build view of DJ to the shortest run. Complexity is O (MNlog (NM)).

 In fact, that's very clear, just do not know bfs What the hell ...... direct $ n ^ 3 $ violent sweep on the line ah. Indeed nothing to say, look at the code.

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<queue>
  5 #define MP(a,b) make_pair(a,b)
  6 #define ma(x,y) memset(x,y,sizeof(x))
  7 #define LL long long
  8 #define INF 1000000
  9 using namespace std;
 10 struct edge
 11 {
 12     int u,v,w,nxt;
 13     #define u(x) ed[x].u
 14     #define v(x) ed[x].v
 15     #define w(x) ed[x].w
 16     #define n(x) ed[x].nxt
 17 }ed[5000000];
 18 int first[300000],num_e;
 19 #define f(x) first[x]
 20 char map[510][510];
 21 int up[510][510],un[510][510];
 22 int le[510][510],re[510][510];
 23 int n,m;
 24 int cx,cy,fx,fy;
 25 inline int get(int i,int j){return (i-1)*m+j;}
 26 inline pair<int,int> ret(int val){return MP(val/m+1,val%m);}
 27 int dis[300010];
 28 bool v[300000];
 29 void dist(int st)
 30 {
 31     ma(dis,0x7f);
 32     priority_queue<pair<int,int> >q;
 33     dis[st]=0;q.push(MP(0,st));
 34     while(!q.empty())
 35     {
 36         int x=q.top().second;q.pop();
 37         if(v[x])continue;v[x]=1;
 38         for(int i=f(x);i;i=n(i))
 39         if(dis[x]+w(i)<dis[v(i)])
 40             dis[v(i)]=dis[x]+w(i),    
 41             q.push(MP(-dis[v(i)],v(i)));
 42     }
 43 }
 44 inline void add(int u,int v,int w);
 45 signed main()
 46 {
 47     cin>>n>>m;
 48     for(int i=1;i<=n;i++)
 49         scanf("%s",map[i]+1);
 50     for(int i=1;i<=n;i++)
 51         for(int j=1;j<=m;j++)
 52         {
 53             if(map[i][j]=='C')cx=i,cy=j;
 54             if(map[i][j]=='F')fx=i,fy=j;
 55             if(map[i][j]=='#')
 56             up[i][j]=un[i][j]=le[i][j]=re[i][j]=INF;
 57             else
 58             {
 59                 for(int k=i-1;k>0;k--)//
 60                 if(map[k][j]=='#'){up[i][j]=k+1;break;}
 61                 for(int k=i+1;k<=n;k++)//
 62                 if(map[k][j]=='#'){un[i][j]=k-1;break;}
 63                 for(int k=j-1;k>0;k--)//
 64                 if(map[i][k]=='#'){le[i][j]=k+1;break;}
 65                 for(int k=j+1;k<=m;k++)//
 66                 if(map[i][k]=='#'){re[i][j]=k-1;break;}
 67             }
 68         }
 69     for(int i=1;i<=n;i++)            
 70         for(int j=1;j<=m;j++)
 71         if(map[i][j]!='#')
 72         {
 73             if(map[i-1][j]!='#')add(get(i,j),get(i-1,j),1);//,add(get(i-1,j),get(i,j),1);//
 74             if(map[i+1][j]!='#')add(get(i,j),get(i+1,j),1);//,add(get(i+1,j),get(i,j),1);//
 75             if(map[i][j-1]!='#')add(get(i,j),get(i,j-1),1);//,add(get(i,j-1),get(i,j),1);//
 76             if(map[i][j+1]!='#')add(get(i,j),get(i,j+1),1);//,add(get(i,j+1),get(i,j),1);// 77             //
 78             {
 79                 add(get(i,j),get(un[i][j],j),i-up[i][j]+1);//
 80                 add(get(i,j),get(i,le[i][j]),i-up[i][j]+1);//
 81                 add(get(i,j),get(i,re[i][j]),i-up[i][j]+1);//
 82             }
 83             //
 84             {
 85                 add(get(i,j),get(up[i][j],j),un[i][j]-i+1);//
 86                 add(get(i,j),get(i,le[i][j]),un[i][j]-i+1);//
 87                 add(get(i,j),get(i,re[i][j]),un[i][j]-i+1);//
 88             }
 89             //
 90             {
 91                 add(get(i,j),get(up[i][j],j),j-le[i][j]+1);//
 92                 add(get(i,j),get(un[i][j],j),j-le[i][j]+1);//
 93                 add(get(i,j),get(i,re[i][j]),j-le[i][j]+1);//
 94             }
 95             //
 96             {
 97                 add(get(i,j),get(up[i][j],j),re[i][j]-j+1);//
 98                 add(get(i,j),get(un[i][j],j),re[i][j]-j+1);//
 99                 add(get(i,j),get(i,le[i][j]),re[i][j]-j+1);//
100             }
101         }
102     dist(get(cx,cy));
103     printf("%d\n",dis[get(fx,fy)]);
104 }
105 inline void add(int u,int v,int w)
106 {
107     if(u==v)return;
108     ++num_e;
109     u(num_e)=u;
110     v(num_e)=v;
111     w (num_e) = w;
112      n (num_e) = f (u);
113      f (u) = num_e;
114 }
View Code

 

Guess you like

Origin www.cnblogs.com/Al-Ca/p/11318850.html