제출 #650164

#제출 시각아이디문제언어결과실행 시간메모리
650164HaYoungJoonRobot (JOI21_ho_t4)C++14
0 / 100
3059 ms79560 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define INF 1e18 using namespace std; const int maxn = 1e5 + 1; typedef pair<ll,ll> pii; typedef pair<ll,pii> piii; typedef pair<ll,piii> piiii; unordered_map<int,ll> dp[maxn], cost[maxn]; vector<piii> adj[maxn]; int n,m; ll dijsktra() { for (int i = 1; i <= n; i++) { dp[i][0] = INF; for (piii j: adj[i]) { int c = j.se.fi; dp[i][c] = INF; } } dp[1][0] = 0; priority_queue<piiii, vector<piiii>, greater<piiii> > pq; pq.push(piiii(0,piii(0,pii(1,0)))); while (!pq.empty()) { int u = pq.top().se.se.fi, oldcol = pq.top().se.se.se; ll D = pq.top().fi, used = - pq.top().se.fi; pq.pop(); if (D > dp[u][oldcol]) continue; if (u == n) return D; for (piii i: adj[u]) { int v = i.fi, newcol = i.se.fi; ll w = i.se.se; if (oldcol == 0) { /// Ko doi mau (u,v) -> doi mau cac canh con lai if (dp[v][0] > D + cost[u][newcol] - w) { dp[v][0] = D + cost[u][newcol] - w; pq.push(piiii(dp[v][0],piii(0,pii(v,0)))); } /// /// Doi mau (u,v) if (dp[v][newcol] > D + w) { dp[v][newcol] = D + w; pq.push(piiii(dp[v][newcol],piii(-w,pii(v,newcol)))); } } else { /// Ko doi mau (u,v) -> doi mau cac canh con lai if (dp[v][0] > D + cost[u][newcol] - w - (newcol == oldcol)*used) { dp[v][0] = D + cost[u][newcol] - w - (newcol == oldcol)*used; pq.push(piiii(dp[v][0],piii(0,pii(v,0)))); } /// /// Doi mau (u,v) if (dp[v][newcol] > D + w) { dp[v][newcol] = D + w; pq.push(piiii(dp[v][newcol],piii(-w,pii(v,newcol)))); } /// Doi mau (u,v) va cac canh con lai if (dp[v][newcol] > D + cost[u][newcol] - (newcol == oldcol)*used) { dp[v][newcol] = D + w; pq.push(piiii(dp[v][newcol],piii(-w,pii(v,newcol)))); } } } } return -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { ll u,v,c,w; cin >> u >> v >> c >> w; adj[u].push_back(piii(v,pii(c,w))); adj[v].push_back(piii(u,pii(c,w))); cost[u][c] += w; cost[v][c] += w; } cout << dijsktra(); return 0; } /* 6 4 1 2 1 1000000000 2 3 1 1000000000 3 4 1 1000000000 4 5 1 1000000000 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...