Submission #1268359

#TimeUsernameProblemLanguageResultExecution timeMemory
1268359ducdevToll (BOI17_toll)C++17
25 / 100
3094 ms7232 KiB
// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

const int MAX_N = 5e4;
const int MOD = 1e9 + 7;
const int INF = 1e9;

template <class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x <= y) return false;
    x = y;
    return true;
};

int n, q, m, k;
vector<ii> queries;
vector<ii> adj[MAX_N + 5];

namespace SUBTASK_1 {
    const int N = MAX_N;
    const int K = 1;

    int numChains;
    int lab[N + 5];
    ll pref[N + 5];

    void dfs(int u) {
        lab[u] = numChains;

        for (ii e : adj[u]) {
            int v = e.first, w = e.second;
            pref[v] = pref[u] + w;
            dfs(v);
        };
    };

    void Solve() {
        for (int u = 0; u < n; u++) {
            if (!lab[u]) {
                ++numChains;
                dfs(u);
            };
        };

        for (const ii &q : queries) {
            int u = q.first, v = q.second;
            if (lab[u] != lab[v]) {
                cout << -1 << '\n';
            } else {
                cout << pref[v] - pref[u] << '\n';
            };
        };
    };
};  // namespace SUBTASK_1

namespace SUBTASK_234 {
    const int N = MAX_N;

    int ans[N + 5], dist[N + 5];
    bool needQuery[N + 5];
    vector<ii> qu[N + 5];

    bool checkSubtask() {
        if (q <= 3000) return true;
        for (const ii &query : queries)
            if (query.first != 0) return false;
        return true;
    };

    void dijkstra(int s, int cnt) {
        for (int u = 0; u < n; u++) dist[u] = INF;

        priority_queue<ii> pq;
        dist[s] = 0;
        pq.push(ii(-dist[s], s));

        while (!pq.empty()) {
            ii top = pq.top();
            pq.pop();
            int d = -top.first;
            int u = top.second;

            if (d > dist[u]) continue;
            if (needQuery[u]) cnt--, needQuery[u] = false;
            if (cnt == 0) break;

            for (ii e : adj[u]) {
                int v = e.first, w = e.second;
                if (minimize(dist[v], dist[u] + w)) {
                    pq.push(ii(-dist[v], v));
                };
            };
        };
    };

    void Solve() {
        for (int i = 0; i < q; i++) {
            int u = queries[i].first, v = queries[i].second;
            qu[u].push_back(make_pair(v, i));
        };

        for (int u = 0; u < n; u++) {
            if (qu[u].empty()) continue;

            int cnt = 0;
            for (const ii &q : qu[u]) {
                int v = q.first;
                if (!needQuery[v]) cnt++;
                needQuery[v] = true;
            };

            dijkstra(u, cnt);

            for (const ii &q : qu[u]) {
                int v = q.first, idx = q.second;
                needQuery[v] = false;
                ans[idx] = (dist[v] == INF ? -1 : dist[v]);
            };
        };

        for (int i = 0; i < q; i++) cout << ans[i] << '\n';
    };
};  // namespace SUBTASK_234

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen("MAIN.INP", "r")) {
        freopen("MAIN.INP", "r", stdin);
        freopen("MAIN.OUT", "w", stdout);
    };

    cin >> k >> n >> m >> q;
    while (m--) {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back(ii(v, w));
    };

    queries.resize(q);
    for (ii &query : queries) cin >> query.first >> query.second;
    
    if (k == 1)
      return SUBTASK_1::Solve(), 0;
    if (SUBTASK_234::checkSubtask())
      return SUBTASK_234::Solve(), 0;
};

Compilation message (stderr)

toll.cpp: In function 'int main()':
toll.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         freopen("MAIN.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |         freopen("MAIN.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...
#Verdict Execution timeMemoryGrader output
Fetching results...