제출 #409494

#제출 시각아이디문제언어결과실행 시간메모리
409494534351Robot (JOI21_ho_t4)C++17
100 / 100
1672 ms114684 KiB
#include <bits/stdc++.h> using namespace std; template<class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template<class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } #define MP make_pair #define PB push_back #define LB lower_bound #define UB upper_bound #define fi first #define se second #define SZ(x) ((int) (x).size()) #define ALL(x) (x).begin(), (x).end() #define FOR(i, a, b) for (auto i = (a); i < (b); i++) #define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--) const long long LLINF = 3e18; const int MAXN = 200013; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpi; typedef vector<pll> vpl; int N, M; map<int, vpi> edge[MAXN]; int color[MAXN]; int cost[MAXN]; map<int, ll> sum[MAXN]; //you can only save it if its color is unique or its color is twice and you just used one of them & paid. ll dist[MAXN]; map<int, ll> special[MAXN]; priority_queue<array<ll, 3>, vector<array<ll, 3> >, greater<array<ll, 3> > > pq; ll ans; //distance, vertex, edge int32_t main() { cout << fixed << setprecision(12); cerr << fixed << setprecision(4); ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> M; FOR(i, 0, M) { int u, v, c, d; cin >> u >> v >> c >> d; u--; v--; edge[u][c].PB({v, i}); edge[v][c].PB({u, i}); sum[u][c] += d; sum[v][c] += d; color[i] = c; cost[i] = d; // edge[u].PB({v, c, d}); // edge[v].PB({u, c, d}); } FOR(u, 0, N) { for (auto &p : edge[u]) { sort(ALL(p.se), [&](pii a, pii b) { return cost[a.se] > cost[b.se]; }); // for (auto e : p.se) // { // cerr << "EDGE " << u << ' ' << e.fi << ' ' << " color " << p.fi << " dis " << e.se << endl; // } } } fill(dist, dist + N, LLINF); dist[0] = 0; pq.push({0, 0, -1}); while(!pq.empty()) { ll d = pq.top()[0]; int u = pq.top()[1], c = pq.top()[2]; pq.pop(); if (c != -1) { if (d != special[u][c]) continue; int cl = color[c]; int it = 0; for (auto e : edge[u][cl]) //you can take an edge for free if you just came here w edge of that color && deg is 2. { // #warning cut time complexity by limiting it to two iterations? int v = e.fi, eid = e.se; ll dis = cost[eid]; ll nd = d + min(dis, sum[u][cl] - dis - cost[c]); if (dist[v] > nd) { dist[v] = nd; pq.push({nd, v, -1}); } it++; if (it > 3) break; // if (special[v].find(eid) == special[v].end() || special[v]) } } else { if (d != dist[u]) continue; for (auto p : edge[u]) { int cl = p.fi; for (auto e : p.se) { int v = e.fi, eid = e.se; ll dis = cost[eid]; //no guarantees -> you just need to make this the unique color from u. ll nd = d + min(dis, sum[u][cl] - dis); // ll nd = d + dis; if (dist[v] > nd) { dist[v] = nd; pq.push({nd, v, -1}); } //but also set the special distance. here you have to actually pay to set this. nd = d + dis; if ((special[v].find(eid) == special[v].end() || special[v][eid] > nd)) { special[v][eid] = nd; pq.push({nd, v, eid}); } } } } } ans = dist[N - 1]; if (ans >= LLINF) ans = -1; cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...