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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<long long, long long>;
using vi = vector<int>;
using vl = vector<long long>;
using vpi = vector<pair<int, int>>;
using vpl = vector<pair<long long, long long>>;
#define fur(i, a, b) for(ll i = a; i <= (ll)b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= (ll)b; --i)
#define fr first
#define sc second
#define mp make_pair
#define pb emplace_back
#define nl "\n"
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const ll mxn = 1e5L + 1;
const ll inf = 1e16L;
ll n, m, s, t, u, v;
vpl adj[mxn];
ll dist[mxn];
bool zer[mxn] = {};
void shortest_path() {
priority_queue<pl> pq;
fur(i, 0, n) {
dist[i] = inf;
}
bool proc[n + 1] = {};
dist[s] = 0;
pq.emplace(-dist[s], s);
while(!pq.empty()) {
ll a = pq.top().sc;
pq.pop();
if(proc[a])
continue;
proc[a] = 1;
for(auto [b, c] : adj[a]) {
if(dist[b] > dist[a] + c) {
dist[b] = dist[a] + c;
pq.emplace(-dist[b], b);
}
}
}
}
void nodes() {
priority_queue<pl> pq;
pq.emplace(dist[t], t);
while(!pq.empty()) {
ll a = pq.top().sc;
pq.pop();
if(zer[a])
continue;
zer[a] = 1;
for(auto [b, c] : adj[a]) {
if(dist[a] - c == dist[b]) {
pq.emplace(dist[b], b);
}
}
}
}
ll find_answer() {
ll len[n + 1];
bool proc[n + 1] = {};
fur(i, 0, n) {
len[i] = inf;
}
priority_queue<tuple<ll, ll, ll>> pq;
len[u] = 0;
pq.emplace(-len[u], u, 0);
while(!pq.empty()) {
ll d, a, ten, dir;
tie(d, a, ten) = pq.top();
pq.pop();
if(proc[a])
continue;
proc[a] = 1;
dir = ten % 2;
ten /= 2;
for(auto [b, c] : adj[a]) {
if(ten == 0) {
// use zero
if(zer[a] && zer[b]) {
if(dist[a] + c == dist[b]) {
len[b] = min(len[b], len[a]);
pq.emplace(-len[a], b, 3);
}
else if(dist[a] - c == dist[b]) {
len[b] = min(len[b], len[a]);
pq.emplace(-len[b], b, 2);
}
}
// dont use zero
if(len[a] + c < len[b]) {
len[b] = len[a] + c;
pq.emplace(-len[b], b, 0);
}
}
else if(ten == 1) {
// continue using
if(zer[a] && zer[b]) {
// check direction
if(dist[a] + c * (dir == 1 ? 1 : -1) == dist[b]) {
len[b] = min(len[b], len[a]);
pq.emplace(-len[b], b, 2 + dir);
}
}
// stop using
if(len[a] + c < len[b]) {
len[b] = len[a] + c;
pq.emplace(-len[b], b, 4);
}
}
else if(ten == 2) {
if(len[a] + c < len[b]) {
len[b] = len[a] + c;
pq.emplace(-len[b], b, 4);
}
}
}
}
return len[v];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> s >> t >> u >> v;
fur(i, 1, m) {
ll a, b, c;
cin >> a >> b >> c;
adj[a].pb(b, c);
adj[b].pb(a, c);
}
shortest_path();
nodes();
cout << find_answer();
}
# | 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... |