A
神秘。
B
神秘。
C
错因:没有判断 到 最短路小于等于 的时候直接输出 。
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const LL kMaxN = 5e5 + 1;
bool f[kMaxN];
LL n, m, l, k, a[2], d[2][kMaxN], t[kMaxN];
struct N {
LL x, w;
friend bool operator<(N a, N b) {
return a.w > b.w;
}
};
vector<N> g[kMaxN];
void D(LL o) {
fill(d[o], d[o] + n + 1, 1e18);
fill(f + 1, f + n + 1, 0);
priority_queue<N> q;
d[o][a[o]] = 0;
q.push({a[o], 0});
while (q.size()) {
LL x = q.top().x;
q.pop();
if (f[x]) {
continue;
}
f[x] = 1;
for (N i : g[x]) {
if (d[o][i.x] > d[o][x] + i.w) {
d[o][i.x] = d[o][x] + i.w;
q.push({i.x, d[o][i.x]});
}
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> a[0] >> a[1] >> l >> k;
for (LL i = 1, u, v, w; i <= m; i++) {
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
D(0), D(1);
if (d[1][a[0]] <= k) {
cout << n * (n - 1) / 2 << '\n';
return 0;
}
for (int i = 1; i <= n; i++) {
t[i] = d[1][i];
}
sort(t + 1, t + n + 1);
LL r = 0;
for (int i = 1; i <= n; i++) {
LL p = k - d[0][i] - l;
LL e = upper_bound(t + 1, t + n + 1, p) - t - 1;
r += e;
}
cout << r << '\n';
return 0;
}D
错因:没有想到当遍历到了之后再爆破的情况,还有就是想得太复杂了,以为要很多遍 dijkstra,但是大体是对的。
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const LL kMaxN = 2e5 + 1;
bool f[kMaxN], e[kMaxN], is[kMaxN];
LL n, m, cnt, d[kMaxN], c[kMaxN];
struct N {
LL x, w;
friend bool operator<(N a, N b) {
return a.w > b.w;
}
};
vector<N> g[kMaxN];
vector<LL> v[kMaxN], ch[kMaxN], fg, nw, to; // to:到达的有结界的
priority_queue<N> q;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (LL i = 1, u, v, w; i <= m; i++) {
cin >> u >> v >> w;
g[u].push_back({v, w});
}
for (LL i = 1; i <= n; i++) {
cin >> c[i];
for (LL j = 1, x; j <= c[i]; j++) {
is[i] = 1;
cin >> x, v[x].push_back(i), ch[i].push_back(x);
}
}
fill(d + 1, d + n + 1, 1e18);
d[1] = 0;
priority_queue<N> q;
q.push({1, 0});
fill(f + 1, f + n + 1, 0);
while (q.size()) {
LL x = q.top().x;
q.pop();
if (f[x]) {
continue;
}
f[x] = 1;
for (LL i : v[x]) {
c[i]--;
if (c[i] == 0 && d[i] != 1e18 && !f[i]) {
for (int o : ch[i]) {
d[i] = max(d[i], d[o]);
}
is[i] = 0;
q.push({i, d[i]});
}
}
for (N i : g[x]) {
if (d[i.x] > d[x] + i.w && !c[i.x]) {
d[i.x] = d[x] + i.w;
for (int o : ch[i.x]) {
d[i.x] = max(d[i.x], d[o]);
}
q.push({i.x, d[i.x]});
} else if (d[i.x] > d[x] + i.w) {
d[i.x] = d[x] + i.w;
}
}
}
cout << d[n] << '\n';
return 0;
}