結果
| 問題 | No.2064 Smallest Sequence on Grid |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-09-04 15:52:51 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,738 bytes |
| 記録 | |
| コンパイル時間 | 2,074 ms |
| コンパイル使用メモリ | 212,064 KB |
| 最終ジャッジ日時 | 2025-02-07 02:40:32 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 2 WA * 2 RE * 3 TLE * 13 MLE * 9 |
ソースコード
typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ll h,w;
std::cin >> h>>w;
vector<vector<pair<ll,ll>>> prev(h,vector<pair<ll,ll>>(w,{-1,-1}));
vector<string> s(h);
for (int i = 0; i < h; i++) {
std::cin >> s[i];
}
priority_queue<tuple<ll,ll,ll>,vector<tuple<ll,ll,ll>>,greater<tuple<ll,ll,ll>>> pq;
if(s[0][1]>s[1][0]){
pq.push({1, s[1][0],1});
prev[1][0] = {0,0};
}else if(s[1][0]>s[0][1]){
pq.push({1, s[0][1],0});
prev[0][1] = {0,0};
}else{
pq.push({1, s[1][0],1});
pq.push({1, s[1][0],1});
prev[0][1] = {0,0};
prev[1][0] = {0,0};
}
vector<ll> minc(h+w+2,1000);
while(!pq.empty()){
auto [now, ch, y] = pq.top();pq.pop();
// std::cout << now<<" "<<ch << std::endl;
if(minc[now]<ch)continue;
minc[now] = ch;
auto x = now-y;
// std::cout << now<<" "<<x<<" "<<y << std::endl;
if(x==w-1&&y==h-1)break;
if(x==w-1){
pq.push({now+1, s[y+1][x] ,y+1});
prev[y+1][x] = {y, x};
}else if(y==h-1){
pq.push({now+1, s[y][x+1],y});
prev[y][x+1] = {y,x};
}else{
pq.push({now+1, s[y+1][x] ,y+1});
prev[y+1][x] = {y, x};
pq.push({now+1, s[y][x+1],y});
prev[y][x+1] = {y,x};
}
}
string ans = "";
pair<ll,ll> now = {h-1,w-1};
while(true){
ans += s[now.first][now.second];
if(now.first==0&&now.second==0)break;
now = prev[now.first][now.second];
}
reverse(ans.begin(),ans.end());
std::cout << ans << std::endl;
}