結果

問題 No.2913 二次元距離空間
ユーザー makichan
提出日時 2024-10-04 21:51:20
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 1,721 bytes
コンパイル時間 4,802 ms
コンパイル使用メモリ 320,180 KB
実行使用メモリ 30,996 KB
最終ジャッジ日時 2024-10-04 21:51:27
合計ジャッジ時間 6,688 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using mint = atcoder::static_modint<998244353>;
//using mint = atcoder::static_modint<1000000007>;
using namespace std;
using namespace atcoder;
using ld = long double;
using ll = long long;
#define mp(a,b) make_pair(a,b)
#define rep(i,s,n) for(int i=s; i<n; i++)
const vector<int> dx{1,0,-1,0},dy{0,1,0,-1};


struct dijkstra{
    public:
    dijkstra():_n(0){}
    dijkstra(int n):_n(n),G(n){}
    void add_edge(int from,int to,ll d){
        G[from].push_back({to,d});
    }
    vector<ll> start_from(int start){
        vector<ll> dist(_n,-1);
        priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> Q;
        Q.push({0,start});
        dist[start]=0;
        while(!Q.empty()){
            auto p=Q.top();
            Q.pop();
            int now=p.second;
            ll d=p.first;
            if(d!=dist[now])continue;
            for(auto e:G[now]){
                int to=e.first;
                ll w=e.second;
                if(dist[to]==-1 || w+d<dist[to])Q.push(mp(w+d,to)),dist[to]=w+d;
            }
        }
        return dist;
    }
    private:
        int _n;
        vector<vector<pair<int,ll>>> G;
};

int main(){
    int h,w;
    cin >> h >> w;
    vector<string> s(h);
    rep(i,0,h)cin >> s[i];
    dijkstra G(h*w);
    rep(i,0,h)rep(j,0,w-1)if(s[i][j]==s[i][j+1]){
        G.add_edge(i*w+j,i*w+j+1,1000000);
        G.add_edge(i*w+j+1,i*w+j,1000000);
    }
    rep(i,0,h-1)rep(j,0,w)if(s[i][j]==s[i+1][j]){
        G.add_edge(i*w+j,(i+1)*w+j,1);
        G.add_edge((i+1)*w+j,i*w+j,1);
    }
    ll ans=G.start_from(0).back();
    if(ans==-1)cout << "No";
    else cout << "Yes\n" << ans/1000000 << " " << ans%1000000; 
}
0