結果

問題 No.2673 A present from B
ユーザー tnakao0123tnakao0123
提出日時 2024-04-26 11:35:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,394 bytes
コンパイル時間 774 ms
コンパイル使用メモリ 60,208 KB
実行使用メモリ 22,420 KB
最終ジャッジ日時 2024-04-26 11:35:30
合計ジャッジ時間 2,593 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,728 KB
testcase_01 AC 5 ms
9,520 KB
testcase_02 AC 5 ms
9,696 KB
testcase_03 AC 5 ms
9,620 KB
testcase_04 AC 5 ms
9,856 KB
testcase_05 AC 5 ms
9,804 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 5 ms
9,728 KB
testcase_10 WA -
testcase_11 AC 5 ms
9,728 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 5 ms
9,856 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 4 ms
9,856 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 4 ms
9,856 KB
testcase_26 AC 5 ms
9,700 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:61:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   61 |     auto [ud, u] = q.top(); q.pop();
      |          ^
main.cpp:65:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   65 |     for (auto [v, w]: nbrs[u]) {
      |               ^

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2673.cc:  No.2673 A present from B - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 500;
const int MAX_M = 500;
const int MAX_GN = MAX_N * MAX_M;
const int INF = 1 << 30;

/* typedef */

typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* global variables */

int as[MAX_M], ds[MAX_GN];;
vpii nbrs[MAX_GN];

/* subroutines */

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) scanf("%d", as + i), as[i]--;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j + 1 < n; j++) {
      int u = i * n + j, v = u + 1;
      int w = (as[i] == j) ? 0 : 1;
      nbrs[u].push_back({v, w});
      nbrs[v].push_back({u, w});
    }
    if (i > 0)
      for (int j = 0; j < n; j++) {
	int u = i * n + j, v = u - n;
	nbrs[u].push_back({v, 0});
      }
  }

  int gn = n * m, st = (m - 1) * n;
  fill(ds, ds + gn, INF);
  ds[st] = 0;
  priority_queue<pii> q;
  q.push({0, st});

  while (! q.empty()) {
    auto [ud, u] = q.top(); q.pop();
    ud = -ud;
    if (ds[u] != ud) continue;

    for (auto [v, w]: nbrs[u]) {
      int vd = ud + w;
      if (ds[v] > vd) {
	ds[v] = vd;
	q.push({-vd, v});
      }
    }
  }

  for (int j = 1; j < n; j++)
    printf("%d%c", ds[j], (j + 1 < n) ? ' ' : '\n');

  return 0;
}
0