結果

問題 No.2573 moving up
ユーザー ゆにぽけゆにぽけ
提出日時 2024-02-21 23:40:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 391 ms / 2,000 ms
コード長 1,622 bytes
コンパイル時間 2,331 ms
コンパイル使用メモリ 156,876 KB
実行使用メモリ 7,260 KB
最終ジャッジ日時 2024-02-21 23:41:00
合計ジャッジ時間 8,379 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 391 ms
7,260 KB
testcase_02 AC 356 ms
7,260 KB
testcase_03 AC 371 ms
7,260 KB
testcase_04 AC 390 ms
7,260 KB
testcase_05 AC 366 ms
7,260 KB
testcase_06 AC 391 ms
7,260 KB
testcase_07 AC 346 ms
7,124 KB
testcase_08 AC 43 ms
6,676 KB
testcase_09 AC 22 ms
6,676 KB
testcase_10 AC 269 ms
6,676 KB
testcase_11 AC 61 ms
6,676 KB
testcase_12 AC 149 ms
6,676 KB
testcase_13 AC 318 ms
7,084 KB
testcase_14 AC 13 ms
6,676 KB
testcase_15 AC 14 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 28 ms
6,676 KB
testcase_18 AC 155 ms
6,676 KB
testcase_19 AC 237 ms
6,676 KB
testcase_20 AC 19 ms
6,676 KB
testcase_21 AC 25 ms
6,676 KB
testcase_22 AC 28 ms
6,676 KB
testcase_23 AC 39 ms
6,676 KB
testcase_24 AC 102 ms
6,676 KB
testcase_25 AC 174 ms
6,676 KB
testcase_26 AC 26 ms
6,676 KB
testcase_27 AC 8 ms
6,676 KB
testcase_28 AC 8 ms
6,676 KB
testcase_29 AC 8 ms
6,676 KB
testcase_30 AC 8 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
#include <atcoder/mincostflow>
using namespace std;
void Main()
{
	int H,W;
	cin >> H >> W;
	vector<int> X(W),Y(W);
	for(int i = 0;i < W;i++)
	{
		cin >> X[i] >> Y[i];
		X[i]--; Y[i]--;
	}
	atcoder::mcf_graph<int,int> G(2 * W + 2);
	int S = 2 * W,T = S + 1;
	const int dx[6] = {-1,-1,0,0,1,1};
	const int dy[6] = {-1,0,-1,1,0,1};
	for(int s = 0;s < W;s++)
	{
		vector<vector<int>> dp(H);
		for(int i = 0;i < H;i++)
		{
			dp[i].assign(W + i,-1);
		}
		dp[0][s] = 0;
		queue<pair<int,int>> Q;
		Q.push(make_pair(0,s));
		while(!Q.empty())
		{
			auto [i,j] = Q.front();
			Q.pop();
			for(int k = 0;k < 6;k++)
			{
				int ni = i + dx[k];
				int nj = j + dy[k];
				if(ni < 0 || ni >= H || nj < 0 || nj >= dp[ni].size())
				{
					continue;
				}
				if(dp[ni][nj] == -1)
				{
					dp[ni][nj] = dp[i][j] + 1;
					Q.push(make_pair(ni,nj));
				}
			}
		}
		for(int t = 0;t < W;t++)
		{
			G.add_edge(s,t + W,1,dp[X[t]][Y[t]]);
		}
		G.add_edge(S,s,1,0);
		G.add_edge(s + W,T,1,0);
	}
	pair<int,int> mcf = G.flow(S,T,W);
	cout << mcf.second << "\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}

0