結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
Example0911
|
| 提出日時 | 2020-11-06 22:06:48 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 458 ms / 2,000 ms |
| コード長 | 1,617 bytes |
| コンパイル時間 | 2,416 ms |
| コンパイル使用メモリ | 208,556 KB |
| 最終ジャッジ日時 | 2025-01-15 20:46:23 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
#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;
}
Example0911