Submission #472565

#TimeUsernameProblemLanguageResultExecution timeMemory
472565Hamed5001Toll (BOI17_toll)C++14
0 / 100
4 ms1484 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mxN = 5e4, OO = 2e16; ll preprocess[mxN]; bool vis[mxN]; ll COST[mxN]; vector<pair<ll, ll>> adj[mxN]; ll K, N, M, O; void pre(ll st) { priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq; pq.push({0, st}); preprocess[st] = 0; while(!pq.empty()) { ll node = pq.top().second; ll cost = pq.top().first; pq.pop(); for (auto c : adj[node]) { if (cost + c.first < preprocess[c.second]) { preprocess[c.second] = cost + c.first; pq.push({cost + c.first, c.second}); } } } } ll dijkstra(ll st, ll en) { for (int i = 0; i < N; i++) COST[i] = OO; priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq; pq.push({0, st}); COST[st] = 0; while(!pq.empty()) { ll node = pq.top().second; ll cost = pq.top().first; if (node == en) return cost; pq.pop(); for (auto c : adj[node]) { if (cost + c.first < COST[c.second]) { COST[c.second] = cost + c.first; pq.push({cost + c.first, c.second}); } } } return -1; } void solve() { cin >> K >> N >> M >> O; for (int i = 0; i < N; i++) preprocess[i] = OO; for (ll i = 0; i < M; i++) { ll u, v, t; cin >> u >> v >> t; adj[u].push_back({t, v}); } pre(0); while(O--) { ll a, b; cin >> a >> b; if (a == 0) { if (preprocess[b] == OO) preprocess[b] = -1; cout << preprocess[b] << endl; } else { cout << dijkstra(a, b) << endl; } } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(0); solve(); }

Compilation message (stderr)

toll.cpp: In function 'int main()':
toll.cpp:83:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |  freopen("input.txt", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:84:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |  freopen("output.txt", "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...