結果
| 問題 |
No.2695 Warp Zone
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-03-22 21:44:53 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 53 ms / 2,000 ms |
| コード長 | 1,281 bytes |
| コンパイル時間 | 2,658 ms |
| コンパイル使用メモリ | 212,052 KB |
| 最終ジャッジ日時 | 2025-02-20 11:29:15 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll h, w, n;
cin >> h >> w >> n;
vector<tuple<int,int,int,int>> edge(n);
vector<pair<int,int>> ca;
ca.emplace_back(1, 1);
ca.emplace_back(h, w);
for(auto &&[a, b, c, d] : edge){
cin >> a >> b >> c >> d;
ca.emplace_back(a, b);
ca.emplace_back(c, d);
}
sort(ca.begin(), ca.end());
int m = ca.size();
vector<vector<ll>> A(m, vector<ll>(m, 1ll << 60));
for(auto [a, b, c, d] : edge){
int v = lower_bound(ca.begin(), ca.end(), make_pair(a, b)) - ca.begin();
int u = lower_bound(ca.begin(), ca.end(), make_pair(c, d)) - ca.begin();
A[v][u] = 1;
}
for(int i = 0; i < m; i++) A[i][i] = 0;
for(int u = 0; u < m; u++){
for(int v = 0; v < m; v++){
A[u][v] = min(A[u][v], abs((ll)ca[u].first - ca[v].first) + abs(ca[u].second - ca[v].second));
}
}
vector<ll> dp(m, 1ll << 60);
vector<bool> used(m);
dp[0] = 0;
for(int i = 0; i < m; i++){
int u = -1;
ll mn = 1ll << 60;
for(int j = 0; j < m; j++){
if(used[j]) continue;
if(dp[j] < mn){
mn = dp[j];
u = j;
}
}
if(u == -1) continue;
used[u] = true;
for(int v = 0; v < m; v++){
dp[v] = min(dp[v], dp[u] + A[u][v]);
}
}
cout << dp.back() << '\n';
}