第十四届蓝桥杯三月真题刷题训练——第 12 天（3.15）

2023-03-14 22:25:17

...全文
426 4 打赏 收藏 举报

0人已提交

4 条回复

• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

``````#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
#define pb(s) push_back(s);
#define SZ(s) ((int)s.size());
#define ms(s,x) memset(s, x, sizeof(s))
#define all(s) s.begin(),s.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 300010;

int n, m;
std::vector<PII> e[N];
LL dist[N];
bool st[N];
void dijkstra(int start, int v)
{
memset(dist, 0x3f, sizeof(dist));
dist[start] = v;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({v, start});
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second, distance = t.first;
if (st[ver]) continue;
st[ver] = true;
for (auto [j, x] : e[ver])
{
if (dist[j] > dist[ver] + x) {
dist[j] = dist[ver] + x;
heap.push({dist[j], j});
}
}
}
}
void solve()
{
cin >> n >> m;
std::vector<std::vector<int>> a(n, std::vector<int>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> a[i][j];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (i != n - 1) {
int u = i * m + j, v = (i + 1) * m + j;
e[u].push_back({v, a[i + 1][j]});
e[v].push_back({u, a[i][j]});
}
if (j != m - 1) {
int u = i * m + j, v = i * m + j + 1;
e[u].push_back({v, a[i][j + 1]});
e[v].push_back({u, a[i][j]});
}
}
}
dijkstra(0, a[0][0]);
LL ans = 0;
for (int i = 0; i < n * m; ++i) ans = max(ans, dist[i]);
cout << ans << '\n';
}
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
while (t--)
{
solve();
}
return 0;
}
```c++

``````

```

3.9w+

2023-03-14 22:25

刷题！