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>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 1e5 + 7;
vector<pii> v[N];
ll d[2][N], dp[N][3];
int n, m, s, t, x, y;
void dijkstra_dag(ll d[], int s) {
FOR(i, 1, n) d[i] = 1e18;
priority_queue<pli, vector<pli>, greater<pli>> pq;
d[s] = 0;
pq.push({d[s], s});
while (!pq.empty()) {
pli tmp = pq.top();
pq.pop();
ll w = tmp.X;
int u = tmp.Y;
if (w != d[u]) continue;
for (auto x : v[u])
if (minimize(d[x.X], w + x.Y)) pq.push({d[x.X], x.X});
}
}
bool onShortestPath(int st, int ed, int w) {
if (d[0][st] + d[1][ed] + w == d[0][t]) return true;
if (d[1][st] + d[0][ed] + w == d[0][t]) return true;
return false;
}
int main() {
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>s>>t>>x>>y;
FOR(i, 1, m) {
int x, y, c;
cin>>x>>y>>c;
v[x].push_back({y, c});
v[y].push_back({x, c});
}
dijkstra_dag(d[0], s);
dijkstra_dag(d[1], t);
FOR(i, 1, n)
FOR(j, 0, 2) dp[i][j] = 1e2;
dp[x][0] = 0;
priority_queue<plii, vector<plii>, greater<plii>> pq;
pq.push({0, {x, 0}});
while (!pq.empty()) {
plii tmp = pq.top();
pq.pop();
ll w = tmp.X;
int u = tmp.Y.X, mask = tmp.Y.Y;
if (w != dp[u][mask]) continue;
if (mask == 0) {
for (auto x : v[u]) {
if (minimize(dp[x.X][0], w + x.Y)) pq.push({dp[x.X][0], {x.X, 0}});
if (onShortestPath(u, x.X, x.Y) && minimize(dp[x.X][1], w)) pq.push({dp[x.X][1], {x.X, 1}});
}
}
else
if (mask == 1) {
for (auto x : v[u]) {
if (onShortestPath(u, x.X, x.Y)) {
if (minimize(dp[x.X][1], w)) pq.push({dp[x.X][1], {x.X, 1}});
}
else {
if (minimize(dp[x.X][2], w + x.Y)) pq.push({dp[x.X][2], {x.X, 2}});
}
}
}
else {
for (auto x : v[u])
if (minimize(dp[x.X][2], w + x.Y)) pq.push({dp[x.X][2], {x.X, 2}});
}
}
cout<<min({dp[y][0], dp[y][1], dp[y][2]});
return 0;
}
# | 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... |