結果
問題 | No.614 壊れたキャンパス |
ユーザー |
|
提出日時 | 2018-01-14 13:10:44 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 246 ms / 2,000 ms |
コード長 | 2,038 bytes |
コンパイル時間 | 1,112 ms |
コンパイル使用メモリ | 123,308 KB |
実行使用メモリ | 14,180 KB |
最終ジャッジ日時 | 2024-12-24 02:32:25 |
合計ジャッジ時間 | 5,431 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> #include <iterator> using namespace std; const long long INF = LLONG_MAX / 4; int main() { int n, m, k, s, t; cin >> n >> m >> k >> s >> t; vector<vector<pair<int, int> > > edges(n+1); edges[0].push_back(make_pair(-1, s)); edges[n].push_back(make_pair(t, -1)); for(int i=0; i<m; ++i){ int a, b, c; cin >> a >> b >> c; edges[a].push_back(make_pair(b, c)); } vector<long long> dp(1, 0); for(int i=0; i<n; ++i){ vector<tuple<int, int, bool> > v; for(unsigned j=0; j<edges[i].size(); ++j) v.push_back(make_tuple(edges[i][j].second, j, true)); for(unsigned j=0; j<edges[i+1].size(); ++j) v.push_back(make_tuple(edges[i+1][j].first, j, false)); vector<long long> nextDp(edges[i+1].size(), INF); long long cost = INF; sort(v.begin(), v.end()); for(const auto& t : v){ int pos, k; bool isInput; tie(pos, k, isInput) = t; if(isInput) cost = min(cost, dp[k] - pos); else nextDp[k] = min(nextDp[k], cost + pos); } cost = INF * 2; reverse(v.begin(), v.end()); for(const auto& t : v){ int pos, k; bool isInput; tie(pos, k, isInput) = t; if(isInput) cost = min(cost, dp[k] + pos); else nextDp[k] = min(nextDp[k], cost - pos); } dp.swap(nextDp); } if(dp[0] < INF) cout << dp[0] << endl; else cout << -1 << endl; return 0; }