This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector <vector <pair <int, int>>> adj;
int N;
pair <int, ll> dijkstra(int s) {
priority_queue <pair <ll, int>> pq;
vector <ll> dis(N+1, 1e17);
vector <bool> processed(N+1);
dis[s] = 0;
pq.push({0, s});
int node = 0;
ll mx = 0;
while (!pq.empty()) {
s = pq.top().second;
pq.pop();
if (processed[s])
continue;
processed[s] = true;
if (mx < dis[s]) {
mx = dis[s];
node = s;
}
for (auto i : adj[s]) {
if (dis[i.first] > dis[s] + i.second) {
dis[i.first] = dis[s] + i.second;
pq.push({-dis[i.first], i.first});
}
}
}
return {node, mx};
}
ll find_shortcut(int n, vector<int> l, vector<int> d, int c)
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
adj.resize(n * 2 + 5);
for (int i = 0; i+1 < n; i++) {
adj[i].push_back({i+1, l[i]});
adj[i+1].push_back({i, l[i]});
}
int ogn = n;
for (int i = 0; i < ogn; i++) {
if (d[i] == 0)
continue;
adj[i].push_back({n, d[i]});
adj[n].push_back({i, d[i]});
n++;
}
N = n;
pair <int, ll> res;
ll ans = 0;
for (int i = 0; i < n; i++) {
res = dijkstra(i);
res = dijkstra(res.first);
ans = max(ans, res.second);
}
for (int i = 0; i < ogn; i++) {
for (int j = i+1; j < ogn; j++) {
adj[i].push_back({j, c});
adj[j].push_back({i, c});
ll tp = 0;
for (int k = 0; k < n; k++) {
res = dijkstra(k);
res = dijkstra(res.first);
tp = max(tp, res.second);
}
ans = min(ans, tp);
adj[i].pop_back();
adj[j].pop_back();
}
}
return ans;
}
// int main() {
// int n;
// cin >> n;
// vector <int> l(n), d(n);
// for (int i = 0; i < n-1; i++)
// cin >> l[i];
// for (int i = 0; i < n; i++)
// cin >> d[i];
// int c;
// cin >> c;
// cout << find_shortcut(n, l, d, c) << '\n';
// }
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |