結果

問題 No.340 雪の足跡
ユーザー n_knuun_knuu
提出日時 2016-02-07 13:20:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 651 ms / 1,000 ms
コード長 2,368 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 167,072 KB
実行使用メモリ 70,216 KB
最終ジャッジ日時 2023-10-21 20:04:33
合計ジャッジ時間 8,495 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
33,140 KB
testcase_01 AC 12 ms
33,140 KB
testcase_02 AC 12 ms
31,096 KB
testcase_03 AC 12 ms
33,140 KB
testcase_04 AC 12 ms
33,140 KB
testcase_05 AC 11 ms
31,092 KB
testcase_06 AC 12 ms
33,140 KB
testcase_07 AC 12 ms
31,096 KB
testcase_08 AC 12 ms
33,140 KB
testcase_09 AC 11 ms
33,140 KB
testcase_10 AC 12 ms
33,140 KB
testcase_11 AC 13 ms
33,172 KB
testcase_12 AC 12 ms
33,220 KB
testcase_13 AC 13 ms
33,264 KB
testcase_14 AC 35 ms
35,604 KB
testcase_15 AC 39 ms
35,736 KB
testcase_16 AC 26 ms
34,252 KB
testcase_17 AC 45 ms
36,168 KB
testcase_18 AC 43 ms
36,168 KB
testcase_19 AC 44 ms
36,168 KB
testcase_20 AC 196 ms
45,068 KB
testcase_21 AC 106 ms
33,172 KB
testcase_22 AC 21 ms
35,052 KB
testcase_23 AC 260 ms
70,208 KB
testcase_24 AC 135 ms
39,240 KB
testcase_25 AC 226 ms
50,676 KB
testcase_26 AC 36 ms
33,496 KB
testcase_27 AC 17 ms
33,148 KB
testcase_28 AC 39 ms
34,616 KB
testcase_29 AC 441 ms
55,488 KB
testcase_30 AC 300 ms
48,588 KB
testcase_31 AC 651 ms
67,188 KB
testcase_32 AC 541 ms
70,216 KB
testcase_33 AC 488 ms
70,216 KB
testcase_34 AC 555 ms
70,216 KB
testcase_35 AC 448 ms
70,216 KB
testcase_36 AC 446 ms
70,216 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:62:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   62 |   scanf("%d%d%d", &W, &H, &N);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:66:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   66 |     int M, prev; scanf("%d%d", &M, &prev);
      |                  ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:68:19: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |       int b; scanf("%d", &b);
      |              ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
//typedef tuple<int, int, int> T;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)
#define ALL(c) c.begin(), c.end()
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

#define INF 1000010

int dist[1000010];
int imos_w[1000010], imos_h[1000010];
vector<int> E[1000010];
int V;

struct Node {
  int v, dist;
  Node(int v, int dist) :
    v(v), dist(dist) { };
  bool operator < (const Node &n) const {
    return dist > n.dist; // reverse
  }
};

struct ShortestPath {
  int start;

  ShortestPath(int start) : start(start) { }

  inline void dijkstra(int goal) {
    queue<Node> que;
    dist[start] = 0;
    que.push(Node(start, 0));

    while (!que.empty()) {
      Node n = que.front(); que.pop();
      int v = n.v, cost = n.dist;
      if (dist[v] < cost) continue;
      cost++;
      REP(i, E[v].size()) {
	int nv = E[v][i];
	if (nv and not dist[nv]) {
	  dist[nv] = cost;
	  if (nv == goal) return ;
	  que.push(Node(nv, cost));
	}
      }
    }
  }
};

int main() {
  int W, H, N;
  scanf("%d%d%d", &W, &H, &N);
  V = W * H;

  REP(i, N) {
    int M, prev; scanf("%d%d", &M, &prev);
    REP(i, M) {
      int b; scanf("%d", &b);
      int p1 = prev, p2 = b; if (p1 > p2) swap(p1, p2);
      int r1 = p1 / W, c1 = p1 % W, r2 = p2 / W, c2 = p2 % W;
      //cout << '(' << r1 << ',' << c1 << ')' << ' ' << '(' << r2 << ',' << c2 << ')' << endl;
      if (r1 == r2) {
	imos_w[p1]++, imos_w[p2]--;
      } else if (c1 == c2) {
	imos_h[c1 * H + r1]++, imos_h[c2 * H + r2]--;
      }
      prev = b;
    }
  }
  REP(i, V-1) imos_h[i+1] += imos_h[i], imos_w[i+1] += imos_w[i];

  REP(i, V-1) {
    if (imos_w[i]) E[i].push_back(i+1), E[i+1].push_back(i);
    if (imos_h[i]) {
      int p1 = (i%H)*W+i/H, p2 = p1 + W;
      E[p1].push_back(p2), E[p2].push_back(p1);
    }
  }
  /*REP(i, W*H) {
    cout << i << ':';
    for (auto v : g.E[i]) cout << v.dst << ' ';
    cout << endl;
    }*/

  ShortestPath sp(0);
  sp.dijkstra(V-1);
  if (V-1 and dist[V-1] == 0) {
    printf("Odekakedekinai..\n");
  } else {
    printf("%d\n", dist[V-1]);
  }
  //REP(i, V) cout << sp.dist[i] << (i == V-1 ? '\n' : ' ');
  return 0;
}
0