#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#define int long long
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define endl '\n'
#define all(v) v.begin(),v.end()
#define gcd(a,b) __gcd(a,b)
#define mt make_tuple
#define pqueue priority_queue
typedef pair<int,int> ii;
typedef tuple<int,int,int> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<iii> viii;
typedef set<int> si;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<si> vsi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
const int MOD = 1e9 + 7;
const int INF = 1e5 + 100;
struct Node{
int d,node,dist1,dist2;
bool operator<(const Node other) const {
return d > other.d;
}
};
int n,m,s,t,u,v;
vector<vii> adj;
vi distu,distv;
void djikstra(int basla,vi &dist){
pqueue<ii,vii,greater<ii>> pq;
pq.push({0,basla});
while (!pq.empty()){
int d,node; tie(d,node) = pq.top();
pq.pop();
if (dist[node] != -1) continue;
dist[node] = d;
for (auto pi : adj[node]){
int to,w; tie(to,w) = pi;
if (dist[to] != -1) continue;
pq.push({d + w,to});
}
}
}
int32_t main(){
cin >> n >> m >> s >> t >> u >> v;
s--; t--; u--; v--;
adj.assign(n + 10,vii());
distu.assign(n + 10,-1);
distv.assign(n + 10,-1);
for (int i = 0; i < m; i++){
int u,v,w; cin >> u >> v >> w;
u--; v--;
adj[u].eb(v,w);
adj[v].eb(u,w);
}
djikstra(u,distu);
djikstra(v,distv);
pqueue<Node> pq;
pq.push({0,s,(int) 1e18,(int) 1e18});
vi dist(n + 10,1e18);
vi ans(n + 10,1e18);
while (!pq.empty()){
Node nd = pq.top();
pq.pop();
int d = nd.d,node = nd.node,dist1 = nd.dist1,dist2 = nd.dist2;
if (dist[node] < d) continue;
dist[node] = d;
dist1 = min(dist1,distu[node]);
dist2 = min(dist2,distv[node]);
ans[node] = min(ans[node],dist1 + dist2);
for (auto pi : adj[node]){
int to,w; tie(to,w) = pi;
if (d + w > dist[to]) continue;
pq.push({d + w,to,dist1,dist2});
}
}
cout << min(ans[t],distu[v]) << endl;
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... |