結果

問題 No.1283 Extra Fee
ユーザー saxofone111saxofone111
提出日時 2021-03-07 14:18:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 447 ms / 2,000 ms
コード長 2,492 bytes
コンパイル時間 2,033 ms
コンパイル使用メモリ 216,488 KB
実行使用メモリ 77,740 KB
最終ジャッジ日時 2024-04-27 23:49:02
合計ジャッジ時間 8,548 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 15 ms
7,424 KB
testcase_12 AC 19 ms
7,808 KB
testcase_13 AC 15 ms
6,272 KB
testcase_14 AC 66 ms
16,128 KB
testcase_15 AC 113 ms
23,552 KB
testcase_16 AC 21 ms
7,680 KB
testcase_17 AC 270 ms
70,972 KB
testcase_18 AC 404 ms
70,300 KB
testcase_19 AC 417 ms
73,316 KB
testcase_20 AC 401 ms
68,844 KB
testcase_21 AC 399 ms
69,948 KB
testcase_22 AC 358 ms
63,032 KB
testcase_23 AC 330 ms
75,520 KB
testcase_24 AC 406 ms
75,484 KB
testcase_25 AC 435 ms
75,648 KB
testcase_26 AC 447 ms
75,560 KB
testcase_27 AC 432 ms
75,676 KB
testcase_28 AC 437 ms
75,644 KB
testcase_29 AC 313 ms
77,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

struct dijkstra{
  ll N;
  llvec d;
  vector<vector<edge>> e;
  dijkstra(ll n){
    N = n;
    //d = llvec(N, 1e18);
    e = vector<vector<edge>>(N);
  }

  void add_edge(ll from, ll to, ll cost){
    e[from].push_back({to, cost});
  }

  void run(ll start){
    priority_queue<P, vector<P>, greater<P>> que;
    que.push({0, start});
    d = llvec(N, 1e18);
    d[start]=0;
    while(!que.empty()){
      P q = que.top();que.pop();
      ll dc = q.first;
      ll x = q.second;
      if(dc>d[x]){
        continue;
      }else{
        for(auto ip: e[x]){
          if(d[ip.x]<=d[x]+ip.c){
            continue;
          }else{
            d[ip.x]= d[x]+ip.c;
            que.push({d[ip.x], ip.x});
          }
        }
      }
    }
  }  
};


/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N, M;
  cin >> N >> M;
  vector<llvec> c(N, llvec(N, 0));
  dijkstra dijk(N*N*2);
  rep(i, M){
    ll a, b, d;
    cin >> a >> b >> d;
    a--;b--;
    c[a][b]+=d;
  }
  rep(i, N){
    rep(j, N){
      if(i-1>=0)dijk.add_edge(i*N+j, (i-1)*N+j, c[i][j]+1);
      if(i+1<N)dijk.add_edge(i*N+j, (i+1)*N+j, c[i][j]+1);
      if(j-1>=0)dijk.add_edge(i*N+j, i*N+j-1, c[i][j]+1);
      if(j+1<N)dijk.add_edge(i*N+j, i*N+j+1, c[i][j]+1);

      if(i-1>=0)dijk.add_edge(i*N+j+N*N, (i-1)*N+j+N*N, c[i][j]+1);
      if(i+1<N)dijk.add_edge(i*N+j+N*N, (i+1)*N+j+N*N, c[i][j]+1);
      if(j-1>=0)dijk.add_edge(i*N+j+N*N, i*N+j-1+N*N, c[i][j]+1);
      if(j+1<N)dijk.add_edge(i*N+j+N*N, i*N+j+1+N*N, c[i][j]+1);

      if(i-1>=0)dijk.add_edge(i*N+j, (i-1)*N+j+N*N, 1);
      if(i+1<N)dijk.add_edge(i*N+j, (i+1)*N+j+N*N, 1);
      if(j-1>=0)dijk.add_edge(i*N+j, i*N+j-1+N*N, 1);
      if(j+1<N)dijk.add_edge(i*N+j, i*N+j+1+N*N, 1);
    }
  }
  dijk.run(0);
  ll ans = min(dijk.d[N*N-1], dijk.d[2*N*N-1]);
  cout << ans << endl;
  return 0;
}
0