답안 #1094861

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1094861 2024-09-30T17:08:32 Z Triseedot Construction Project 2 (JOI24_ho_t2) C++17
0 / 100
1 ms 348 KB
// In honor of El Psy Congroo

//#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>
#include <random>
#include <chrono>
using namespace std;

// variables
using ld = long double;
using ll = long long;
using ull = unsigned long long;
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// bitwise operations
#define cnt_bit(n) __builtin_popcountll(n)
#define low_bit(n) ((n) & (-(n)))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_bit(n, i) ((n) | (1ll << (i)))
#define reset_bit(n, i) ((n) & ~(1ll << (i)))
#define flip_bit(n, i) ((n) ^ (1ll << (i)))
// math
#define sqr(n) ((n) * (n))
int log2_floor(ull n) {
    return n ? __builtin_clzll(1) - __builtin_clzll(n) : -1;
}
// vector
#define len(x) (int) x.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for(auto& el : v) { 
        is >> el;
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < len(v); i++) {
        if (i) os << ' ';
        os << v[i];
    }
    return os;
}
template<class... Args>
auto create(size_t n, Args&&... args) {
    if constexpr(sizeof...(args) == 1) {
        return vector(n, args...);
    }
    else {
        return vector(n, create(args...));
    }
}
template<typename T>
void remove_dups(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
// random
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

//////solution start//////

void solve() {
    int n, m;
    cin >> n >> m;
    int s, t, l;
    ll k;
    cin >> s >> t >> l >> k;
    k -= l;
    s--, t--;
    vector<vector<pair<int, int>>> g(n);
    for (int i = 0; i < m; i++) {
        int v, u, w;
        cin >> v >> u >> w;
        v--, u--;
        g[v].emplace_back(u, w);
        g[u].emplace_back(v, w);
    }

    auto dijkstra = [&] (int start) {
        vector<optional<ll>> dist(n);
        dist[start] = 0;
        min_heap<pair<ll, int>> pq;
        pq.emplace(*dist[start], start);
        while (!pq.empty()) {
            auto [dist_v, v] = pq.top();
            pq.pop();
            if (dist[v] != dist_v) continue;
            for (auto [u, w] : g[v]) {
                if (dist[u].has_value() && *dist[u] < *dist[v] + w) continue;
                dist[u] = *dist[v] + w;
                pq.emplace(*dist[u], u);
            }
        }
        return dist;
    };

    vector dist_s = dijkstra(s), dist_t = dijkstra(t);
    if (dist_s[t] <= k) {
        cout << n * 1ll * (n - 1) / 2;
        return;
    }
    vector<ll> vals;
    for (int v = 0; v < n; v++) {
        if (dist_t[v].has_value()) {
            vals.push_back(*dist_t[v]);
        }
    }
    sort(all(vals));
    ll ans = 0;
    for (int v = 0; v < n; v++) {
        if (!dist_s[v].has_value() || dist_s[v] > k) continue;
        ans += lower_bound(all(vals), k - *dist_s[v]) - vals.begin() + 1;
    }
    cout << ans << '\n';
}

//////solution end//////
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int tt = 1;
    //cin >> tt;
    for (int i = 1; i <= tt; i++) {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -