結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-09 22:35:48 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 178 ms / 2,000 ms |
| コード長 | 2,517 bytes |
| コンパイル時間 | 1,623 ms |
| コンパイル使用メモリ | 139,532 KB |
| 最終ジャッジ日時 | 2025-01-16 21:00:06 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
constexpr ll inf = 1e+17;
using Data = tuple<ll, int, int>;
ll dist[505][505][2];
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<vector<int>> c(N, vector<int>(N));
for(int i = 0; i < M; ++i){
int h, w;
cin >> h >> w;
--h, --w;
cin >> c[h][w];
}
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
dist[i][j][0] = dist[i][j][1] = inf;
}
}
dist[0][0][0] = 0;
priority_queue<Data, vector<Data>, greater<Data>> que;
que.emplace(0, 0, 0);
while(!que.empty()){
auto [d, num, f] = que.top();
que.pop();
int x = num / N, y = num % N;
if(d > dist[x][y][f]) continue;
for(int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
if(f == 1){
if(chmin(dist[nx][ny][1], dist[x][y][1] + 1 + c[nx][ny])){
que.emplace(dist[nx][ny][1], nx * N + ny, 1);
}
}
else{
if(chmin(dist[nx][ny][1], dist[x][y][0] + 1)){
que.emplace(dist[nx][ny][1], nx * N + ny, 1);
}
if(chmin(dist[nx][ny][0], dist[x][y][0] + 1 + c[nx][ny])){
que.emplace(dist[nx][ny][0], nx * N + ny, 0);
}
}
}
}
cout << min(dist[N - 1][N - 1][0], dist[N - 1][N - 1][1]) << endl;
return 0;
}