Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : _example
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 21 November 2022 [21:41]
- */
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- int node,weight;
- };
- vector<A > g[100010];
- struct B{
- int currNode;
- long long sumWeight;
- bool operator < (const B&o) const{
- return sumWeight > o.sumWeight;
- }
- };
- priority_queue<B > heap;
- long long disA[100010],disB[100010],disC[100010];
- bool path[100010];
- int main(){
- cin.tie(0)->sync_with_stdio(0);
- cin.exceptions(cin.failbit);
- int n,m,u,v,w,a,b,c,d;
- cin >> n >> m;
- for(int i=1;i<=m;i++){
- cin >> u >> v >> w;
- g[u].push_back({v,w});
- g[v].push_back({u,w});
- }
- cin >> a >> b >> c >> d;
- for(int i=1;i<=n;i++)
- disA[i] = disB[i] = disC[i] = 1e18;
- disA[a] = 0;
- heap.push({a,0});
- while(!heap.empty()){
- B now = heap.top();
- heap.pop();
- for(auto x:g[now.currNode]){
- if(disA[x.node] > now.sumWeight + x.weight){
- disA[x.node] = now.sumWeight + x.weight;
- heap.push({x.node,disA[x.node]});
- }
- }
- }
- disB[b] = 0;
- heap.push({b,0});
- while(!heap.empty()){
- B now = heap.top();
- heap.pop();
- for(auto x:g[now.currNode]){
- if(disB[x.node] > now.sumWeight + x.weight){
- disB[x.node] = now.sumWeight + x.weight;
- heap.push({x.node,disB[x.node]});
- }
- }
- }
- long long shortest = disA[b];
- for(int i=1;i<=n;i++)
- if(disA[i] + disB[i] == shortest)
- path[i] = true;
- if(!path[c]){
- disC[c] = 0;
- heap.push({c,0});
- }
- while(!heap.empty()){
- B now = heap.top();
- heap.pop();
- for(auto x:g[now.currNode]){
- if(disC[x.node] > now.sumWeight + x.weight && !path[x.node]){
- disC[x.node] = now.sumWeight + x.weight;
- heap.push({x.node,disC[x.node]});
- }
- }
- }
- if(disC[d] == 1e18) cout << "-1\n";
- else cout << disC[d] << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment