From d09b9c8dc2d480c91b35c3670cc5bea04e2cc2c0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 1 Nov 2026 00:15:19 +0800 Subject: [PATCH] 00:15(160) --- subway/subway.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/subway/subway.cpp b/subway/subway.cpp index 72149c2..e7d7d6f 100644 --- a/subway/subway.cpp +++ b/subway/subway.cpp @@ -4,6 +4,7 @@ int c, n, m, d, q; // 25 500 5e5 20 1e5 int w[500005]; vector> fri[500505]; int de[500505]; +int dline[505][505]; priority_queue, vector>, greater>> pq; void dig(int s) { memset(de, 0x7f, sizeof(de[0]) * (n + m + 1)); @@ -35,16 +36,32 @@ int main() { fri[sta].push_back({ti, m + i}); } } + memset(dline, 0x7f, sizeof dline); + for(int i = 1; i <= n; ++i) { + dig(m + i); + for(int j = 1; j <= n; ++j) { + dline[i][j] = de[m + j]; + } + } while(q--) { int x, y; scanf("%d%d", &x, &y); - dig(x); - printf("%d\n", w[x] + w[y] + de[y]); + int ans = INT_MAX; + for(pair i : fri[x]) { + for(pair j : fri[y]) { + ans = min(ans, dline[i.second - m][j.second - m] + i.first + j.first); + } + } + // dig(x); + printf("%d\n", w[x] + w[y] + ans); } return 0; } /* +1-15 g++ subway/subway.cpp -o subway/subway -std=c++14 -O2 -static -Wall && cp subway/subway ~ && chmod +x ~/subway && ~/subway 500 条线路各视为图上的一个点,分别和地铁站连线 + +考虑优化,注意到可以预处理维护两个地铁线路的最短距离,再枚举起点终点的线路选择 */ \ No newline at end of file