Submission #1119625

# Submission time Handle Problem Language Result Execution time Memory
1119625 2024-11-27T08:22:42 Z InvMOD Construction Project 2 (JOI24_ho_t2) C++14
100 / 100
309 ms 29408 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;


int n, m, S, T;

ll dist[2][N], L, K;

vector<pair<int,ll>> adj[N];

void dijkstra(int id, int source){
    priority_queue<pair<ll,int>> pq;

    FOR(i, 1, n){
        if(i == source){
            dist[id][i] = 0;
            pq.push(make_pair(0, i));
        }
        else dist[id][i] = INF;
    }

    while(!pq.empty()){
        pair<ll,int> eq = pq.top(); pq.pop();

        ll cur_dist = -eq.first; int x = eq.second;
        if(cur_dist > dist[id][x]) continue;

        for(pair<int,ll> e : adj[x]){
            int v = e.first; ll w = e.second;
            if(dist[id][v] > cur_dist + w){
                dist[id][v] = cur_dist + w;
                pq.push(make_pair(-dist[id][v], v));
            }
        }
    }
    return;
}

void solve()
{
    cin >> n >> m >> S >> T >> L >> K;
    FOR(i, 1, m){
        int u,v; ll w; cin >> u >> v >> w;
        adj[u].push_back(make_pair(v, w));
        adj[v].push_back(make_pair(u, w));
    }

    dijkstra(0, S);
    dijkstra(1, T);

    if(dist[0][T] <= K){
        cout << (1ll * n * (n - 1)) / 2ll <<"\n";
        return;
    }

    vector<int> idS(n+1); iota(1 + all(idS), 1);
    vector<int> idT(n+1); iota(1 + all(idT), 1);

    sort(1 + all(idS), [&](int u, int v){
        return dist[0][u] < dist[0][v];
    });

    sort(1 + all(idT), [&](int u, int v){
        return dist[1][u] < dist[1][v];
    });

    vector<bool> rem(n+1, true);

    ll answer = 0;
    for(int i = 1, j = n; i <= n; i++){
        int u = idS[i];
        while(j > 0 && dist[1][idT[j]] + dist[0][u] + L > K) rem[idT[j]] = false, j--;
        if(dist[0][u] + dist[1][u] <= K){
            answer = answer + (n - u);
        }
        else answer = answer + 1ll * ((j > 0 ? j - rem[u] : j));
    }

    cout << answer <<"\n";
    return;
}

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

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

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

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Correct 4 ms 4944 KB Output is correct
5 Correct 6 ms 5200 KB Output is correct
6 Correct 5 ms 5480 KB Output is correct
7 Correct 66 ms 21540 KB Output is correct
8 Correct 83 ms 21320 KB Output is correct
9 Correct 92 ms 21856 KB Output is correct
10 Correct 5 ms 4948 KB Output is correct
11 Correct 4 ms 4948 KB Output is correct
12 Correct 4 ms 5204 KB Output is correct
13 Correct 12 ms 5204 KB Output is correct
14 Correct 69 ms 16540 KB Output is correct
15 Correct 240 ms 23180 KB Output is correct
16 Correct 5 ms 4948 KB Output is correct
17 Correct 5 ms 5228 KB Output is correct
18 Correct 105 ms 22196 KB Output is correct
19 Correct 5 ms 4960 KB Output is correct
20 Correct 4 ms 4960 KB Output is correct
21 Correct 5 ms 5216 KB Output is correct
22 Correct 6 ms 5380 KB Output is correct
23 Correct 59 ms 16480 KB Output is correct
24 Correct 219 ms 23204 KB Output is correct
25 Correct 5 ms 4948 KB Output is correct
26 Correct 5 ms 5460 KB Output is correct
27 Correct 102 ms 27824 KB Output is correct
28 Correct 73 ms 20392 KB Output is correct
29 Correct 78 ms 19256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5112 KB Output is correct
2 Correct 5 ms 5156 KB Output is correct
3 Correct 5 ms 4944 KB Output is correct
4 Correct 5 ms 4944 KB Output is correct
5 Correct 5 ms 4944 KB Output is correct
6 Correct 5 ms 4944 KB Output is correct
7 Correct 4 ms 5164 KB Output is correct
8 Correct 5 ms 5200 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 4 ms 4956 KB Output is correct
12 Correct 4 ms 4944 KB Output is correct
13 Correct 4 ms 5044 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 4 ms 4948 KB Output is correct
16 Correct 5 ms 4956 KB Output is correct
17 Correct 6 ms 4960 KB Output is correct
18 Correct 5 ms 4960 KB Output is correct
19 Correct 4 ms 4960 KB Output is correct
20 Correct 4 ms 4960 KB Output is correct
21 Correct 5 ms 4972 KB Output is correct
22 Correct 6 ms 4960 KB Output is correct
23 Correct 5 ms 5180 KB Output is correct
24 Correct 5 ms 4960 KB Output is correct
25 Correct 4 ms 4960 KB Output is correct
26 Correct 4 ms 5128 KB Output is correct
27 Correct 5 ms 5112 KB Output is correct
28 Correct 5 ms 4948 KB Output is correct
29 Correct 5 ms 4948 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 5 ms 5112 KB Output is correct
32 Correct 5 ms 5160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5112 KB Output is correct
2 Correct 5 ms 5156 KB Output is correct
3 Correct 5 ms 4944 KB Output is correct
4 Correct 5 ms 4944 KB Output is correct
5 Correct 5 ms 4944 KB Output is correct
6 Correct 5 ms 4944 KB Output is correct
7 Correct 4 ms 5164 KB Output is correct
8 Correct 5 ms 5200 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 4 ms 4956 KB Output is correct
12 Correct 4 ms 4944 KB Output is correct
13 Correct 4 ms 5044 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 4 ms 4948 KB Output is correct
16 Correct 5 ms 4956 KB Output is correct
17 Correct 6 ms 4960 KB Output is correct
18 Correct 5 ms 4960 KB Output is correct
19 Correct 4 ms 4960 KB Output is correct
20 Correct 4 ms 4960 KB Output is correct
21 Correct 5 ms 4972 KB Output is correct
22 Correct 6 ms 4960 KB Output is correct
23 Correct 5 ms 5180 KB Output is correct
24 Correct 5 ms 4960 KB Output is correct
25 Correct 4 ms 4960 KB Output is correct
26 Correct 4 ms 5128 KB Output is correct
27 Correct 5 ms 5112 KB Output is correct
28 Correct 5 ms 4948 KB Output is correct
29 Correct 5 ms 4948 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 5 ms 5112 KB Output is correct
32 Correct 5 ms 5160 KB Output is correct
33 Correct 5 ms 5304 KB Output is correct
34 Correct 5 ms 5472 KB Output is correct
35 Correct 5 ms 5216 KB Output is correct
36 Correct 6 ms 5448 KB Output is correct
37 Correct 7 ms 5216 KB Output is correct
38 Correct 5 ms 5188 KB Output is correct
39 Correct 7 ms 5448 KB Output is correct
40 Correct 8 ms 5628 KB Output is correct
41 Correct 6 ms 5340 KB Output is correct
42 Correct 5 ms 5216 KB Output is correct
43 Correct 7 ms 5260 KB Output is correct
44 Correct 6 ms 5472 KB Output is correct
45 Correct 6 ms 5472 KB Output is correct
46 Correct 6 ms 5472 KB Output is correct
47 Correct 5 ms 5340 KB Output is correct
48 Correct 6 ms 5484 KB Output is correct
49 Correct 5 ms 5216 KB Output is correct
50 Correct 5 ms 5204 KB Output is correct
51 Correct 5 ms 5460 KB Output is correct
52 Correct 6 ms 5256 KB Output is correct
53 Correct 6 ms 5460 KB Output is correct
54 Correct 6 ms 5460 KB Output is correct
55 Correct 7 ms 5180 KB Output is correct
56 Correct 6 ms 5308 KB Output is correct
57 Correct 6 ms 5204 KB Output is correct
58 Correct 7 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Correct 4 ms 4944 KB Output is correct
5 Correct 6 ms 5200 KB Output is correct
6 Correct 5 ms 5480 KB Output is correct
7 Correct 66 ms 21540 KB Output is correct
8 Correct 83 ms 21320 KB Output is correct
9 Correct 92 ms 21856 KB Output is correct
10 Correct 5 ms 4948 KB Output is correct
11 Correct 4 ms 4948 KB Output is correct
12 Correct 4 ms 5204 KB Output is correct
13 Correct 12 ms 5204 KB Output is correct
14 Correct 69 ms 16540 KB Output is correct
15 Correct 240 ms 23180 KB Output is correct
16 Correct 5 ms 4948 KB Output is correct
17 Correct 5 ms 5228 KB Output is correct
18 Correct 105 ms 22196 KB Output is correct
19 Correct 5 ms 4960 KB Output is correct
20 Correct 4 ms 4960 KB Output is correct
21 Correct 5 ms 5216 KB Output is correct
22 Correct 6 ms 5380 KB Output is correct
23 Correct 59 ms 16480 KB Output is correct
24 Correct 219 ms 23204 KB Output is correct
25 Correct 5 ms 4948 KB Output is correct
26 Correct 5 ms 5460 KB Output is correct
27 Correct 102 ms 27824 KB Output is correct
28 Correct 73 ms 20392 KB Output is correct
29 Correct 78 ms 19256 KB Output is correct
30 Correct 5 ms 5112 KB Output is correct
31 Correct 5 ms 5156 KB Output is correct
32 Correct 5 ms 4944 KB Output is correct
33 Correct 5 ms 4944 KB Output is correct
34 Correct 5 ms 4944 KB Output is correct
35 Correct 5 ms 4944 KB Output is correct
36 Correct 4 ms 5164 KB Output is correct
37 Correct 5 ms 5200 KB Output is correct
38 Correct 4 ms 4944 KB Output is correct
39 Correct 4 ms 4944 KB Output is correct
40 Correct 4 ms 4956 KB Output is correct
41 Correct 4 ms 4944 KB Output is correct
42 Correct 4 ms 5044 KB Output is correct
43 Correct 4 ms 4948 KB Output is correct
44 Correct 4 ms 4948 KB Output is correct
45 Correct 5 ms 4956 KB Output is correct
46 Correct 6 ms 4960 KB Output is correct
47 Correct 5 ms 4960 KB Output is correct
48 Correct 4 ms 4960 KB Output is correct
49 Correct 4 ms 4960 KB Output is correct
50 Correct 5 ms 4972 KB Output is correct
51 Correct 6 ms 4960 KB Output is correct
52 Correct 5 ms 5180 KB Output is correct
53 Correct 5 ms 4960 KB Output is correct
54 Correct 4 ms 4960 KB Output is correct
55 Correct 4 ms 5128 KB Output is correct
56 Correct 5 ms 5112 KB Output is correct
57 Correct 5 ms 4948 KB Output is correct
58 Correct 5 ms 4948 KB Output is correct
59 Correct 4 ms 4948 KB Output is correct
60 Correct 5 ms 5112 KB Output is correct
61 Correct 5 ms 5160 KB Output is correct
62 Correct 5 ms 5304 KB Output is correct
63 Correct 5 ms 5472 KB Output is correct
64 Correct 5 ms 5216 KB Output is correct
65 Correct 6 ms 5448 KB Output is correct
66 Correct 7 ms 5216 KB Output is correct
67 Correct 5 ms 5188 KB Output is correct
68 Correct 7 ms 5448 KB Output is correct
69 Correct 8 ms 5628 KB Output is correct
70 Correct 6 ms 5340 KB Output is correct
71 Correct 5 ms 5216 KB Output is correct
72 Correct 7 ms 5260 KB Output is correct
73 Correct 6 ms 5472 KB Output is correct
74 Correct 6 ms 5472 KB Output is correct
75 Correct 6 ms 5472 KB Output is correct
76 Correct 5 ms 5340 KB Output is correct
77 Correct 6 ms 5484 KB Output is correct
78 Correct 5 ms 5216 KB Output is correct
79 Correct 5 ms 5204 KB Output is correct
80 Correct 5 ms 5460 KB Output is correct
81 Correct 6 ms 5256 KB Output is correct
82 Correct 6 ms 5460 KB Output is correct
83 Correct 6 ms 5460 KB Output is correct
84 Correct 7 ms 5180 KB Output is correct
85 Correct 6 ms 5308 KB Output is correct
86 Correct 6 ms 5204 KB Output is correct
87 Correct 7 ms 5332 KB Output is correct
88 Correct 136 ms 23704 KB Output is correct
89 Correct 140 ms 23596 KB Output is correct
90 Correct 169 ms 24872 KB Output is correct
91 Correct 65 ms 18984 KB Output is correct
92 Correct 60 ms 19088 KB Output is correct
93 Correct 78 ms 17388 KB Output is correct
94 Correct 68 ms 17368 KB Output is correct
95 Correct 244 ms 24624 KB Output is correct
96 Correct 199 ms 24508 KB Output is correct
97 Correct 205 ms 24692 KB Output is correct
98 Correct 192 ms 24560 KB Output is correct
99 Correct 76 ms 23600 KB Output is correct
100 Correct 88 ms 23576 KB Output is correct
101 Correct 46 ms 18856 KB Output is correct
102 Correct 47 ms 19276 KB Output is correct
103 Correct 92 ms 17388 KB Output is correct
104 Correct 74 ms 17304 KB Output is correct
105 Correct 259 ms 24364 KB Output is correct
106 Correct 309 ms 24792 KB Output is correct
107 Correct 251 ms 29164 KB Output is correct
108 Correct 229 ms 29408 KB Output is correct
109 Correct 88 ms 21788 KB Output is correct
110 Correct 90 ms 21624 KB Output is correct
111 Correct 127 ms 19572 KB Output is correct
112 Correct 108 ms 19692 KB Output is correct