結果
問題 |
No.1283 Extra Fee
|
ユーザー |
|
提出日時 | 2020-11-07 12:17:38 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 374 ms / 2,000 ms |
コード長 | 1,579 bytes |
コンパイル時間 | 1,086 ms |
コンパイル使用メモリ | 89,580 KB |
実行使用メモリ | 16,836 KB |
最終ジャッジ日時 | 2024-11-16 07:04:07 |
合計ジャッジ時間 | 6,741 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <utility> #include <queue> #include <tuple> using namespace std; typedef tuple<long long, int, int> P; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; int main(){ int N, M; cin >> N >> M; vector<int> h(M), w(M); vector<long long> c(M); vector<vector<long long>> cost(N, vector<long long> (N, 1)); for(int i = 0; i < M; i++) { cin >> h[i] >> w[i] >> c[i]; cost[h[i] - 1][w[i] - 1] += c[i]; } long long INF = 1e18; vector<vector<long long>> d(2, vector<long long>(N * N, INF)); priority_queue<P, vector<P>, greater<P>> q; d[0][0] = 0; q.push(P(0, 0, 0)); while(!q.empty()){ P v = q.top(); q.pop(); long long temp = get<0>(v); int s = get<1>(v), t = get<2>(v); if(d[s][t] < temp) continue; if(s == 0 && cost[t / N][t % N] > 1){ if(d[1][t] > d[0][t] - cost[t / N][t % N] + 1){ d[1][t] = d[0][t] - cost[t / N][t % N] + 1; q.push(P(d[1][t], 1, t)); } } for(int i = 0; i < 4; i++){ int x = t / N, y = t % N; int nx = x + dx[i], ny = y + dy[i]; if(0 <= nx && nx < N && 0 <= ny && ny < N){ int nt = nx * N + ny; if(d[s][nt] > d[s][t] + cost[nx][ny]){ d[s][nt] = d[s][t] + cost[nx][ny]; q.push(P(d[s][nt], s, nt)); } } } } cout << min(d[1][N * N - 1], d[0][N * N - 1]) << endl; }