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;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
CTL - Da Lat
Cay ngay cay dem nhung deo duoc cong nhan
*/
struct Node {
int u, stat;
ll du;
Node() {}
Node(int u, int stat, ll du) : u(u), stat(stat), du(du) {}
};
struct cmp {
bool operator ()(Node a, Node b) const {
return a.du > b.du;
}
};
const ll oo = 1e18;
const int N = 1e5 + 1;
int n, m, s, t, u, v;
vpi adj[N];
ll d[N][4], d1[2][N];
void dijkstra(int source, ll d[]) {
priority_queue<pll, vpll, greater<pll> > pq;
FOR(i, 1, n) d[i] = oo;
d[source] = 0;
pq.push({0, source});
WHILE(!pq.empty()) {
pll cur = pq.top();
pq.pop();
int u = cur.nd;
ll du = cur.st;
if (du > d[u]) continue;
EACH(j, adj[u]) {
int v = j.st, w = j.nd;
if (d[v] > du + w) {
d[v] = du + w;
pq.push({d[v], v});
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> m;
cin >> s >> t;
cin >> u >> v;
FOR(i, 1, m) {
int a, b, c;
cin >> a >> b >> c;
adj[a].pb({b, c});
adj[b].pb({a, c});
}
dijkstra(s, d1[0]);
dijkstra(t, d1[1]);
// FOR(i, 1, n) cerr << d1[0][i] << ' ';
// cerr << '\n';
// FOR(i, 1, n) cerr << d1[1][i] << ' ';
// cerr << '\n';
priority_queue<Node, vector<Node>, cmp> pq;
FOR(i, 1, n)
FOR(j, 0, 3)
d[i][j] = oo;
d[u][0] = 0;
pq.push(Node(u, 0, 0));
WHILE(!pq.empty()) {
Node tmp = pq.top();
pq.pop();
int u = tmp.u, stat = tmp.stat;
ll du = tmp.du;
// debug(u, stat, du);
if (du > d[u][stat]) continue;
EACH(j, adj[u]) {
int v = j.st, w = j.nd;
if (!stat) {
if (d[v][0] > du + w) {
d[v][0] = du + w;
pq.push(Node(v, 0, d[v][0]));
}
if (d1[0][u] + w + d1[1][v] == d1[0][t] && d[v][1] > du) {
d[v][1] = du;
pq.push(Node(v, 1, d[v][1]));
}
if (d1[0][v] + w + d1[1][u] == d1[0][t] && d[v][2] > du) {
d[v][2] = du;
pq.push(Node(v, 2, d[v][2]));
}
}
else if (stat == 1) {
if (d1[1][u] == w + d1[1][v] && d[v][1] > du) {
d[v][1] = du;
pq.push(Node(v, 1, d[v][1]));
}
if (d[v][3] > du + w) {
d[v][3] = du + w;
pq.push(Node(v, 3, d[v][3]));
}
}
else if (stat == 2) {
if (d1[0][u] == w + d1[0][v] && d[v][2] > du) {
d[v][2] = du;
pq.push(Node(v, 2, d[v][2]));
}
if (d[v][3] > du + w) {
d[v][3] = du + w;
pq.push(Node(v, 3, d[v][3]));
}
}
else if (d[v][3] > du + w) {
d[v][3] = du + w;
pq.push(Node(v, 3, d[v][3]));
}
}
}
cout << min({d[v][0], d[v][1], d[v][2], d[v][3]});
return 0;
}
/*
8 8
5 7
6 8
1 2 2
2 3 3
3 4 4
1 4 1
1 5 5
2 6 6
3 7 7
4 8 8
*/
Compilation message (stderr)
commuter_pass.cpp: In function 'void dijkstra(int, ll*)':
commuter_pass.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
commuter_pass.cpp:54:2: note: in expansion of macro 'FOR'
54 | FOR(i, 1, n) d[i] = oo;
| ^~~
commuter_pass.cpp:26:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
commuter_pass.cpp:63:3: note: in expansion of macro 'EACH'
63 | EACH(j, adj[u]) {
| ^~~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
commuter_pass.cpp:79:5: note: in expansion of macro 'FOR'
79 | FOR(i, 1, m) {
| ^~~
commuter_pass.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
commuter_pass.cpp:92:5: note: in expansion of macro 'FOR'
92 | FOR(i, 1, n)
| ^~~
commuter_pass.cpp:24:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
commuter_pass.cpp:93:5: note: in expansion of macro 'FOR'
93 | FOR(j, 0, 3)
| ^~~
commuter_pass.cpp:26:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
commuter_pass.cpp:104:6: note: in expansion of macro 'EACH'
104 | EACH(j, adj[u]) {
| ^~~~
# | 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... |