結果

問題 No.1283 Extra Fee
ユーザー Example0911Example0911
提出日時 2020-11-06 22:06:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 1,617 bytes
コンパイル時間 2,264 ms
コンパイル使用メモリ 215,360 KB
実行使用メモリ 66,308 KB
最終ジャッジ日時 2024-04-27 23:17:46
合計ジャッジ時間 8,638 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
28,868 KB
testcase_01 AC 11 ms
28,992 KB
testcase_02 AC 11 ms
29,000 KB
testcase_03 AC 11 ms
28,992 KB
testcase_04 AC 11 ms
28,992 KB
testcase_05 AC 10 ms
28,992 KB
testcase_06 AC 11 ms
29,120 KB
testcase_07 AC 11 ms
28,868 KB
testcase_08 AC 10 ms
28,996 KB
testcase_09 AC 11 ms
29,000 KB
testcase_10 AC 10 ms
28,868 KB
testcase_11 AC 24 ms
30,916 KB
testcase_12 AC 29 ms
31,040 KB
testcase_13 AC 23 ms
30,116 KB
testcase_14 AC 71 ms
34,624 KB
testcase_15 AC 108 ms
38,084 KB
testcase_16 AC 31 ms
30,788 KB
testcase_17 AC 259 ms
62,040 KB
testcase_18 AC 359 ms
59,540 KB
testcase_19 AC 389 ms
61,012 KB
testcase_20 AC 363 ms
58,876 KB
testcase_21 AC 377 ms
59,496 KB
testcase_22 AC 329 ms
56,120 KB
testcase_23 AC 330 ms
61,956 KB
testcase_24 AC 397 ms
61,908 KB
testcase_25 AC 403 ms
62,076 KB
testcase_26 AC 402 ms
62,084 KB
testcase_27 AC 404 ms
61,992 KB
testcase_28 AC 412 ms
62,148 KB
testcase_29 AC 282 ms
66,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<ll, pair<ll, ll>>;
ll INF = (1LL << 60);
int v, e;
vector<pair<ll, ll>>edge[1000010];


vector<vector<ll>> dijkstra(int s) {
	priority_queue<P, vector<P>, greater<P>>pq;
	vector<vector<ll>>dist(v, vector<ll>(2, INF));
	dist[s][0] = 0;
	pq.push({ dist[s][0],{s, 0} });
	while (!pq.empty()) {
		P p = pq.top(); pq.pop();
		ll d = p.first, from = p.second.first;
		ll tmp = p.second.second;
		if (dist[from][tmp] < d)continue;
		for (auto nv : edge[from]) {
			int to = nv.first; ll cost = nv.second;
			if (dist[from][tmp] + cost < dist[to][tmp]) {
				dist[to][tmp] = dist[from][tmp] + cost;
				pq.push({ dist[to][tmp],{to, tmp} });
			}
			if (tmp == 0 && cost > 1) {
				if (dist[from][tmp] + 1 < dist[to][1]) {
					dist[to][1] = dist[from][tmp] + 1;
					pq.push({ dist[to][1],{to, 1} });
				}
			}
		}
	}
	return dist;
}
ll cost[510][510];
int main() {
	cin >> v >> e; 
	for (int i = 0; i < 510; i++) {
		for (int j = 0; j < 510; j++) {
			cost[i][j] = 1;
		}
	}
	for (int i = 0; i < e; i++) {
		ll h, w, c; cin >> h >> w >> c;
		cost[h - 1][w - 1] += c;
	}
	for (int i = 0; i < v; i++) {
		for (int j = 0; j < v; j++) {
			if (i + 1 < v) {
				edge[i*v + j].push_back({ (i + 1)*v + j, cost[i + 1][j] });
				edge[(i + 1)*v + j].push_back({ i*v + j, cost[i][j] });
			}
			if (j + 1 < v) {
				edge[i*v + j].push_back({ i*v + j + 1, cost[i][j + 1] });
				edge[i*v + (j + 1)].push_back({ i*v + j, cost[i][j] });
			}
		}
	}
	v *= v;
	vector<vector<ll>>dist = dijkstra(0);
	cout << min(dist[v - 1][0], dist[v - 1][1]) << endl;

}
0