結果
| 問題 |
No.2913 二次元距離空間
|
| コンテスト | |
| ユーザー |
kwm_t
|
| 提出日時 | 2024-10-05 10:16:41 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 110 ms / 2,000 ms |
| コード長 | 3,236 bytes |
| コンパイル時間 | 2,843 ms |
| コンパイル使用メモリ | 263,640 KB |
| 実行使用メモリ | 30,648 KB |
| 最終ジャッジ日時 | 2024-10-05 10:16:47 |
| 合計ジャッジ時間 | 4,613 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
template<typename T>
struct Dijkstra {
private:
int n;
bool needRestore;
struct Edge {
int to;
T cost;
};
std::vector<std::vector<Edge>> g;
std::vector<std::vector<Edge>> gR;
public:
const T INF = numeric_limits<T>::max();
std::vector<T> dp;
Dijkstra() {}
Dijkstra(int n, bool needRestore = false) : n(n), needRestore(needRestore) {
g.resize(n);
if (needRestore)gR.resize(n);
}
Dijkstra<T>& operator=(const Dijkstra<T>& obj) {
this->v = obj.v;
this->g = obj.g;
this->dp = obj.dp;
return *this;
}
void add_edge(int from, int to, T weight, bool twoWay = false) {
g[from].push_back({ to,weight });
if (needRestore)gR[to].push_back({ from,weight });
if (twoWay) {
g[to].push_back({ from,weight });
if (needRestore)gR[from].push_back({ to,weight });
}
}
int add_vertex() {
g.push_back(std::vector<Edge>());
return n++;
}
void build(int s) {
dp.assign(n, INF);
std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<>> pq;
dp[s] = 0;
pq.push({ dp[s], s });
while (!pq.empty()) {
auto p = pq.top();
pq.pop();
T cost = p.first;
int v = p.second;
if (dp[v] < cost) continue;
for (const auto &e : g[v]) {
if (dp[e.to] > dp[v] + e.cost) {
dp[e.to] = dp[v] + e.cost;
pq.push({ dp[e.to], e.to });
}
}
}
}
std::vector<int> restore(int s, int t) {
// restoreのsはbuildのsと一致している必要がある
std::vector<int>route = { t };
while (s != t) {
for (const auto & e : gR[t]) {
if (dp[e.to] + e.cost == dp[t]) {
t = e.to;
route.push_back(t);
break;
}
}
}
std::reverse(route.begin(), route.end());
return route;
}
T get(int t) {
if (INF == dp[t])return -1;
else return dp[t];
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w; cin >> h >> w;
vector<string>s(h);
Dijkstra<long long> dijkstra(h * w);
rep(i, h)cin >> s[i];
rep(i, h)rep(j, w - 1)if ('#' != s[i][j] && '#' != s[i][j + 1]) {
int u = i * w + j;
int v = u + 1;
dijkstra.add_edge(u, v, INF, true);
}
rep(i, h - 1)rep(j, w)if ('#' != s[i][j] && '#' != s[i + 1][j]) {
int u = i * w + j;
int v = u + w;
dijkstra.add_edge(u, v, 1, true);
}
dijkstra.build(0);
auto ans = dijkstra.get(h*w - 1);
if (-1 == ans) {
cout << "No" << endl;
}
else {
cout << "Yes" << endl;
cout << ans / INF << " " << ans % INF << endl;
}
return 0;
}
kwm_t