| 比赛 |
!信心赛 |
评测结果 |
AAAAAAAAAA |
| 题目名称 |
海拔 |
最终得分 |
100 |
| 用户昵称 |
xuyuqing |
运行时间 |
0.970 s |
| 代码语言 |
C++ |
内存使用 |
15.34 MiB |
| 提交时间 |
2026-01-17 10:18:02 |
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
constexpr int N = 252525;
int n;
vector<pair<int, long long>> graph[N];
int s;
int t;
long long res[N];
bool vis[N];
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
void add (int u, int v, long long w) {
graph[u].emplace_back(v, w);
}
void addBoth (int u, int v, long long w) {
graph[u].emplace_back(v, w);
graph[v].emplace_back(u, w);
}
int cal (int x, int y) {
return (x - 1) * (n + 2) + y;
}
long long dijkstra (int s, int t) {
memset (res, 0x3f, sizeof (res));
res[s] = 0;
pq.emplace(0, s);
while (!pq.empty()) {
auto [_, u] = pq.top();
pq.pop();
if (vis[u]) {
continue;
}
vis[u] = true;
for (auto [v, w] : graph[u]) {
if (res[u] + w < res[v]) {
res[v] = res[u] + w;
pq.emplace(res[v], v);
}
}
}
return res[t];
}
int main () {
freopen ("altitude.in", "r", stdin);
freopen ("altitude.out", "w", stdout);
cin >> n;
long long num;
for (int i = 1; i <= n + 1; i++) {
for (int j = 1; j <= n; j++) {
cin >> num;
add (cal (i, j + 1), cal (i + 1, j + 1), num);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n + 1; j++) {
cin >> num;
add (cal (i + 1, j + 1), cal (i + 1, j), num);
}
}
for (int i = 1; i <= n + 1; i++) {
for (int j = 1; j <= n; j++) {
cin >> num;
add (cal (i + 1, j + 1), cal (i, j + 1), num);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n + 1; j++) {
cin >> num;
add (cal (i + 1, j), cal (i + 1, j + 1), num);
}
}
s = 0;
t = cal (n + 2, n + 2) + 1;
for (int i = 1; i <= n; i++) {
addBoth (s, cal (n + 2, i + 1), 0);
addBoth (s, cal (i + 1, 1), 0);
addBoth (cal (1, i + 1), t, 0);
addBoth (cal (i + 1, n + 2), t, 0);
}
cout << min (dijkstra (s, t), dijkstra (t, s)) << endl;
return 0;
}