結果
問題 |
No.1028 闇討ち
|
ユーザー |
|
提出日時 | 2020-04-18 21:50:30 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,295 bytes |
コンパイル時間 | 1,993 ms |
コンパイル使用メモリ | 185,552 KB |
実行使用メモリ | 11,940 KB |
最終ジャッジ日時 | 2024-10-04 00:24:03 |
合計ジャッジ時間 | 7,929 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 9 TLE * 1 -- * 10 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<(int)(n); i++) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; int INFTY = 100100100; vector<vector<int>> A(n, vector<int>(n)); rep(i,n) rep(j,n) { int a; cin >> a; A[i][j] = --a; } vector<int> dx = {-1, 0, 1, -1, 1, -1, 0, 1}; vector<int> dy = {-1, -1, -1, 0, 0, 1, 1, 1}; map<int,int> sp; int ans = 0; rep(i, n) sp[i] = INFTY; int d = INFTY; rep(i,n) { queue<P> q; vector<vector<int>> dist(n, vector<int>(n, INFTY)); q.push(make_pair(i,0)); int cnt = 0; map<int,int> d; dist[i][0] = 0; while (cnt != n*n){ cnt++; P p = q.front(); q.pop(); int x = p.first; int y = p.second; d[A[x][y]] += dist[x][y]; rep(j, 8) { int nx = x + dx[j]; int ny = y + dy[j]; if (0 <= nx && nx < n && 0 <= ny && ny < n) { if (dist[nx][ny] == INFTY) { q.push(make_pair(nx,ny)); dist[nx][ny] = dist[x][y] + 1; } } } } for (auto m : d) { sp[m.first] = min(sp[m.first], m.second); } } for (auto s: sp) { ans += s.second; } cout << ans << endl; }