Submission #1112316

#TimeUsernameProblemLanguageResultExecution timeMemory
1112316luvnaConstruction Project 2 (JOI24_ho_t2)C++14
100 / 100
188 ms29276 KiB
#include<bits/stdc++.h>

#define all(v) v.begin(), v.end()
#define endl "\n"
#define int long long

using namespace std;

typedef long long ll;

const int N = 2e5 + 15;
const ll INF = 1e18;

int n, m, S, T;
ll L, limit;
vector<pair<int,int>> g[N];
vector<ll> distS, distT;

void dijk(vector<ll>& dist, int root){
    dist.resize(N, INF);
    dist[root] = 0;
    priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
    pq.push({0,root});
    while(!pq.empty()){
        ll wu; int u; tie(wu, u) = pq.top(); pq.pop();
        if(wu > dist[u]) continue;
        for(auto [v, wv] : g[u]){
            if(dist[v] > dist[u] + wv){
                dist[v] = dist[u] + wv;
                pq.push({dist[v],v});
            }
        }
    }
}

void solve(){
    cin >> n >> m;

    cin >> S >> T >> L >> limit;

    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});
    }

    dijk(distS, S);
    dijk(distT, T);

    if(distS[T] <= limit){//match whatever
        cout << 1LL*n*(n-1) / 2;
        return;
    }

    ll ans = 0;

    vector<ll> A, B;

    //a + b + L <= limit
    //a <= limit - L - b
    //sort a decrease, sort b increase -> greed

    for(int i = 1; i <= n; i++){
        A.push_back(distS[i]);
        B.push_back(distT[i]);
    }

    sort(all(A)); sort(all(B), greater<ll>());

    for(int i = 0,j = 0; i < B.size(); i++){
        while(j < A.size() && A[j] <= limit - L - B[i]) j++;
        ans += j;
    }

    cout << ans;
}

signed main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(0); cout.tie(0);

    #define task "task"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message (stderr)

Main.cpp: In function 'void dijk(std::vector<long long int>&, long long int)':
Main.cpp:27:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   27 |         for(auto [v, wv] : g[u]){
      |                  ^
Main.cpp: In function 'void solve()':
Main.cpp:70:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i = 0,j = 0; i < B.size(); i++){
      |                          ~~^~~~~~~~~~
Main.cpp:71:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |         while(j < A.size() && A[j] <= limit - L - B[i]) j++;
      |               ~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...