結果

問題 No.1283 Extra Fee
ユーザー tnakao0123tnakao0123
提出日時 2020-11-07 23:44:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 2,064 bytes
コンパイル時間 852 ms
コンパイル使用メモリ 102,268 KB
実行使用メモリ 9,900 KB
最終ジャッジ日時 2024-04-27 23:37:19
合計ジャッジ時間 3,983 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 8 ms
6,944 KB
testcase_12 AC 10 ms
6,940 KB
testcase_13 AC 7 ms
6,944 KB
testcase_14 AC 25 ms
7,016 KB
testcase_15 AC 38 ms
7,248 KB
testcase_16 AC 10 ms
6,944 KB
testcase_17 AC 107 ms
9,796 KB
testcase_18 AC 130 ms
8,440 KB
testcase_19 AC 138 ms
8,460 KB
testcase_20 AC 131 ms
8,424 KB
testcase_21 AC 133 ms
8,432 KB
testcase_22 AC 118 ms
8,388 KB
testcase_23 AC 116 ms
8,476 KB
testcase_24 AC 130 ms
8,644 KB
testcase_25 AC 143 ms
8,472 KB
testcase_26 AC 149 ms
8,476 KB
testcase_27 AC 149 ms
8,480 KB
testcase_28 AC 148 ms
8,476 KB
testcase_29 AC 117 ms
9,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1283.cc:  No.1283 Extra Fee - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 500;
const long long LINF = 1LL << 62;
const int dxs[] = { 1, 0, -1, 0 }, dys[] = { 0, -1, 0, 1 };

/* typedef */

typedef long long ll;
typedef pair<ll,int> pli;

/* global variables */

int fs[MAX_N][MAX_N];
ll ds0[MAX_N][MAX_N], ds1[MAX_N][MAX_N];

/* subroutines */

inline int xy2p(int x, int y, int n) { return n * y + x; }
inline void p2xy(int p, int &x, int &y, int n) { x = p % n, y = p / n; }

void dijkstra(int n, int sx, int sy, ll ds[MAX_N][MAX_N]) {
  for (int y = 0; y < n; y++) fill(ds[y], ds[y] + n, LINF);
  ds[sy][sx] = 0;

  priority_queue<pli> q;
  q.push(pli(0, xy2p(sx, sy, n)));

  while (! q.empty()) {
    pli u = q.top(); q.pop();
    ll ud = -u.first;
    int ux, uy;
    p2xy(u.second, ux, uy, n);
    if (ds[uy][ux] != ud) continue;

    for (int di = 0; di < 4; di++) {
      int vx = ux + dxs[di], vy = uy + dys[di];
      if (vx >= 0 && vx < n && vy >= 0 && vy < n) {
	ll vd = ud + 1 + fs[vy][vx];
	if (ds[vy][vx] > vd) {
	  ds[vy][vx] = vd;
	  q.push(pli(-vd, xy2p(vx, vy, n)));
	}
      }
    }
  }
}

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int y, x, c;
    scanf("%d%d%d", &y, &x, &c);
    y--, x--;
    fs[y][x] = c;
  }

  if (false)
    for (int y = 0; y < n; y++) {
      for (int x = 0; x < n; x++) printf("%d ", fs[y][x]); putchar('\n');
    }

  dijkstra(n, 0, 0, ds0);
  dijkstra(n, n - 1, n - 1, ds1);

  ll mind = LINF;
  for (int y = 0; y < n; y++)
    for (int x = 0; x < n; x++) {
      ll d = ds0[y][x] + ds1[y][x] - (ll)fs[y][x] * 2;
      if (mind > d) mind = d;
    }

  printf("%lld\n", mind);
  return 0;
}
0