結果

問題 No.1283 Extra Fee
ユーザー hiro71687khiro71687k
提出日時 2023-04-10 18:34:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 598 ms / 2,000 ms
コード長 2,208 bytes
コンパイル時間 5,268 ms
コンパイル使用メモリ 260,260 KB
実行使用メモリ 11,776 KB
最終ジャッジ日時 2024-04-15 22:36:19
合計ジャッジ時間 11,816 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 14 ms
5,376 KB
testcase_12 AC 27 ms
5,376 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 52 ms
5,376 KB
testcase_15 AC 93 ms
5,888 KB
testcase_16 AC 16 ms
5,376 KB
testcase_17 AC 143 ms
10,880 KB
testcase_18 AC 598 ms
11,136 KB
testcase_19 AC 457 ms
11,520 KB
testcase_20 AC 446 ms
11,136 KB
testcase_21 AC 411 ms
11,264 KB
testcase_22 AC 391 ms
10,496 KB
testcase_23 AC 169 ms
11,520 KB
testcase_24 AC 250 ms
11,520 KB
testcase_25 AC 454 ms
11,520 KB
testcase_26 AC 503 ms
11,648 KB
testcase_27 AC 454 ms
11,776 KB
testcase_28 AC 498 ms
11,776 KB
testcase_29 AC 155 ms
11,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll inf=144499999999994;
ll mod=998244353;
int main(){
    ll n,m;
    cin >> n >> m;
    vector<vector<ll>>g(n+2,vector<ll>(n+2,inf));
    for (ll i = 1; i <=n; i++)
    {
        for (ll j = 1; j <=n; j++)
        {
            g[i][j]=0;
        }
    }
    for (ll i = 0; i < m; i++)
    {
        ll h,w,c;
        cin >> h >> w >> c;
        g[h][w]=c;
    }
    queue<pair<ll,ll>>que;
    que.push({1,1});
    vector<vector<ll>>memo1(n+2,vector<ll>(n+2,inf)),memo2(n+2,vector<ll>(n+2,inf)),memo(n+2,vector<ll>(n+2,0));
    memo1[1][1]=0;
    memo2[1][1]=0;
    while (!que.empty())
    {
        ll x=que.front().first;
        ll y=que.front().second;
        que.pop();
        if (x<=0||x>n||y<=0||y>n)
        {
            continue;
        }
        memo[x][y]=0;
        for (ll i = -1; i <=1 ; i++)
        {
            for (ll j = -1; j <=1; j++)
            {
                if (abs(i)+abs(j)!=1)
                {
                    continue;
                }
                if (memo1[x+i][y+j]>memo1[x][y]+g[x+i][y+j]+1)
                {
                    memo1[x+i][y+j]=memo1[x][y]+g[x+i][y+j]+1;
                    if (memo[x+i][y+j]==0)
                    {
                        que.push({x+i,y+j});
                        memo[x+i][y+j]=1;
                    }
                }
                if (memo2[x+i][y+j]>memo1[x][y]+1)
                {
                    memo2[x+i][y+j]=memo1[x][y]+1;
                    if (memo[x+i][y+j]==0)
                    {
                        que.push({x+i,y+j});
                        memo[x+i][y+j]=1;
                    }
                }
                if (memo2[x+i][y+j]>memo2[x][y]+g[x+i][y+j]+1)
                {
                    memo2[x+i][y+j]=memo2[x][y]+g[x+i][y+j]+1;
                    if (memo[x+i][y+j]==0)
                    {
                        que.push({x+i,y+j});
                        memo[x+i][y+j]=1;
                    }
                }
            }
        }
    }
    cout << min(memo1[n][n],memo2[n][n]) << endl;
}
0