This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author: MisterReaper (Ahmet Alp Orakci)
#include <bits/stdc++.h>
using namespace std;
#define Data pair <Best, int>
#define ONLINE_JUDGE
const int MAXN = 1e5 + 5;
const long long INF = 1e18;
#ifndef ONLINE_JUDGE
#include "debug.h"
#define OPEN freopen(".in", "r", stdin); freopen(".out", "w", stdout);
#define TIME cerr << fixed << setprecision(2) << 1000.0 * clock() / CLOCKS_PER_SEC << " milliseconds ";
#else
#define debug(...) void(23)
#define OPEN void(0000)
#define TIME void(232323233)
#endif
struct Best
{
long long val1, val2;
Best(long long a = INF, long long b = INF) : val1(a), val2(b) {}
void add(long long x)
{
if(x <= val1)
{
val2 = val1;
val1 = x;
}
else if(x <= val2)
{
val2 = x;
}
}
bool ok() {return val1 != INF && val2 != INF;}
};
bool operator> (Best a, Best b)
{
return (a.val2 != b.val2) ? a.val2 > b.val2 : a.val1 > b.val1;
}
bool operator< (Best a, Best b)
{
return (a.val2 != b.val2) ? a.val2 < b.val2 : a.val1 < b.val1;
}
bool operator== (Best a, Best b)
{
return a.val1 == b.val1 && a.val2 == b.val2;
}
bool operator!= (Best a, Best b)
{
return !(a == b);
}
vector <pair <int, int>> graph[MAXN];
Best dists[MAXN];
int solve(int n, int m, int (*edges)[2], int* l, int k, int *entry)
{
for(int i = 1; i <= m; i++)
{
int u = edges[i -1][0], v = edges[i -1][1], c = l[i -1];
graph[u].emplace_back(v, c);
graph[v].emplace_back(u, c);
}
priority_queue <Data, vector <Data>, greater <Data>> pq;
for(int i = 1; i <= k; i++)
{
int x = entry[i -1];
dists[x] = {0, 0};
pq.emplace(dists[x], x);
}
while(!pq.empty())
{
Best val = pq.top().first; int node = pq.top().second;
pq.pop();
if(dists[node] != val) continue;
debug(node);
for(auto [child, c] : graph[node])
{
int cost = val.val2 + c;
Best childval = dists[child]; childval.add(cost);
debug(childval.val1, childval.val2);
if(childval < dists[child])
{
dists[child] = childval;
if(dists[child].ok())
{
pq.emplace(childval, child);
}
}
}
}
if(dists[0].val2 == INF)
{
cout << "BAD";
return -1;
}
return dists[0].val2;
}
int travel_plan(int n, int m, int (*r)[2], int* l, int k, int *p)
{
return solve(n, m, r, l, k, p);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |