結果

問題 No.1283 Extra Fee
ユーザー chocopuuchocopuu
提出日時 2020-11-27 18:13:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,929 bytes
コンパイル時間 2,340 ms
コンパイル使用メモリ 214,304 KB
実行使用メモリ 14,036 KB
最終ジャッジ日時 2023-08-10 06:25:16
合計ジャッジ時間 7,531 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,308 KB
testcase_01 AC 4 ms
7,436 KB
testcase_02 AC 4 ms
7,412 KB
testcase_03 AC 4 ms
7,316 KB
testcase_04 AC 4 ms
7,324 KB
testcase_05 AC 4 ms
7,312 KB
testcase_06 AC 4 ms
7,392 KB
testcase_07 AC 5 ms
7,632 KB
testcase_08 AC 5 ms
7,372 KB
testcase_09 AC 4 ms
7,372 KB
testcase_10 AC 5 ms
7,364 KB
testcase_11 AC 14 ms
7,744 KB
testcase_12 AC 16 ms
7,564 KB
testcase_13 AC 14 ms
7,424 KB
testcase_14 AC 49 ms
7,724 KB
testcase_15 AC 76 ms
8,188 KB
testcase_16 AC 18 ms
7,660 KB
testcase_17 AC 172 ms
14,036 KB
testcase_18 AC 263 ms
9,268 KB
testcase_19 AC 289 ms
9,408 KB
testcase_20 AC 269 ms
9,244 KB
testcase_21 AC 274 ms
9,440 KB
testcase_22 AC 244 ms
9,184 KB
testcase_23 AC 219 ms
9,364 KB
testcase_24 AC 290 ms
9,420 KB
testcase_25 AC 300 ms
9,628 KB
testcase_26 AC 297 ms
9,400 KB
testcase_27 AC 297 ms
9,552 KB
testcase_28 AC 298 ms
9,476 KB
testcase_29 AC 181 ms
13,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
//#include "atcoder/all"
//using namespace atcoder;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template<class T>istream&operator >>(istream&is,vector<T>&vec){for(T&x:vec)is>>x;return is;}
template<class T>inline void out(T t){cout << t << "\n";}
template<class T,class... Ts>inline void out(T t,Ts... ts){cout << t << " ";out(ts...);}
template<class T>inline bool CHMIN(T&a,T b){if(a > b){a = b;return true;}return false;}
template<class T>inline bool CHMAX(T&a,T b){if(a < b){a = b;return true;}return false;}
constexpr int INF = 1e18;

int dist[505][505][2];
vector<pair<int,int>>dir = {{1,0},{0,1},{-1,0},{0,-1}};
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1, 1};

signed main(){
	int N, M;
	cin >> N >> M;
	vector<vector<int>>fee(N, vector<int>(N));
	REP(i, M) {
		int h, w, c;
		cin >> h >> w >> c;
		fee[h - 1][w - 1] = c;
	}
	REP(i, N) REP(j, N) fee[i][j] += 1;
	REP(i, 505) REP(j, 505) REP(k, 2) dist[i][j][k] = INF;
	dist[0][0][0] = 0;
	using T = tuple<int, int, int, int>;
	priority_queue<T, vector<T>, greater<T>> que;
	que.push({0, 0, 0, 0});
	while(que.size()) {
		auto[cost, i, j, free] = que.top();
		que.pop();
		if(dist[i][j][free] != cost) continue;
		for(auto[dx, dy]: dir) {
			int ni = i + dx;
			int nj = j + dy;
			if(!IN(0, ni, N) || !IN(0, nj, N)) continue;
			if(CHMIN(dist[ni][nj][free], cost + fee[ni][nj])) {
				que.push({dist[ni][nj][free], ni, nj, free});
			}
			if(free == 0 && CHMIN(dist[ni][nj][1], cost + 1)) {
				que.push({dist[ni][nj][1], ni, nj, 1});
			}
		}
	}
	out(min(dist[N - 1][N - 1][0], dist[N - 1][N - 1][1]));
}
0