結果
| 問題 |
No.614 壊れたキャンパス
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-12-14 00:37:30 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 1,614 ms / 2,000 ms |
| コード長 | 2,079 bytes |
| コンパイル時間 | 2,008 ms |
| コンパイル使用メモリ | 183,056 KB |
| 実行使用メモリ | 167,824 KB |
| 最終ジャッジ日時 | 2024-12-14 10:32:00 |
| 合計ジャッジ時間 | 7,881 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int MM = 1e9 + 7;
const double eps = 1e-8;
const int MAXN = 2e6 + 10;
int n, m;
void prework(){
}
void read(){
}
ll K;
int S, T;
vector<int> E[MAXN];
vector<PII> F[MAXN];
vector<ll> D[MAXN];
const ll INF = 1ll << 60;
unordered_map<int, int> tag1, tag2;
void solve(int casi){
// cout << "Case #" << casi << ": ";
cin >> n >> m >> K >> S >> T;
E[1].push_back(S);
E[n].push_back(T);
for (int i = 1; i <= m; i++){
int x, y, z;
cin >> x >> y >> z;
E[x].push_back(y);
E[x+1].push_back(z);
F[x].push_back(PII(y, z));
}
sort(E[1].begin(), E[1].end());
E[1].resize(unique(E[1].begin(), E[1].end()) - E[1].begin());
for (auto &x : E[1]){
D[1].push_back(abs(x - S));
}
if (n == 1){
cout << abs(S - T) << endl;
return ;
}
for (int i = 1; i < n; i++){
tag1.clear();
tag2.clear();
sort(E[i+1].begin(), E[i+1].end());
E[i+1].resize(unique(E[i+1].begin(), E[i+1].end()) - E[i+1].begin());
sort(F[i].begin(), F[i].end());
D[i+1].resize(E[i+1].size());
int n2 = E[i+1].size(), n1 = E[i].size();
for (int j = 0; j < E[i].size(); j++)
tag1[E[i][j]] = j;
for (int j = 0; j < E[i + 1].size(); j++)
tag2[E[i + 1][j]] = j;
for (int j = 0; j < n2; j++)
D[i+1][j] = INF;
for (auto &x : F[i]){
D[i+1][tag2[x.second]] = min(D[i+1][tag2[x.second]], D[i][tag1[x.first]]/* + abs(x.first - x.second)*/);
}
for (int j = 1; j < n2; j++){
D[i+1][j] = min(D[i+1][j], D[i+1][j-1] + abs(E[i+1][j] - E[i+1][j-1]));
}
for (int j = n2 - 2; j >= 0; j--){
D[i+1][j] = min(D[i+1][j], D[i+1][j+1] + abs(E[i+1][j] - E[i+1][j+1]));
}
/* cout << "=========== " << i << endl;
for (int j = 0; j < n2; j++)
cout << E[i+1][j] << ' ' << D[i+1][j] << endl;
*/
}
if (D[n][tag2[T]] >= INF)
D[n][tag2[T]] = -1;
cout << D[n][tag2[T]] << endl;
}
void printans(){
}
int main(){
std::ios::sync_with_stdio(false);
prework();
int T = 1;
// cin>>T;
for(int i = 1; i <= T; i++){
read();
solve(i);
printans();
}
return 0;
}