結果

問題 No.2673 A present from B
ユーザー tnakao0123tnakao0123
提出日時 2024-04-26 12:10:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,612 bytes
コンパイル時間 733 ms
コンパイル使用メモリ 62,748 KB
実行使用メモリ 37,056 KB
最終ジャッジ日時 2024-04-26 12:11:02
合計ジャッジ時間 2,424 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
15,488 KB
testcase_01 AC 7 ms
15,616 KB
testcase_02 AC 7 ms
15,616 KB
testcase_03 AC 7 ms
15,616 KB
testcase_04 AC 7 ms
15,704 KB
testcase_05 AC 7 ms
15,616 KB
testcase_06 AC 111 ms
36,792 KB
testcase_07 AC 109 ms
37,056 KB
testcase_08 AC 137 ms
36,992 KB
testcase_09 AC 7 ms
15,488 KB
testcase_10 AC 7 ms
15,488 KB
testcase_11 AC 7 ms
15,488 KB
testcase_12 AC 29 ms
21,632 KB
testcase_13 AC 32 ms
21,632 KB
testcase_14 AC 47 ms
25,596 KB
testcase_15 AC 19 ms
18,816 KB
testcase_16 AC 19 ms
18,944 KB
testcase_17 AC 14 ms
17,664 KB
testcase_18 AC 21 ms
19,200 KB
testcase_19 AC 7 ms
15,616 KB
testcase_20 AC 36 ms
23,808 KB
testcase_21 AC 84 ms
32,256 KB
testcase_22 AC 7 ms
15,744 KB
testcase_23 AC 6 ms
15,444 KB
testcase_24 AC 7 ms
15,616 KB
testcase_25 AC 7 ms
15,616 KB
testcase_26 AC 7 ms
15,488 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:74:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   74 |     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_H = MAX_M * 2 + 1;
const int MAX_GN = MAX_H * MAX_N;
const int INF = 1 << 30;

/* typedef */

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

/* 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]--;

  int h = 2 * m + 1;
  for (int i = 0; i < h; i++) {
    if (! (i & 1))
      for (int j = 0; j + 1 < n; j++) {
	int u = i * n + j, v = u + 1;
	nbrs[u].push_back({v, 1});
	nbrs[v].push_back({u, 1});
      }

    if (i > 0)
      for (int j = 0; j < n; j++) {
	int u = i * n + j, v = u - n;
	if ((i & 1) && as[i / 2] == j)
	  nbrs[u].push_back({v + 1, 0});
	else if ((i & 1) && as[i / 2] == j - 1)
	  nbrs[u].push_back({v - 1, 0});
	else
	  nbrs[u].push_back({v, 0});
      }
  }

  int gn = h * n, st = (h - 1) * n + 0;
  fill(ds, ds + gn, INF);
  ds[st] = 0;

  qi q0, q1;
  q0.push(st);

  while (! q0.empty() || ! q1.empty()) {
    if (q0.empty()) swap(q0, q1);
    int u = q0.front(); q0.pop();

    for (auto [v, w]: nbrs[u]) {
      int vd = ds[u] + w;
      if (ds[v] > vd) {
	ds[v] = vd;
	if (w == 0) q0.push(v);
	else q1.push(v);
      }
    }
  }

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

  return 0;
}
0