P5304 简化版。
容易想到不论每一个值 是多少,都是每一条边每一条边来考虑。
那么可以考虑先求出每一条边在 为多大时才需要封闭。
令第 条边需要封闭时的 为 。
那么对于第 条边的终点,令它到达 时的距离为 ,同理令 到第 条边的起点的距离为 。
于是有 。
发现 已知,, 均可以通过跑最短路得到(以 为起点跑正图,以 为起点跑反图)。
所以每一条边的 均可以求出,再将其排序求前缀和,每一次询问时二分查找出最大的合法边,将其前缀和输出即可。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 5, INF = 1e18;
int n, m, a, b, jua[N], jub[N], sum[N];
struct zrr {
int u, v, w, c;
} l[N];
struct node {
int id, ju;
bool operator < (const node A) const {
return ju > A.ju;
}
};
vector<node> g[N], g2[N];
struct lll {
int d, id;
} k[N];
bool cmp (lll qwe, lll ewq) {
return qwe.d < ewq.d;
}
void dijka (int s) {
memset(jua, 0x7f, sizeof(jua));
priority_queue<node> pq;
pq.push({s, 0});
while(!pq.empty()) {
node t = pq.top(); pq.pop();
// cout << t.id << " " << t.ju << "\n";
if (t.ju >= jua[t.id]) continue;
jua[t.id] = t.ju;
for (int i = 0; i < g[t.id].size(); i++) {
pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju});
}
}
}
void dijkb (int s) {
memset(jub, 0x7f, sizeof(jub));
priority_queue<node> pq;
pq.push({s, 0});
while(!pq.empty()) {
node t = pq.top(); pq.pop();
if (t.ju >= jub[t.id]) continue;
jub[t.id] = t.ju;
for (int i = 0; i < g2[t.id].size(); i++) {
pq.push({g2[t.id][i].id, g2[t.id][i].ju + t.ju});
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> a >> b;
for (int i = 1; i <= m; i++) {
cin >> l[i].u >> l[i].v >> l[i].w >> l[i].c;
int u = l[i].u, v = l[i].v, w = l[i].w;
g[u].push_back({v, w}); g2[v].push_back({u, w});
}
dijka(a);
// cout << "\n";
dijkb(b);
// for (int i = 1; i <= n; i++) {
// cout << jua[i] << " " << jub[i] << "\n";
// }
int ans = 0;
for (int i = 1; i <= m; i++) {
int lu = jua[l[i].u] + jub[l[i].v] + l[i].w;
if (jua[l[i].u] > 1e18 || jub[l[i].v] > 1e18) lu = INF;
k[i] = {lu, l[i].c};
// cout << l[i].u << " " << l[i].v << " " << lu << "\n";
}
sort(k + 1, k + m + 1, cmp);
for (int i = 1; i <= m; i++) {
sum[i] = sum[i - 1] + k[i].id;
}
int q; cin >> q;
while(q--) {
int d; cin >>d;
int l = 0, r = m;
while(l < r) {
int mid = (l + r + 1) / 2;
if (k[mid].d > d) r = mid - 1;
else l = mid;
}
cout << sum[l] << "\n";
}
return 0;
}