結果

問題 No.1283 Extra Fee
ユーザー auauaauaua
提出日時 2020-11-06 22:43:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 445 ms / 2,000 ms
コード長 2,084 bytes
コンパイル時間 1,859 ms
コンパイル使用メモリ 178,540 KB
実行使用メモリ 61,712 KB
最終ジャッジ日時 2023-08-10 06:05:09
合計ジャッジ時間 8,609 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 17 ms
6,284 KB
testcase_12 AC 21 ms
6,812 KB
testcase_13 AC 15 ms
5,540 KB
testcase_14 AC 74 ms
13,200 KB
testcase_15 AC 111 ms
19,008 KB
testcase_16 AC 22 ms
6,560 KB
testcase_17 AC 302 ms
56,408 KB
testcase_18 AC 397 ms
55,680 KB
testcase_19 AC 445 ms
58,132 KB
testcase_20 AC 403 ms
54,900 KB
testcase_21 AC 404 ms
55,388 KB
testcase_22 AC 355 ms
49,924 KB
testcase_23 AC 350 ms
59,760 KB
testcase_24 AC 417 ms
59,864 KB
testcase_25 AC 443 ms
59,892 KB
testcase_26 AC 439 ms
60,064 KB
testcase_27 AC 443 ms
59,936 KB
testcase_28 AC 431 ms
60,076 KB
testcase_29 AC 316 ms
61,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll MOD=1e9+7;
const ll mod=MOD;
const int MAX=1000010;
const double PI=acos(-1.0);

//dijkstra(O(ElogV))
//0-indexed
void dijkstra(ll n,ll s,vector<vector<pll> >&G,vector<ll> &d){
    priority_queue<pll,vector<pll>,greater<pll> >pq;
    rep(i,n){
        d[i]=inf;
    }
    d[s]=0;
    pq.push(mp(0,s));
    while(!pq.empty()){
        pll k=pq.top();pq.pop();
        ll v=k.second;
        if(d[v]<k.first)continue;
        for(auto e:G[v]){
            if(d[e.first]>d[v]+e.second){
                d[e.first]=d[v]+e.second;
                pq.push(mp(d[e.first],e.first));
            }
        }
    }
}

ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};

signed main(){
    ll n,m;cin>>n>>m;
    vector<vector<ll> >c(n,vector<ll>(n));
    rep(i,m){
        ll h,w;cin>>h>>w;
        h--;w--;
        cin>>c[h][w];
    }
    vector<vector<pll> >G(n*n);
    vector<vector<pll> >G2(n*n);
    vector<ll>d(n*n);
    vector<ll>d2(n*n);
    rep(i,n){
        rep(j,n){
            ll now=i*n+j;
            rep(l,4){
                ll nx=i+dx[l],ny=j+dy[l];
                if(nx<0||nx>=n||ny<0||ny>=n)continue;
                ll cost=1+c[nx][ny];
                ll to=nx*n+ny;
                G[now].pb(mp(to,cost));
                G2[to].pb(mp(now,cost));
            }
        }
    }
    dijkstra(n*n,0,G,d);
    dijkstra(n*n,n*n-1,G2,d2);
    ll ans=inf;
    REP(i,1,n*n-1){
        ll h=i/n;
        ll w=i%n;
        ll dd=d[i]+d2[i]-c[h][w];
        if(c[h][w]==0)dd--;
        ans=min(d[i]+d2[i]-c[h][w],ans);
    }
    cout<<ans<<endl;
}
0