結果

問題 No.614 壊れたキャンパス
ユーザー tsutaj
提出日時 2017-12-14 13:38:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,516 ms / 2,000 ms
コード長 4,269 bytes
コンパイル時間 1,976 ms
コンパイル使用メモリ 135,940 KB
実行使用メモリ 66,020 KB
最終ジャッジ日時 2024-12-14 12:40:38
合計ジャッジ時間 11,304 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

//
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
typedef pair<int, int> pii;
typedef long long ll;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;
struct Edge {
int to_idx, to_height;
};
struct Elem {
int flr, cost;
bool operator<(const Elem &x) const {
return cost > x.cost;
}
};
bool exist[200010];
map< int, map<int, vector<Edge> > > edges;
vector<int> in[200010], out[200010];
map<int, int> rec;
vector<int> get_array(int idx, int val) {
int idx_l = --upper_bound(in[idx].begin(), in[idx].end(), val) - in[idx].begin();
int idx_r = lower_bound(in[idx].begin(), in[idx].end(), val) - in[idx].begin();
int P = in[idx].size();
vector<int> cand;
if(idx_l >= 0 && idx_l < P) cand.push_back(in[idx][idx_l]);
if(idx_r >= 0 && idx_r < P) cand.push_back(in[idx][idx_r]);
return cand;
}
signed main() {
int N, M, K, S, T; cin >> N >> M >> K >> S >> T;
in[1].push_back(S);
rep(i,0,M) {
int a, b, c; cin >> a >> b >> c;
exist[a] = true;
edges[a][b].push_back(Edge{a+1, c});
out[a].push_back(b);
in[a+1].push_back(c);
}
int cnt = 0;
repq(i,1,N) {
sort(in[i].begin(), in[i].end());
sort(out[i].begin(), out[i].end());
in[i].erase(unique(in[i].begin(), in[i].end()), in[i].end());
out[i].erase(unique(out[i].begin(), out[i].end()), out[i].end());
cnt += exist[i];
}
if(cnt != N-1) {
cout << -1 << endl;
return 0;
}
if(N == 1) {
cout << abs(S - T) << endl;
return 0;
}
rec[S] = 0;
repq(i,1,N) {
// floor, cost
priority_queue<Elem> tmp;
for(auto x : rec) tmp.push(Elem{x.first, x.second});
while(!tmp.empty()) {
Elem cur = tmp.top(); tmp.pop();
int idx = lower_bound(in[i].begin(), in[i].end(), cur.flr) - in[i].begin();
int dd[] = {-1, 1};
// printf("cur.flr = %lld, idx = %lld\n", cur.flr, idx);
rep(k,0,2) {
int nidx = idx + dd[k];
if(nidx < 0 || nidx >= (int)in[i].size()) continue;
int to = in[i][nidx];
// printf("cur = %lld, adjacent_elem = %lld\n", cur.flr, to);
int diff = abs(cur.flr - to);
if(!rec.count(to) || rec[to] > rec[cur.flr] + diff) {
int ncost = rec[to] = rec[cur.flr] + diff;
tmp.push(Elem{to, ncost});
}
}
}
/*
printf("index = %lld\n", i);
for(auto x : rec) {
printf("height = %lld, cost = %lld\n", x.first, x.second);
}
*/
map<int, int> swp;
if(i == N) continue;
for(auto from : out[i]) {
int cur_cost = INF;
vector<int> cand = get_array(i, from);
// for(auto point : cand) printf("candidate: %lld\n", point);
for(auto point : cand) chmin(cur_cost, rec[point] + abs(point - from));
for(auto e : edges[i][from]) {
int to = e.to_height;
if(!swp.count(to) || swp[to] > cur_cost) {
swp[to] = cur_cost;
}
}
}
swap(rec, swp);
}
int ans = INF;
for(auto x : rec) chmin(ans, x.second + abs(x.first - T));
cout << ans << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0