Submission #1295273

#TimeUsernameProblemLanguageResultExecution timeMemory
1295273ionut27Commuter Pass (JOI18_commuter_pass)C++20
0 / 100
2103 ms243580 KiB
#include "bits/stdc++.h" #include <type_traits> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") // ============ Macros starts here ============ int recur_depth = 0; #ifdef DEBUG #define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;} #else #define dbg(x) #endif // DEBUG template<typename Ostream, typename Cont> typename enable_if<is_same<Ostream, ostream>::value, Ostream&>::type operator<<(Ostream& os, const Cont& v) { os << "{"; for (auto& x : v) { os << x << ", "; } return os << "}"; } template<typename Ostream, typename ...Ts> Ostream& operator<<(Ostream& os, const pair<Ts...>& p) { return os << "{" << p.first << ", " << p.second << "}"; } #define readFast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #ifdef LOCAL #define read() ifstream fin("date.in.txt") #else #define read() readFast #endif // LOCAL // ============ Macros ends here ============ #define fin cin #define ll long long #define sz(x) (int)(x).size() #define all(v) v.begin(), v.end() #define output(x) (((int)(x) && cout << "YES\n") || cout << "NO\n") #define LSB(x) (x & (-x)) #define test cout << "WORKS\n"; const int N = 1e5 + 15; const int MOD = 1e9 + 7; // vector<pair<int, int>> graf[N]; unordered_map<int, unordered_map<int, int>> graf; vector<int> from[N]; int n, m, S, T, U, V; vector<ll> djk(vector<int> start) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; vector<ll> dist(n, LLONG_MAX); for (int node : start) { que.push({ 0, node }); dist[node] = 0; } while (!que.empty()) { ll cost = que.top().first; int node = que.top().second; que.pop(); if (cost != dist[node]) { continue; } for (auto [to, w] : graf[node]) { if (dist[to] > cost + w) { dist[to] = cost + w; que.push({ dist[to], to }); from[to] = { node }; } else if (dist[to] == cost + w) { from[to].push_back(node); } } } return dist; } vector<ll> djk2(vector<int> start) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; vector<ll> dist(n, LLONG_MAX); for (int node : start) { que.push({ 0, node }); dist[node] = 0; } while (!que.empty()) { ll cost = que.top().first; int node = que.top().second; que.pop(); if (cost != dist[node]) { continue; } for (auto [to, w] : graf[node]) { if (dist[to] > cost + w) { dist[to] = cost + w; que.push({ dist[to], to }); } } } return dist; } int main() { read(); fin >> n >> m; fin >> S >> T; fin >> U >> V; --S, --T, --U, --V; for (int i = 0; i < m; ++i) { int a, b, c; fin >> a >> b >> c; --a, --b; graf[a][b] = c; graf[b][a] = c; } vector<ll> distST = djk({ S }); dbg(distST); queue<int> que_back; for (int to : from[T]) { que_back.push(to); } while (!que_back.empty()) { int node = que_back.front(); que_back.pop(); for (int to : from[node]) { graf[node][to] = 0; graf[to][node] = 0; que_back.push(to); } } vector<ll> distUV = djk2({ U }); dbg(distUV); cout << distUV[V] << '\n'; return 0; } /*stuff you should look for !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * test the solution with the given example * int overflow, array bounds, matrix bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH ~Benq~*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...