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