Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : _example
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 16 November 2022 [21:15]
- */
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- int nowNode,sumDistance,skipped;
- bool operator < (const A&o) const{
- return sumDistance > o.sumDistance;
- }
- };
- priority_queue<A > heap;
- struct B{
- int nextNode,edgeWeight;
- };
- vector<B > g[100010];
- int dis[100010][2];
- int main(){
- int n,m,s,t,u,v,w,sumWeight = 0;
- scanf("%d %d %d %d",&n,&m,&s,&t);
- for(int i=1;i<=m;i++){
- scanf("%d %d %d",&u,&v,&w);
- g[u].push_back({v,w});
- g[v].push_back({u,w});
- sumWeight+=w;
- }
- for(int i=0;i<n;i++)
- dis[i][0] = dis[i][1] = 1e9;
- dis[s][0] = 0;
- heap.push({s,0,0});
- while(!heap.empty()){
- A now = heap.top();
- heap.pop();
- for(auto x:g[now.nowNode]){
- //normal
- if(dis[x.nextNode][now.skipped] > now.sumDistance + x.edgeWeight){
- dis[x.nextNode][now.skipped] = now.sumDistance + x.edgeWeight;
- heap.push({x.nextNode,now.sumDistance + x.edgeWeight,now.skipped});
- }
- //special
- if(now.skipped == 0 && dis[x.nextNode][now.skipped+1] > now.sumDistance){
- dis[x.nextNode][now.skipped+1] = now.sumDistance;
- heap.push({x.nextNode,now.sumDistance,now.skipped+1});
- }
- }
- }
- printf("%d\n",sumWeight - dis[t][1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment