#line 1 "a.cpp" #include #include using namespace std; #line 1 "/home/kotatsugame/library/graph/MCF.cpp" //Minimum Cost Flow O(FE log V) #include #include #include #line 6 "/home/kotatsugame/library/graph/MCF.cpp" #include template struct MCF{ struct edge{ int to,rev,cap; T cost; }; vector >G; vectorh,d; vectorpv,pe; MCF(int n_=0):G(n_),h(n_,0),d(n_),pv(n_),pe(n_){} void add_edge(int from,int to,int cap,T cost) { G[from].push_back({to,(int)G[to].size(),cap,cost}); G[to].push_back({from,(int)G[from].size()-1,0,-cost}); } T min_cost_flow(int s,int t,int f)//ans or -1 { T ret=0; while(f>0) { priority_queue,vector >,greater > >P; fill(d.begin(),d.end(),numeric_limits::max()); d[s]=0; P.push(make_pair(0,s)); while(!P.empty()) { pairp=P.top();P.pop(); if(d[p.second]0&&d[e.to]>d[p.second]+e.cost+h[p.second]-h[e.to]) { d[e.to]=d[p.second]+e.cost+h[p.second]-h[e.to]; pv[e.to]=p.second; pe[e.to]=i; P.push(make_pair(d[e.to],e.to)); } } } if(d[t]==numeric_limits::max())return -1; for(int u=0;u>H>>W>>sx>>sy>>gx>>gy; for(int i=0;i>s[i]; for(int i=0;i >P; P.push(make_pair(sx,sy)); while(!P.empty()) { int x=P.front().first,y=P.front().second; P.pop(); for(int r=0;r<4;r++) { int tx=x+d[r],ty=y+d[r+1]; if(s[tx][ty]=='#'||dist[tx][ty]<=dist[x][y]+1)continue; dist[tx][ty]=dist[x][y]+1; P.push(make_pair(tx,ty)); } } if(dist[gx][gy]==(int)1e9) { cout<<-1<=3)fn=true; x=nx; y=ny; } if(two) { cout<F(H*W); for(int i=0;i=3)ans=min(ans,dist[gx][gy]*2+4+dist[i][j]*4); } for(int i=0;i=3)ans=min(ans,dist[sx][sy]*2+4+dist[i][j]*4); } cout<<(ans<(int)1e9?ans:-1)<