結果
問題 | No.614 壊れたキャンパス |
ユーザー |
|
提出日時 | 2017-12-14 03:00:52 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,232 ms / 2,000 ms |
コード長 | 2,197 bytes |
コンパイル時間 | 1,451 ms |
コンパイル使用メモリ | 123,720 KB |
実行使用メモリ | 65,860 KB |
最終ジャッジ日時 | 2024-12-14 10:58:05 |
合計ジャッジ時間 | 12,834 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#include "iostream"#include "climits"#include "list"#include "queue"#include "stack"#include "set"#include "functional"#include "algorithm"#include "string"#include "map"#include "unordered_map"#include "iomanip"#include "cmath"using namespace std;const long long int MOD = 1000000007;long long int N, M, K, H, W, L, R;int main() {ios::sync_with_stdio(false);cin.tie(0);cin >> N >> M >> K >> L >> R;vector<pair<long long int, long long int>>edge;vector<map<long long int, long long int>>m(N + 1);map<long long int, long long int>dis;for (int i = 0; i < M; i++) {long long int a, b, c;cin >> a >> b >> c;m[a][b]++;m[a + 1][c]++;edge.push_back({ a*MOD + b,(a + 1)*MOD + c });}sort(edge.begin(), edge.end());m[1][L]++;m[N][R]++;for (int i = 1; i <= N; i++) {for (auto j : m[i]) {dis[i*MOD + j.first] = LLONG_MAX;}}dis[MOD + L] = 0;priority_queue<pair<long long int, long long int>, vector<pair<long long int, long long int>>, greater<pair<long long int, long long int>>>PQ;PQ.push({ 0,MOD + L });while (!PQ.empty()) {auto box = PQ.top();PQ.pop();long long int cw = box.second / MOD;long long int ch = box.second %MOD;long long int cc = box.first;if (dis[cw*MOD + ch] < cc) {continue;}auto it = m[cw].find(ch);if (it != m[cw].begin()) {it--;int nh = (*it).first;if (dis[cw*MOD + nh] > cc + abs(nh - ch)) {dis[cw*MOD + nh] = cc + abs(nh - ch);PQ.push({ dis[cw*MOD + nh],cw*MOD + nh });}it++;}it++;if (it != m[cw].end()) {long long int nh = (*it).first;if (dis[cw*MOD + nh] > cc + abs(nh - ch)) {dis[cw*MOD + nh] = cc + abs(nh - ch);PQ.push({ dis[cw*MOD + nh],cw*MOD + nh });}}auto itb = lower_bound(edge.begin(), edge.end(), make_pair(cw*MOD + ch, (long long int)0));auto ite = lower_bound(edge.begin(), edge.end(), make_pair(cw*MOD + ch + 1, (long long int)0));for (; itb != ite; ++itb) {long long int n = (*itb).second;if (dis[n] > cc) {dis[n] = cc;PQ.push({ cc,n });}}}if (dis[N*MOD + R] == LLONG_MAX) {cout << -1 << endl;return 0;}cout << dis[N*MOD + R] << endl;return 0;}