結果
問題 | No.340 雪の足跡 |
ユーザー | n_knuu |
提出日時 | 2016-02-07 10:37:05 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 875 ms / 1,000 ms |
コード長 | 2,870 bytes |
コンパイル時間 | 1,802 ms |
コンパイル使用メモリ | 173,196 KB |
実行使用メモリ | 224,252 KB |
最終ジャッジ日時 | 2024-09-21 21:28:13 |
合計ジャッジ時間 | 12,384 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 3 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 4 ms
5,376 KB |
testcase_14 | AC | 51 ms
19,456 KB |
testcase_15 | AC | 55 ms
20,352 KB |
testcase_16 | AC | 27 ms
11,008 KB |
testcase_17 | AC | 66 ms
22,784 KB |
testcase_18 | AC | 62 ms
23,040 KB |
testcase_19 | AC | 65 ms
22,912 KB |
testcase_20 | AC | 243 ms
100,224 KB |
testcase_21 | AC | 96 ms
5,376 KB |
testcase_22 | AC | 47 ms
81,424 KB |
testcase_23 | AC | 402 ms
175,096 KB |
testcase_24 | AC | 160 ms
82,200 KB |
testcase_25 | AC | 319 ms
125,256 KB |
testcase_26 | AC | 29 ms
5,888 KB |
testcase_27 | AC | 7 ms
5,376 KB |
testcase_28 | AC | 44 ms
13,312 KB |
testcase_29 | AC | 553 ms
132,300 KB |
testcase_30 | AC | 410 ms
101,388 KB |
testcase_31 | AC | 875 ms
206,208 KB |
testcase_32 | AC | 872 ms
224,024 KB |
testcase_33 | AC | 816 ms
224,060 KB |
testcase_34 | AC | 866 ms
224,152 KB |
testcase_35 | AC | 784 ms
224,160 KB |
testcase_36 | AC | 784 ms
224,252 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:84:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 84 | scanf("%d%d%d", &W, &H, &N); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~ 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:90:19: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 90 | int b; scanf("%d", &b); | ~~~~~^~~~~~~~~~
ソースコード
#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; } }; struct Graph { int V; vector<vector<Edge>> E; Graph(int V) : V(V) { E.resize(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 } }; struct ShortestPath { const Graph g; int start; vector<int> dist; ShortestPath(const Graph g, int start) : g(g), start(start) { } void dijkstra(int goal) { std::priority_queue<Node> que; dist.resize(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 (v == goal) return ; if (dist[v] < cost) continue; for (Edge e : g.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 main() { int W, H, N; scanf("%d%d%d", &W, &H, &N); vector<int> imos_w(H*W, 0), imos_h(H*W, 0); 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, W*H-1) imos_h[i+1] += imos_h[i], imos_w[i+1] += imos_w[i]; Graph g(H*W); REP(i, H*W) { if (imos_w[i]) g.add_edge(i, i+1, 1), g.add_edge(i+1, i, 1); if (imos_h[i]) { int p1 = (i%H)*W+i/H, p2 = p1 + W; g.add_edge(p1, p2, 1), g.add_edge(p2, p1, 1); } } /*REP(i, W*H) { cout << i << ':'; for (auto v : g.E[i]) cout << v.dst << ' '; cout << endl; }*/ ShortestPath sp(g, 0); sp.dijkstra(W*H-1); if (sp.dist[W*H-1] == INF) { printf("Odekakedekinai..\n"); } else { printf("%d\n", sp.dist[W*H-1]); } //REP(i, g.V) cout << sp.dist[i] << (i == g.V-1 ? '\n' : ' '); return 0; }