Skip to content

Commit 531565e

Browse files
committed
LibreOJ: 2090
「ZJOI2016」旅行者
1 parent cb5b40b commit 531565e

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

LibreOJ/2090.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @file 2090.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2022-12-09
5+
*
6+
* @copyright Copyright (c) 2022
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
#define maxn 20005
20+
#define maxq 100005
21+
22+
typedef pair<int, int> pii;
23+
typedef tuple<int, int, int> tiii;
24+
25+
int n, m;
26+
vector<vector<pii>> graph;
27+
priority_queue<pii, vector<pii>, greater<pii>> que;
28+
int dist[maxn], ans[maxq];
29+
bool vis[maxn];
30+
31+
int _(int x, int y) { return (x - 1) * m + y; }
32+
pii $(int x) { return {(x - 1) / m + 1, (x - 1) % m + 1}; }
33+
34+
void Dijkstra(int S, int xl, int xr, int yl, int yr) {
35+
for (int x = xl; x <= xr; x++)
36+
for (int y = yl; y <= yr; y++) dist[_(x, y)] = 1e9, vis[_(x, y)] = false;
37+
que.emplace(dist[S] = 0, S);
38+
while (!que.empty()) {
39+
int p = que.top().second;
40+
que.pop();
41+
if (vis[p]) continue;
42+
vis[p] = true;
43+
for (auto i : graph[p])
44+
if (xl <= $(i.first).first && $(i.first).first <= xr && yl <= $(i.first).second && $(i.first).second <= yr &&
45+
dist[i.first] > dist[p] + i.second)
46+
que.emplace(dist[i.first] = dist[p] + i.second, i.first);
47+
}
48+
return;
49+
}
50+
51+
void solve(int xl, int xr, int yl, int yr, vector<tiii> ques) {
52+
if (ques.empty()) return;
53+
if (xl == xr && yl == yr) {
54+
for (auto i : ques) ans[get<2>(i)] = 0;
55+
return;
56+
}
57+
if (xr - xl > yr - yl) {
58+
int xm = (xl + xr) >> 1;
59+
for (int y = yl; y <= yr; y++) {
60+
Dijkstra(_(xm, y), xl, xr, yl, yr);
61+
for (auto i : ques) ans[get<2>(i)] = min(ans[get<2>(i)], dist[get<0>(i)] + dist[get<1>(i)]);
62+
}
63+
vector<tiii> sque[2];
64+
for (auto i : ques)
65+
if (($(get<0>(i)).first <= xm) == ($(get<1>(i)).first <= xm)) sque[$(get<0>(i)).first > xm].push_back(i);
66+
solve(xl, xm, yl, yr, sque[0]), solve(xm + 1, xr, yl, yr, sque[1]);
67+
} else {
68+
int ym = (yl + yr) >> 1;
69+
for (int x = xl; x <= xr; x++) {
70+
Dijkstra(_(x, ym), xl, xr, yl, yr);
71+
for (auto i : ques) ans[get<2>(i)] = min(ans[get<2>(i)], dist[get<0>(i)] + dist[get<1>(i)]);
72+
}
73+
vector<tiii> sque[2];
74+
for (auto i : ques)
75+
if (($(get<0>(i)).second <= ym) == ($(get<1>(i)).second <= ym)) sque[$(get<0>(i)).second > ym].push_back(i);
76+
solve(xl, xr, yl, ym, sque[0]), solve(xl, xr, ym + 1, yr, sque[1]);
77+
}
78+
return;
79+
}
80+
81+
void solve(void) {
82+
cin >> n >> m, graph.resize(_(n, m) + 1);
83+
for (int i = 1, v; i <= n; i++)
84+
for (int j = 1; j < m; j++)
85+
cin >> v, graph[_(i, j)].emplace_back(_(i, j + 1), v), graph[_(i, j + 1)].emplace_back(_(i, j), v);
86+
for (int i = 1, v; i < n; i++)
87+
for (int j = 1; j <= m; j++)
88+
cin >> v, graph[_(i, j)].emplace_back(_(i + 1, j), v), graph[_(i + 1, j)].emplace_back(_(i, j), v);
89+
int q;
90+
cin >> q;
91+
vector<tiii> ques;
92+
for (int i = 1, sx, sy, tx, ty; i <= q; i++)
93+
cin >> sx >> sy >> tx >> ty, ques.emplace_back(_(sx, sy), _(tx, ty), i), ans[i] = INT_MAX;
94+
solve(1, n, 1, m, ques);
95+
for (int i = 1; i <= q; i++) cout << ans[i] << endl;
96+
return;
97+
}
98+
99+
bool mem2;
100+
101+
int main() {
102+
ios::sync_with_stdio(false), cin.tie(nullptr);
103+
#ifdef LOCAL
104+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
105+
#endif
106+
107+
int _ = 1;
108+
while (_--) solve();
109+
110+
#ifdef LOCAL
111+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
112+
#endif
113+
return 0;
114+
}

0 commit comments

Comments
 (0)