Submission #1294318

#TimeUsernameProblemLanguageResultExecution timeMemory
1294318IcelastConstruction Project 2 (JOI24_ho_t2)C++20
100 / 100
243 ms24924 KiB
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
vector<ll> dijkstra(int n, int s, vector<vector<pair<ll, ll>>> &adj){
    vector<ll> dist(n+1, INF);
    dist[s] = 0;
    vector<bool> vis(n+1, false);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    pq.push({0, s});
    while(!pq.empty()){
        ll d_u = pq.top().first;
        int u = pq.top().second;
        pq.pop();
        if(vis[u]){
            continue;
        }
        vis[u] = true;
        for(auto it : adj[u]){
            int v = it.first;
            ll w = it.second;
            if(vis[v]) continue;
            if(dist[v] > d_u+w){
                dist[v] = d_u+w;
                pq.push({dist[v], v});
            }
        }
    }
    return dist;
}
void solve(){
    int n, m;
    cin >> n >> m;
    ll S, T, L, K;
    cin >> S >> T >> L >> K;
    vector<vector<pair<ll, ll>>> adj(n+1);
    for(int i = 1; i <= m; i++){
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    vector<ll> dS = dijkstra(n, S, adj), dT = dijkstra(n, T, adj);
    if(dS[T] <= K){
        cout << (ll)n*(n-1)/2;
        return;
    }
    vector<ll> tv;
    for(int i = 1; i <= n; i++){
        tv.push_back(dT[i]);
    }
    sort(tv.begin(), tv.end());
    tv.push_back(INF);
    K -= L;
    ll ans = 0;
    for(int i = 1; i <= n; i++){
        int p = upper_bound(tv.begin(), tv.end(), K-dS[i]) - tv.begin() - 1;
        p++;
        ans += p;
    }
    cout << ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...