提出 #21333042


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;

struct Edge {
  int to, c;
  Edge(int to, int c): to(to), c(c) {}
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<Edge>> g(n);
  rep(i,m) {
    int a, b; char c;
    cin >> a >> b >> c;
    --a; --b;
    g[a].emplace_back(b, c-'a');
    g[b].emplace_back(a, c-'a');
  }
  const int INF = 1001001001;
  vector<vector<int>> dp(n, vector<int>(n, INF));
  queue<P> q;
  auto push = [&](int a, int b, int x) {
    if (dp[a][b] != INF) return;
    dp[a][b] = x;
    q.emplace(a,b);
  };
  push(0,n-1,0);
  while (!q.empty()) {
    auto [a, b] = q.front(); q.pop();
    for (auto ea : g[a]) for (auto eb : g[b]) {
      if (ea.c != eb.c) continue;
      push(ea.to, eb.to, dp[a][b]+1);
    }
  }
  int ans = INF;
  rep(i,n) {
    ans = min(ans, dp[i][i]*2);
    for (auto e : g[i]) {
      ans = min(ans, dp[i][e.to]*2+1);
    }
  }
  if (ans == INF) ans = -1;
  cout << ans << endl;
  return 0;
}

提出情報

提出日時
問題 F - Construct a Palindrome
ユーザ snuke
言語 C++ (GCC 9.2.1)
得点 600
コード長 1143 Byte
結果 AC
実行時間 60 ms
メモリ 15336 KiB

ジャッジ結果

セット名 Sample All after_contest
得点 / 配点 0 / 0 600 / 600 0 / 0
結果
AC × 3
AC × 46
AC × 1
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All big_answer_00.txt, extreme_00.txt, extreme_01.txt, extreme_02.txt, handmade_00.txt, handmade_01.txt, handmade_02.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_constructive_00.txt, random_constructive_01.txt, random_constructive_02.txt, random_constructive_03.txt, random_constructive_04.txt, random_constructive_05.txt, random_constructive_06.txt, random_constructive_07.txt, random_constructive_08.txt, random_constructive_09.txt, random_constructive_10.txt, random_constructive_11.txt, random_constructive_12.txt, random_constructive_13.txt, random_constructive_14.txt, random_constructive_15.txt, sample_01.txt, sample_02.txt, sample_03.txt, tester2_00.txt, tester2_01.txt, tester3_00.txt, tester_00.txt
after_contest after_contest_00.txt
ケース名 結果 実行時間 メモリ
after_contest_00.txt AC 9 ms 3540 KiB
big_answer_00.txt AC 11 ms 7512 KiB
extreme_00.txt AC 6 ms 4564 KiB
extreme_01.txt AC 16 ms 4504 KiB
extreme_02.txt AC 33 ms 15336 KiB
handmade_00.txt AC 2 ms 3536 KiB
handmade_01.txt AC 2 ms 3536 KiB
handmade_02.txt AC 2 ms 3536 KiB
random_00.txt AC 8 ms 7676 KiB
random_01.txt AC 10 ms 7600 KiB
random_02.txt AC 8 ms 7652 KiB
random_03.txt AC 9 ms 7544 KiB
random_04.txt AC 17 ms 3988 KiB
random_05.txt AC 11 ms 3580 KiB
random_06.txt AC 16 ms 4000 KiB
random_07.txt AC 15 ms 4448 KiB
random_08.txt AC 2 ms 3588 KiB
random_09.txt AC 6 ms 6168 KiB
random_10.txt AC 12 ms 3712 KiB
random_11.txt AC 6 ms 6332 KiB
random_12.txt AC 6 ms 5224 KiB
random_13.txt AC 9 ms 5844 KiB
random_14.txt AC 7 ms 7476 KiB
random_15.txt AC 10 ms 3800 KiB
random_constructive_00.txt AC 8 ms 7560 KiB
random_constructive_01.txt AC 8 ms 7644 KiB
random_constructive_02.txt AC 7 ms 7460 KiB
random_constructive_03.txt AC 9 ms 7676 KiB
random_constructive_04.txt AC 7 ms 5768 KiB
random_constructive_05.txt AC 12 ms 4476 KiB
random_constructive_06.txt AC 6 ms 5296 KiB
random_constructive_07.txt AC 11 ms 6264 KiB
random_constructive_08.txt AC 5 ms 3492 KiB
random_constructive_09.txt AC 12 ms 3840 KiB
random_constructive_10.txt AC 7 ms 5192 KiB
random_constructive_11.txt AC 16 ms 4064 KiB
random_constructive_12.txt AC 5 ms 3504 KiB
random_constructive_13.txt AC 8 ms 6036 KiB
random_constructive_14.txt AC 7 ms 4124 KiB
random_constructive_15.txt AC 13 ms 3720 KiB
sample_01.txt AC 2 ms 3644 KiB
sample_02.txt AC 2 ms 3536 KiB
sample_03.txt AC 3 ms 3476 KiB
tester2_00.txt AC 60 ms 8256 KiB
tester2_01.txt AC 55 ms 8108 KiB
tester3_00.txt AC 16 ms 6252 KiB
tester_00.txt AC 38 ms 7536 KiB