Submission #1081416

#TimeUsernameProblemLanguageResultExecution timeMemory
1081416IcelastRobot (JOI21_ho_t4)C++17
100 / 100
748 ms158956 KiB
#include <iostream> #include <bits/stdc++.h> #define ll long long using namespace std; const ll maxn = 2*1e5+5, INF = 4e18+9; vector<ll> dijkstra(int s, vector<vector<pair<ll, ll>>> &adj){ int n = adj.size(); vector<ll> dist(n+1, INF); dist[s] = 0; vector<bool> vis(n+1, false); priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq; pq.push({0, s}); while(!pq.empty()){ ll d_u = pq.top().first; int u = pq.top().second; pq.pop(); if(vis[u]){ continue; } vis[u] = true; for(auto it : adj[u]){ int v = it.first; ll w = it.second; if(vis[v]) continue; if(dist[v] > d_u+w){ dist[v] = d_u+w; pq.push({dist[v], v}); } } } return dist; } bool spfa(int s, vector<ll>& d, vector<vector<pair<ll, ll>>> adj) { int n = adj.size(); d.assign(n+1, INF); vector<int> cnt(n+1, 0); vector<bool> inqueue(n+1, false); queue<int> q; d[s] = 0; q.push(s); inqueue[s] = true; while (!q.empty()) { int v = q.front(); q.pop(); inqueue[v] = false; for (auto edge : adj[v]) { int to = edge.first; ll len = edge.second; if (d[v] + len < d[to]) { d[to] = d[v] + len; if (!inqueue[to]) { q.push(to); inqueue[to] = true; cnt[to]++; if (cnt[to] > n) return false; // negative cycle } } } } return true; } struct road{ ll v, c, p; }; void solve(){ int n, m; cin >> n >> m; vector<vector<road>> adj(n+1); for(int i = 1; i <= m; i++){ int u, v, c, p; cin >> u >> v >> c >> p; adj[u].push_back({v, c, p}); adj[v].push_back({u, c, p}); } vector<bool> vis(n+1, false); auto dfs = [&](auto dfs, int u) -> void{ vis[u] = true; for(auto it : adj[u]){ int v = it.v; if(vis[v]) continue; dfs(dfs, v); } }; dfs(dfs, 1); if(!vis[n]){ cout << -1; return; } vector<map<int, int>> mp(n+1), nn(n+1); vector<map<int, vector<int>>> to(n+1); vector<map<ll, ll>> sum(n+1); int N = n; for(int i = 1; i <= n; i++){ for(auto it : adj[i]){ mp[i][it.c]++; to[i][it.c].push_back(it.v); sum[i][it.c]+=it.p; if(nn[i][it.c] == 0){ N++; nn[i][it.c] = N; } } } vector<vector<pair<ll, ll>>> adj2(N+1); for(int i = 1; i <= n; i++){ for(auto it : adj[i]){ adj2[i].push_back({it.v, it.p}); adj2[i].push_back({nn[i][it.c], 0}); adj2[nn[i][it.c]].push_back({it.v, sum[i][it.c]-it.p}); adj2[i].push_back({nn[it.v][it.c], 0}); } } vector<ll> dist(N+1, INF); dist = dijkstra(1, adj2); cout << dist[n]; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...