Submission #503060

#TimeUsernameProblemLanguageResultExecution timeMemory
503060XIIRobot (JOI21_ho_t4)C++17
100 / 100
783 ms81860 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define fi first #define se second #define mp make_pair #define eb emplace_back #define ALL(x) (x).begin(), (x).end() #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define FORU(i, a, b) for(int i = (a); i <= (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define IOS cin.tie(0)->sync_with_stdio(false); #define PROB "JOI21_ho_t4" void Fi(){ if(fopen(PROB".inp", "r")){ freopen(PROB".inp", "r", stdin); freopen(PROB".out", "w", stdout); } } const int N = 1e5 + 1; const ll INF = 1e18; int n, m; using pi = pair<int, int>; map<int, vector<pi>> adj[N]; map<int, ll> tot[N], dp2[N]; ll dp[N]; using T = pair<ll, pair<int, int>>; struct cmp{ const bool operator()(const T &a, const T &b) const { return a.fi > b.fi; } }; priority_queue<T, vector<T>, cmp> pq; //priority_queue<T, vector<T>, greater<T>> pq; ll dijkstra(const int &s, const int &e){ fill(dp + 1, dp + n + 1, INF); pq.emplace(0, mp(s, 0)); dp[s] = 0; while(!pq.empty()){ ll wu = pq.top().fi; auto [u, cu] = pq.top().se; pq.pop(); if(cu){ if(dp2[u][cu] != wu) continue; for(auto [v, wv]: adj[u][cu]){ ll w = wu + (tot[u][cu] - wv); if(dp[v] > w){ dp[v] = w; pq.emplace(dp[v], mp(v, 0)); } } } else{ if(dp[u] != wu) continue; for(auto [cv, ed]: adj[u]){ for(auto [v, wv]: ed){ ll case1 = wu + wv; if(dp[v] > case1){ dp[v] = case1; pq.emplace(dp[v], mp(v, 0)); } ll case2 = wu + (tot[u][cv] - wv); if(dp[v] > case2){ dp[v] = case2; pq.emplace(dp[v], mp(v, 0)); } ll case3 = wu; if(!dp2[v].count(cv) || dp2[v][cv] > case3){ dp2[v][cv] = case3; pq.emplace(dp2[v][cv], mp(v, cv)); } } } } } return (dp[e] == INF ? -1 : dp[e]); } int main(){ IOS; Fi(); cin >> n >> m; FORU(i, 1, m){ int u, v, c, p; cin >> u >> v >> c >> p; adj[u][c].eb(v, p); adj[v][c].eb(u, p); tot[u][c] += p; tot[v][c] += p; } cout << dijkstra(1, n); return 0; }

Compilation message (stderr)

Main.cpp: In function 'void Fi()':
Main.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen(PROB".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen(PROB".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...