#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
x = 0;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar());
for (; c >= '0' && c <= '9'; c = getchar())
x = (x << 1) + (x << 3) + (c ^ 48);
}
const int N = 5e5 + 5, M = 1e6 + 5;
int n, m, s, t, w;
set<int> e[N];
int lin[N], nxt[M << 1], to[M << 1], tot;
void in(int x, int y) {
nxt[++tot] = lin[x];
lin[x] = tot;
to[tot] = y;
}
int dis[N], fr[N];
bool vis[N];
priority_queue< pair<int, int>> q;
void dij() {
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
q.push(make_pair(0, s));
while (q.size()) {
int x = q.top().second;
q.pop();
if (vis[x])
continue;
vis[x] = 1;
for (int i = lin[x]; i; i = nxt[i]) {
int y = to[i];
if (dis[x] + 1 < dis[y]) {
dis[y] = dis[x] + 1;
fr[y] = x;
if (!vis[y])
q.push(make_pair(-dis[y], y));
}
}
}
}
int fa[N];
inline int get(int x) {
return x == fa[x] ? x : (fa[x] = get(fa[x]));
}
int main() {
freopen("road.in", "r", stdin);
freopen("road.out", "w", stdout);
read(n);
read(m);
for (int i = 1; i <= m; i++) {
int x, y;
read(x);
read(y);
read(w);
in(x, y);
in(y, x);
e[x].insert(y);
e[y].insert(x);
}
read(s);
read(t);
dij();
if (dis[t] == 0x3f3f3f3f)
return printf("-1\n"), 0;
int p = t, q = fr[t];
while (p != s) {
e[p].erase(q);
e[q].erase(p);
p = q;
q = fr[p];
}
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= n; i++) {
for (auto p = e[i].begin(); p != e[i].end(); ++p) {
int x = get(i), y = get(*p);
if (x != y)
fa[y] = x;
}
}
bool f = 1;
for (int i = 1; i < n; i++)
f &= (get(i) == get(i + 1));
if (!f)
printf("-1\n");
else
printf("%lld\n", 1ll * dis[t]*w);
return 0;
}
请各位大佬指教!!!谢谢!!!