結果

問題 No.1323 うしらずSwap
ユーザー kotatsugamekotatsugame
提出日時 2020-12-20 23:39:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 999 ms / 3,000 ms
コード長 3,798 bytes
コンパイル時間 2,022 ms
コンパイル使用メモリ 91,584 KB
実行使用メモリ 151,240 KB
最終ジャッジ日時 2023-08-09 13:59:44
合計ジャッジ時間 13,458 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 51 ms
12,840 KB
testcase_17 AC 59 ms
13,068 KB
testcase_18 AC 50 ms
12,892 KB
testcase_19 AC 60 ms
12,872 KB
testcase_20 AC 49 ms
12,836 KB
testcase_21 AC 349 ms
151,036 KB
testcase_22 AC 876 ms
150,964 KB
testcase_23 AC 350 ms
150,708 KB
testcase_24 AC 999 ms
151,088 KB
testcase_25 AC 353 ms
150,696 KB
testcase_26 AC 324 ms
150,976 KB
testcase_27 AC 329 ms
151,240 KB
testcase_28 AC 56 ms
12,868 KB
testcase_29 AC 311 ms
150,940 KB
testcase_30 AC 483 ms
150,956 KB
testcase_31 AC 303 ms
150,628 KB
testcase_32 AC 544 ms
151,116 KB
testcase_33 AC 103 ms
12,828 KB
testcase_34 AC 92 ms
12,812 KB
testcase_35 AC 91 ms
12,852 KB
testcase_36 AC 90 ms
13,084 KB
testcase_37 AC 86 ms
12,856 KB
testcase_38 AC 76 ms
13,028 KB
testcase_39 AC 82 ms
12,912 KB
testcase_40 AC 82 ms
12,848 KB
testcase_41 AC 82 ms
13,092 KB
testcase_42 AC 76 ms
12,876 KB
testcase_43 AC 44 ms
12,856 KB
testcase_44 AC 50 ms
12,832 KB
testcase_45 AC 50 ms
12,948 KB
testcase_46 AC 53 ms
12,896 KB
testcase_47 AC 46 ms
12,916 KB
testcase_48 AC 230 ms
109,864 KB
testcase_49 AC 422 ms
116,260 KB
testcase_50 AC 226 ms
105,020 KB
testcase_51 AC 85 ms
83,060 KB
testcase_52 AC 143 ms
94,424 KB
testcase_53 AC 445 ms
116,796 KB
testcase_54 AC 153 ms
94,476 KB
testcase_55 AC 339 ms
116,664 KB
testcase_56 AC 111 ms
88,900 KB
testcase_57 AC 584 ms
128,256 KB
testcase_58 AC 3 ms
4,384 KB
testcase_59 AC 6 ms
10,212 KB
testcase_60 AC 3 ms
4,384 KB
testcase_61 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:9:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]

ソースコード

diff #

#line 1 "a.cpp"
#include<iostream>
#include<queue>
using namespace std;
#line 1 "/home/kotatsugame/library/graph/MCF.cpp"
//Minimum Cost Flow O(FE log V)
#include<algorithm>
#include<utility>
#include<vector>
#line 6 "/home/kotatsugame/library/graph/MCF.cpp"
#include<limits>
template<typename T>
struct MCF{
	struct edge{
		int to,rev,cap;
		T cost;
	};
	vector<vector<edge> >G;
	vector<T>h,d;
	vector<int>pv,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<pair<T,int>,vector<pair<T,int> >,greater<pair<T,int> > >P;
			fill(d.begin(),d.end(),numeric_limits<T>::max());
			d[s]=0;
			P.push(make_pair(0,s));
			while(!P.empty())
			{
				pair<T,int>p=P.top();P.pop();
				if(d[p.second]<p.first)continue;
				for(int i=0;i<G[p.second].size();i++)
				{
					edge&e=G[p.second][i];
					if(e.cap>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<T>::max())return -1;
			for(int u=0;u<G.size();u++)h[u]+=d[u];
			int d=f;
			for(int u=t;u!=s;u=pv[u])d=min(d,G[pv[u]][pe[u]].cap);
			f-=d;
			ret+=d*h[t];
			for(int u=t;u!=s;u=pv[u])
			{
				G[pv[u]][pe[u]].cap-=d;
				G[u][G[pv[u]][pe[u]].rev].cap+=d;
			}
		}
		return ret;
	}
};
#line 5 "a.cpp"
int H,W,sx,sy,gx,gy;
string s[1333];
int dist[1333][1333];
int d[5]={0,1,0,-1};
main()
{
	cin>>H>>W>>sx>>sy>>gx>>gy;
	for(int i=0;i<H;i++)cin>>s[i];
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)dist[i][j]=1e9;
	sx--,sy--;
	gx--,gy--;
	dist[sx][sy]=0;
	queue<pair<int,int> >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<<endl;
		return 0;
	}
	bool fn=false,two=false;
	int x=gx,y=gy;
	while(x!=sx||y!=sy)
	{
		int nx=-1,ny=-1;
		int cnt=0;
		for(int r=0;r<4;r++)
		{
			int tx=x+d[r],ty=y+d[r+1];
			if(s[tx][ty]=='#')continue;
			if(dist[tx][ty]==dist[x][y]-1)
			{
				if(nx<0)nx=tx,ny=ty;
				else two=true;
			}
			cnt++;
		}
		if((x!=gx||y!=gy)&&cnt>=3)fn=true;
		x=nx;
		y=ny;
	}
	if(two)
	{
		cout<<dist[gx][gy]*2<<endl;
		return 0;
	}
	else if(fn)
	{
		cout<<dist[gx][gy]*2+2<<endl;
		return 0;
	}
	MCF<int>F(H*W);
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)if(s[i][j]=='.')
	{
		for(int r=0;r<4;r++)
		{
			int tx=i+d[r],ty=j+d[r+1];
			if(s[tx][ty]=='.')F.add_edge(i*W+j,tx*W+ty,1,1);
		}
	}
	int ans=F.min_cost_flow(sx*W+sy,gx*W+gy,2);
	if(ans==-1)ans=1e9;
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)if(dist[i][j]<(int)1e9)
	{
		int cnt=0;
		for(int r=0;r<4;r++)
		{
			int x=i+d[r],y=j+d[r+1];
			if(s[x][y]=='.')cnt++;
		}
		if(cnt>=3)ans=min(ans,dist[gx][gy]*2+4+dist[i][j]*4);
	}
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)dist[i][j]=1e9;
	dist[gx][gy]=0;
	P.push(make_pair(gx,gy));
	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));
		}
	}
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)if(dist[i][j]<(int)1e9)
	{
		int cnt=0;
		for(int r=0;r<4;r++)
		{
			int x=i+d[r],y=j+d[r+1];
			if(s[x][y]=='.')cnt++;
		}
		if(cnt>=3)ans=min(ans,dist[sx][sy]*2+4+dist[i][j]*4);
	}
	cout<<(ans<(int)1e9?ans:-1)<<endl;
}
0