結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2020-11-07 23:44:44 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 139 ms / 2,000 ms |
| コード長 | 2,064 bytes |
| コンパイル時間 | 951 ms |
| コンパイル使用メモリ | 100,676 KB |
| 実行使用メモリ | 9,904 KB |
| 最終ジャッジ日時 | 2024-11-16 07:07:14 |
| 合計ジャッジ時間 | 3,925 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
/* -*- 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;
}
tnakao0123