#include <bits/stdc++.h>
#define ll long long
#define pr pair <ll, int>
#define fi first
#define se second
using namespace std;
const int N = 1e5 + 5;
struct haha
{
ll val;
int ver, type, g;
};
struct cmp
{
bool operator() (const haha &a, const haha &b)
{
return a.val > b.val;
}
};
int n, m, s, t, U, V;
vector <pr> a[N], g_s[N], g_t[N];
ll dist[N][5], d_s[N], d_t[N];
void inp()
{
cin >> n >> m >> s >> t >> U >> V;
for (int i = 1; i <= m; i++)
{
int x, y;
ll w;
cin >> x >> y >> w;
a[x].push_back({w, y});
a[y].push_back({w, x});
}
}
void dijkstra_ST()
{
priority_queue<pr, vector<pr>, greater<pr>> q;
q.push({0, s});
memset(d_s, 0x3f, sizeof(d_s));
d_s[s] = 0;
while (!q.empty())
{
int u = q.top().se;
ll k = q.top().fi;
q.pop();
if (k > d_s[u])
continue;
for (pr i : a[u])
{
int v = i.se;
ll val = k + i.fi;
if (d_s[v] > val)
{
d_s[v] = val;
q.push({val, v});
}
}
}
}
void dijkstra_TS()
{
priority_queue<pr, vector<pr>, greater<pr>> q;
q.push({0, t});
memset(d_t, 0x3f, sizeof(d_t));
d_t[t] = 0;
while (!q.empty())
{
int u = q.top().se;
ll k = q.top().fi;
q.pop();
if (k > d_t[u])
continue;
for (pr i : a[u])
{
int v = i.se;
ll val = k + i.fi;
if (d_t[v] > val)
{
d_t[v] = val;
q.push({val, v});
}
}
}
}
vector<pr> *getTmp(int g, int u)
{
if (g == 1)
return &g_t[u];
if (g == 2)
return &g_s[u];
return &a[u];
}
void dijkstra_UV ()
{
priority_queue <haha, vector<haha>, cmp> q;
memset(dist, 0x3f, sizeof(dist));
dist[U][0] = dist[U][1] = dist[U][2] = 0;
if (d_s[t] == d_s[U] + d_t[U])
{
q.push({0, U, 1, 2});
q.push({0, U, 1, 1});
}
else
q.push({0, U, 0, 0});
while (!q.empty())
{
int u = q.top().ver, type = q.top().type, g = q.top().g;
ll k = q.top().val;
//cout << u << " " << type << " " << g << " " << k << "\n";
q.pop();
if (k > dist[u][type])
continue;
vector <pr> *tmp = getTmp(g, u);
for (pr i : *tmp)
{
ll val = k + i.fi;
int v = i.se;
if (type == 0)
{
int graph = 0;
if (d_s[t] == d_s[v] + d_t[v])
graph = 2;
// cout << u << " " << v << " " << graph << "\n";
if (graph && dist[v][1] > val)
{
q.push({val, v, 1, 1});
q.push({val, v, 1, 2});
dist[v][1] = val;
}
if (dist[v][0] > val)
{
q.push({val, v, 0, 0});
dist[v][0] = val;
}
}
if(type == 1)
{
if (dist[v][1] > dist[u][1])
{
q.push({dist[u][1], v, 1, g});
dist[v][1] = dist[u][1];
}
}
if (type == 2)
{
if (dist[v][2] > val)
{
dist[v][2] = val;
q.push({val, v, 2, 0});
}
}
}
if (type == 1)
{
for (pr i : a[u])
{
int v = i.se;
ll val = i.fi + k;
if (dist[v][2] > val)
{
q.push({val, v, 2, 0});
dist[v][2] = val;
}
}
}
}
}
void trace(int u, ll d[], int pre, int type)
{
for (pr i : a[u])
{
int v = i.se;
if (d[u] == d[v] + i.fi && v != pre && d_s[t] == d_s[v] + d_t[v])
{
if (type == 1)
g_s[v].push_back({i.fi, u});
else
g_t[v].push_back({i.fi, u});
trace(v, d, u, type);
}
}
}
void solve()
{
dijkstra_ST();
dijkstra_TS();
trace(t, d_s, -1, 1);
trace(s, d_t, -1, 2);
// for (int i = 1; i <= n; i++)
// {
// cout << i << " ";
// for (pr j : g_t[i])
// cout << j.se << " ";
// cout << "\n";
// }
dijkstra_UV();
cout << (min({dist[V][1], dist[V][2], dist[V][0]}) > 1e18 ? - 1 : min({dist[V][1], dist[V][2], dist[V][0]}));
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
inp();
solve();
}
| # | 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... |