Submission #1233373

#TimeUsernameProblemLanguageResultExecution timeMemory
1233373clemmy14Swapping Cities (APIO20_swap)C++20
0 / 100
796 ms589824 KiB
#include "swap.h" #include<bits/stdc++.h> using namespace std; int n; // weight then node vector<vector<pair<int, int>>> adj; void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) { n=N; adj = vector<vector<pair<int, int>>>(N); for(int i=0; i<M; i++) { adj[U[i]].push_back({W[i], V[i]}); adj[V[i]].push_back({W[i], U[i]}); } for(int i=0; i<N; i++) sort(adj[i].begin(), adj[i].end()); } int ans=0; set<pair<int, int>> noUse; vector<int> dijkstra(int s, int e) { vector<int> par(n, 0), dis(n, 1e9); dis[s]=0; vector<bool> vis(n, false); priority_queue<pair<int, int>> q; q.push({0, s}); while(!q.empty()) { int node = q.top().second; q.pop(); if(vis[node]) continue; vis[node]=true; if(node == e) { ans=dis[e]; break; } for(auto [w, child] : adj[node]) if(!noUse.count({node, child})) { if(dis[child] > max(dis[node], w)) { dis[child]=max(dis[node], w); par[child]=node; q.push({-dis[child], child}); } } } vector<int> path; path.push_back(e); for(int i=e; i!=s; i=par[i]) { path.push_back(par[i]); } //reverse(path.begin(), path.end()); return path; } int getMinimumFuelCapacity(int X, int Y) { ans=-1; vector<int> path=dijkstra(X, Y); // for(auto x : path) cout << x << ' '; // cout << endl; int newAns=-1; for(int i=0; i<path.size(); i++) { if(path[i] == X || path[i] == Y) { if(adj[path[i]].size() > 2) { if(adj[path[i]][2].first < ans) { if(newAns != -1) newAns=min(newAns, adj[path[i]][1].first); else newAns = adj[path[i]][1].first; } else { if(newAns != -1) newAns=min(newAns, adj[path[i]][2].first); else newAns = adj[path[i]][2].first; } } } else { bool f=false; for(int j=0; j<adj[path[i]].size(); j++) { if(!f && adj[path[i]][j].first == ans) f=true; else { if(newAns != -1) newAns=min(newAns, adj[path[i]][j].first); else newAns = adj[path[i]][j].first; break; } } } } noUse.clear(); for(int i=1; i<path.size(); i++) { noUse.insert({path[i], path[i-1]}); noUse.insert({path[i-1], path[i]}); } int oldAns=ans; ans=-1; dijkstra(X, Y); noUse.clear(); if(newAns == -1) newAns=ans; else if(ans != -1) newAns=min(newAns, ans); if(newAns == -1) return -1; return max(newAns, oldAns); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...