結果

問題 No.2673 A present from B
ユーザー tnakao0123
提出日時 2024-04-26 11:59:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,606 bytes
コンパイル時間 588 ms
コンパイル使用メモリ 64,368 KB
最終ジャッジ日時 2025-02-21 09:05:58
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:39:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   39 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:40:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   40 |   for (int i = 0; i < m; i++) scanf("%d", as + i), as[i]--;
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

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});
      }
    }
    else {
      int u = i * n + as[i / 2], v = u + 1;
      nbrs[u].push_back({v, 0});
      nbrs[v].push_back({u, 0});
    }

    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 = 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