This repository has been archived on 2026-11-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
csps2026/subway/subway.cpp
T
2026-11-01 00:15:19 +08:00

67 lines
2.0 KiB
C++

#include<bits/stdc++.h>
using namespace std;
int c, n, m, d, q; // 25 500 5e5 20 1e5
int w[500005];
vector<pair<int, int>> fri[500505];
int de[500505];
int dline[505][505];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
void dig(int s) {
memset(de, 0x7f, sizeof(de[0]) * (n + m + 1));
de[s] = 0;
pq.push({0, s});
while(!pq.empty()) {
pair<int, int> u = pq.top();
pq.pop();
for(pair<int, int> v : fri[u.second]) {
if(de[v.second] > de[u.second] + v.first) {
de[v.second] = de[u.second] + v.first;
pq.push({de[v.second], v.second});
}
}
}
}
int main() {
freopen("subway.in", "r", stdin);
freopen("subway.out", "w", stdout);
scanf("%d%d%d%d%d", &c, &n, &m, &d, &q);
for(int i = 1; i <= m; ++i) {
scanf("%d", &w[i]);
}
for(int i = 1, k; i <= n; ++i) {
scanf("%d", &k);
for(int j = 1, sta, ti; j <= k; ++j) {
scanf("%d%d", &sta, &ti);
fri[m + i].push_back({ti, sta});
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);
int ans = INT_MAX;
for(pair<int, int> i : fri[x]) {
for(pair<int, int> 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 条线路各视为图上的一个点,分别和地铁站连线
考虑优化,注意到可以预处理维护两个地铁线路的最短距离,再枚举起点终点的线路选择
*/