結果
問題 | No.614 壊れたキャンパス |
ユーザー | Pachicobue |
提出日時 | 2017-12-14 01:00:19 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,735 bytes |
コンパイル時間 | 3,121 ms |
コンパイル使用メモリ | 235,560 KB |
実行使用メモリ | 149,300 KB |
最終ジャッジ日時 | 2024-05-08 08:15:18 |
合計ジャッジ時間 | 14,318 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 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 | 967 ms
141,056 KB |
testcase_09 | AC | 1,190 ms
149,300 KB |
testcase_10 | RE | - |
testcase_11 | AC | 1,058 ms
141,052 KB |
testcase_12 | AC | 1,038 ms
141,052 KB |
testcase_13 | AC | 1,019 ms
141,440 KB |
testcase_14 | AC | 944 ms
141,800 KB |
testcase_15 | RE | - |
testcase_16 | AC | 862 ms
140,692 KB |
testcase_17 | AC | 258 ms
114,176 KB |
testcase_18 | AC | 490 ms
129,504 KB |
testcase_19 | AC | 431 ms
127,924 KB |
ソースコード
#include <bits/stdc++.h> #define VARNAME(x) #x #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; using ld = long double; template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "sz:" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template <typename S, typename T> ostream& operator<<(ostream& os, const pair<S, T>& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = (ll)1e9 + 7LL; constexpr ld PI = static_cast<ld>(3.1415926535898); template <typename T> constexpr T INF = numeric_limits<T>::max() / 10; struct CostGraph { using T = ll; CostGraph(const ll v) : V{v} { edge.resize(v); rev_edge.resize(v); } struct Edge { Edge(const ll from, const ll to, const T cost) : from{from}, to{to}, cost{cost} {} const ll from; const ll to; const T cost; bool operator<(const Edge& e) const { return cost != e.cost ? cost < e.cost : to < e.to; } }; void addEdge(const ll from, const ll to, const T cost = 1) { edge[from].push_back(Edge{from, to, cost}); rev_edge[to].push_back(Edge(to, from, cost)); } vector<vector<Edge>> edge; vector<vector<Edge>> rev_edge; const ll V; }; void Dijkstra(const CostGraph& g, const ll s, vector<CostGraph::T>& d) { using T = CostGraph::T; assert(s < g.V); assert(d.size() == g.V); using P = pair<T, ll>; priority_queue<P, vector<P>, greater<P>> q; for (ll i = 0; i < g.V; i++) { d[i] = INF<T>; } d[s] = 0; q.push(make_pair(0, s)); while (not q.empty()) { const P& p = q.top(); const T cost = p.first; const ll v = p.second; q.pop(); if (d[v] < cost) { continue; } for (const auto& e : g.edge[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; q.push(make_pair(d[e.to], e.to)); } } } } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, M, K, S, T; cin >> N >> M >> K >> S >> T; S--, T--; vector<ll> num(N); vector<tuple<ll, ll, ll>> bridge(M); vector<vector<ll>> pos(N); for (ll i = 0; i < M; i++) { ll a, b, c; cin >> a >> b >> c; a--, b--, c--; pos[a].push_back(b); num[a]++; pos[a + 1].push_back(c); num[a + 1]++; bridge[i] = make_tuple(a, b, c); } pos[0].push_back(S); num[0]++; pos[N - 1].push_back(T); num[N - 1]++; vector<map<ll, ll>> mps(N); for (ll i = 0; i < N; i++) { num[i] += (i == 0 ? 0 : num[i - 1]); sort(pos[i].begin(), pos[i].end()); for (ll j = 0; j < pos[i].size(); j++) { mps[i][pos[i][j]] = j; } } CostGraph g(num[N - 1]); for (ll i = 0; i < N; i++) { for (ll j = 0; j < pos[i].size() - 1; j++) { g.addEdge(j + (i == 0 ? 0 : num[i - 1]), j + 1 + (i == 0 ? 0 : num[i - 1]), pos[i][j + 1] - pos[i][j]); g.addEdge(j + 1 + (i == 0 ? 0 : num[i - 1]), j + (i == 0 ? 0 : num[i - 1]), pos[i][j + 1] - pos[i][j]); } } for (ll i = 0; i < M; i++) { ll a, b, c; tie(a, b, c) = bridge[i]; g.addEdge((a == 0 ? 0 : num[a - 1]) + mps[a][b], num[a] + mps[a + 1][c], 0); } const ll s = mps[0][S]; const ll t = mps[N - 1][T] + (N == 1 ? 0 : num[N - 2]); vector<ll> dist(num[N - 1]); Dijkstra(g, s, dist); cout << (dist[t] == INF<ll> ? -1 : dist[t]) << endl; return 0; }