結果

問題 No.1028 闇討ち
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2020-04-17 22:33:34
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,432 bytes
コンパイル時間 1,170 ms
コンパイル使用メモリ 130,048 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-05-07 21:12:18
合計ジャッジ時間 6,265 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 358 ms
5,376 KB
testcase_10 AC 637 ms
5,376 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <numeric>
#include <tuple>
#include <climits>

using namespace std;
using P = pair<int,int>;
using TP = tuple<int,int,int>;

const vector dx = {+0,+1,+1,+0,+1};
const vector dy = {+1,+1,+0,-1,-1};

queue<TP> q;

void bfs(int s, const int& n, const vector<vector<int>>& a, vector<vector<int>>& dist) {
    q.emplace(s,0,0);

    vector<vector<bool>> visited(n, vector<bool> (n,false));
    while(!q.empty()) {
        auto [y,x,c] = q.front(); q.pop();
        if (visited[y][x]) continue;
        visited[y][x] = true;
        dist[s][a[y][x]-1] += c;
        for (int i=0; i<5; i++) {
            int xi = x + dx[i];
            int yi = y + dy[i];
            if (xi<0 || n<=xi) continue;
            if (yi<0 || n<=yi) continue;
            if (visited[yi][xi]) continue;
            q.emplace(yi, xi, c+1);
        }
    }
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> a(n,vector<int>(n,0));
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            scanf("%d",&a[i][j]);
        }
    }

    vector<vector<int>> dist(n, vector<int>(n, 0));
    for (int i=0; i<n; i++) {
        bfs(i,n,a,dist);
    }

    vector<int> ans(n, INT_MAX);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            ans[i] = min(ans[i], dist[j][i]);
        }
    }


    cout << accumulate(ans.begin(), ans.end(), 0LL) << endl;
}
0