結果

問題 No.2095 High Rise
ユーザー Haa
提出日時 2022-10-07 22:04:39
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 799 ms / 2,000 ms
コード長 1,479 bytes
コンパイル時間 1,852 ms
コンパイル使用メモリ 180,560 KB
実行使用メモリ 137,216 KB
最終ジャッジ日時 2024-06-12 18:31:39
合計ジャッジ時間 8,128 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y) {return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;};
constexpr ll MOD=998244353;
constexpr ll INF=2e18;

struct edge { ll to, cost; };
vector<edge> g[2000500];
vector<ll> d(2000500,INF);

void dijkstra(int s){
    priority_queue<P,vector<P>,greater<P>> que;
    d[s]=0;
    que.push({0,s});
    while(!que.empty()){
        P p=que.top();
        que.pop();
        int v=p.second;
        if(d[v]<p.first)
            continue;
        for(auto e:g[v]){
            if(d[e.to]>d[v]+e.cost){
                d[e.to]=d[v]+e.cost;
                que.push({d[e.to],e.to});
            }
        }
    }
}

int main(){
    int n, m; cin >> n >> m;
    VVI a(n,VI(m));
    REP(i,n)REP(j,m) cin >> a[i][j];
    if(n==1){
        cout << 0 << endl;
        return 0;
    }
    int s=2000498, t=2000499;
    REP(j,m) g[s].push_back({j,a[0][j]});
    REP(j,m) g[(n-1)*m+j].push_back({t,0});
    REP(i,n){
        REP(j,m) g[i*m+j].push_back({(i+n)*m,0});
        REP(j,m) g[(i+n)*m].push_back({i*m+j,a[i][j]});
        if(i<n-1)
            REP(j,m) g[i*m+j].push_back({(i+1)*m+j,a[i+1][j]});
    }
    dijkstra(s);
    cout << d[t] << endl;
    return 0;
}
0