結果

問題 No.2064 Smallest Sequence on Grid
ユーザー erbowlerbowl
提出日時 2022-09-04 15:52:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,738 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 219,720 KB
実行使用メモリ 402,112 KB
最終ジャッジ日時 2024-04-29 16:21:50
合計ジャッジ時間 10,574 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 3 ms
5,376 KB
testcase_06 WA -
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
}
0