题解:P4814 [CCO 2014] 国王格鲁夫
King_tissue · · 题解
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;
}