結果
| 問題 |
No.1028 闇討ち
|
| コンテスト | |
| ユーザー |
iqueue02
|
| 提出日時 | 2020-04-17 23:07:37 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,282 bytes |
| コンパイル時間 | 1,945 ms |
| コンパイル使用メモリ | 177,296 KB |
| 実行使用メモリ | 10,496 KB |
| 最終ジャッジ日時 | 2024-10-03 15:02:25 |
| 合計ジャッジ時間 | 6,873 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 9 TLE * 1 -- * 10 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;
constexpr int INF = 2e9;
int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int main() {
int n;
cin >> n;
vector<int> dp(n,INF);
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cin >> a[i][j], a[i][j]--;
}
auto bfs = [&](int sx, int sy){
queue<P> que;
que.push(make_pair(sx, sy));
vector<vector<int>> d(n, vector<int>(n, INF));
vector<int> tmp(n, 0);
d[sx][sy] = 0;
while (!que.empty()) {
auto p = que.front(); que.pop();
tmp[a[p.first][p.second]] += d[p.first][p.second];
for (int i = 0; i < 8; i++) {
int x = p.first + dx[i], y = p.second + dy[i];
if (x < 0 || y < 0 || x >= n || y >= n) continue;
if (d[x][y] != INF) continue;
d[x][y] = d[p.first][p.second] + 1;
que.push(make_pair(x, y));
}
}
for (int i = 0; i < n; i++) dp[i] = min(dp[i], tmp[i]);
};
for (int i = 0; i < n; i++) {
bfs(i, 0);
}
ll res = 0;
for (int i = 0; i < n; i++) {
res += dp[i];
}
cout << res << endl;
return 0;
}
iqueue02