結果

問題 No.2573 moving up
ユーザー ゆにぽけ
提出日時 2024-02-21 23:40:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 358 ms / 2,000 ms
コード長 1,622 bytes
コンパイル時間 1,659 ms
コンパイル使用メモリ 152,256 KB
最終ジャッジ日時 2025-02-19 18:40:15
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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();
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0