- 入门7 最短路
O2?
- @ 2026-11-18 22:22:54
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct edge{
int v,w;
};
vector<vector<edge> > G;
const int N = 2e5+5;
int ds[N] = {},dt[N] = {};
int n,m,s,t,l,k;
void Dijkstra(){
ds[s] = 0;
priority_queue<pair<int,int> >q;
q.push({s,0});
while(!q.empty()) {
int first = q.top().first;
int second = -q.top().second;
q.pop();
if(ds[first] != second) {
continue;
}
for(int i = 0; i < G[first].size();i ++ ) {
int y = G[first][i].v;
int w = G[first][i].w;
if(ds[y] > ds[first] + w){
ds[y] = ds[first] + w;
q.push({y,-ds[y]});
}
}
}
}
void Dijkstra1(){
dt[t] = 0;
priority_queue<pair<int,int> >q;
q.push({t,0});
while(!q.empty()) {
int first = q.top().first;
int second = -q.top().second;
q.pop();
if(dt[first] != second) {
continue;
}
for(int i = 0; i < G[first].size();i ++ ) {
int y = G[first][i].v,w = G[first][i].w;
if(dt[y] > dt[first] + w){
dt[y] = dt[first] + w;
q.push({y,-dt[y]});
}
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m >> s >> t >> l >> k;
G.resize(200005);
for(int i = 1; i <= m; i ++ ) {
int u,v,w;
cin >> u >> v >> w;
G[u].push_back({v,w});
G[v].push_back({u,w});
}
for(int i = 1; i <= n;i ++ ) {
if(i != s) {
ds[i] = 1e18;
}
if(i != t) {
dt[i] = 1e18;
}
}
Dijkstra();
Dijkstra1();
if(ds[t] <= k) {
cout << n*(n-1) / 2 << '\n';
return 0;
}
sort(dt+1,dt+n+1);
int ans = 0;
for(int i = 1; i <= n;i ++ ) {
auto it = upper_bound(dt+1,dt+n+1,k-l-ds[i])-dt-1;
ans += it;
}
cout << ans << '\n';
return 0;
}
此代码在 hfoj 上可以通过,但是在锣鼓不开 -O2 的情况下过不了。
0 comments
No comments so far...