結果

問題 No.340 雪の足跡
ユーザー n_knuun_knuu
提出日時 2016-02-07 15:12:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 787 ms / 1,000 ms
コード長 3,519 bytes
コンパイル時間 1,465 ms
コンパイル使用メモリ 168,580 KB
実行使用メモリ 93,256 KB
最終ジャッジ日時 2023-10-21 20:06:33
合計ジャッジ時間 9,826 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
34,020 KB
testcase_01 AC 12 ms
34,024 KB
testcase_02 AC 12 ms
29,928 KB
testcase_03 AC 12 ms
34,020 KB
testcase_04 AC 13 ms
34,024 KB
testcase_05 AC 12 ms
29,924 KB
testcase_06 AC 13 ms
34,024 KB
testcase_07 AC 12 ms
31,976 KB
testcase_08 AC 12 ms
34,024 KB
testcase_09 AC 12 ms
34,020 KB
testcase_10 AC 13 ms
34,024 KB
testcase_11 AC 13 ms
34,084 KB
testcase_12 AC 13 ms
34,212 KB
testcase_13 AC 14 ms
34,224 KB
testcase_14 AC 47 ms
37,932 KB
testcase_15 AC 52 ms
38,132 KB
testcase_16 AC 32 ms
35,932 KB
testcase_17 AC 61 ms
38,784 KB
testcase_18 AC 58 ms
38,784 KB
testcase_19 AC 60 ms
38,784 KB
testcase_20 AC 141 ms
43,444 KB
testcase_21 AC 107 ms
34,020 KB
testcase_22 AC 28 ms
39,784 KB
testcase_23 AC 146 ms
42,944 KB
testcase_24 AC 137 ms
42,884 KB
testcase_25 AC 167 ms
45,976 KB
testcase_26 AC 37 ms
34,652 KB
testcase_27 AC 17 ms
34,040 KB
testcase_28 AC 46 ms
36,500 KB
testcase_29 AC 506 ms
72,120 KB
testcase_30 AC 338 ms
64,596 KB
testcase_31 AC 681 ms
88,108 KB
testcase_32 AC 777 ms
93,256 KB
testcase_33 AC 720 ms
93,256 KB
testcase_34 AC 787 ms
93,256 KB
testcase_35 AC 657 ms
93,256 KB
testcase_36 AC 642 ms
93,256 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:85:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   85 |   scanf("%d%d%d", &W, &H, &N); V = W*H;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:88:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   88 |     int M, prev; scanf("%d%d", &M, &prev);
      |                  ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:91:19: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   91 |       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 1<<30
#define MAX_V 1000010

// graph by adjacency list
struct Edge {
  int dst, weight;
  Edge(int dst, int weight) :
    dst(dst), weight(weight) { }
  bool operator < (const Edge &e) const {
    return weight > e.weight;
  }
};

vector<Edge> E[1000010];

struct Graph {
  int V;

  Graph(int V) : V(V) { }

  void add_edge(int src, int dst, int weight) {
    E[src].push_back(Edge(dst, weight));
  }
};

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
  }
};

int dist[1000010];

struct ShortestPath {
  const Graph g;
  int start;

  ShortestPath(const Graph g, int start) :
    g(g), start(start) { }

  void dijkstra() {
    std::priority_queue<Node> que;
    fill(dist, dist + g.V, INF);
    dist[start] = 0;
    que.push(Node(start, 0));

    while (!que.empty()) {
      Node n = que.top(); que.pop();
      int v = n.v, cost = n.dist;
      if (dist[v] < cost) continue;
      for (Edge e : E[v]) {
	if (dist[v] + e.weight < dist[e.dst]) {
	  dist[e.dst] = dist[v] + e.weight;
	  que.push(Node(e.dst, dist[e.dst]));
	}
      }
    }
  }

};

int curve[1000010], imos_w[1000010], imos_h[1000010];

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

  REP(i, N) {
    int M, prev; scanf("%d%d", &M, &prev);
    curve[prev] = 1;
    REP(j, M) {
      int b; scanf("%d", &b);
      curve[b] = 1;
      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];

  FOR(i, 1, V-1) {
    int p = (i%W)*H+i/W;
    if ((imos_w[i-1] or imos_w[i]) and (imos_h[p-1] or imos_h[p])) curve[i] = true;
  }
  /*
  REP(i, V) {
    if (curve[i]) cout << i << ' ';
  }
  cout << endl; //*/
  //REP(i, V) cout << curve[i] << (i == V-1 ? '\n' : ' ');


  Graph g(V);
  REP(i, V-1) {
    if (imos_w[i]) {
      int n = i;
      while (imos_w[n] == imos_w[n+1] and not curve[n+1]) n++;
      //cout << i << ' ' << n+1 << endl;
      g.add_edge(i, n+1, n+1-i), g.add_edge(n+1, i, n+1-i);
      i = n;
    }
  }
  REP(i, V-1) {
    if (imos_h[i]) {
      int n = i;
      while (imos_h[n] == imos_h[n+1] and not curve[(n%H)*W+n/H+W]) n++;
      int p1 = (i%H)*W+i/H, p2 = (n%H)*W+n/H;
      //cout << p1 << ' ' << p2+W << endl;
      g.add_edge(p1, p2+W, n+1-i), g.add_edge(p2+W, p1, n+1-i);
      i = n;
    }
  }

  /*
  REP(i, V) {
    if (not g.E[i].size()) continue;
    cout << i << ':';
    for (auto v : g.E[i]) cout << v.dst << ' ';
    cout << endl;
    } // */

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