Advertisement
remember_Me

Untitled

Aug 6th, 2020
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x)                x.begin(),x.end()
  3. #define F                     first
  4. #define pb                    push_back
  5. #define S                     second
  6. #define IOfast                ios_base::sync_with_stdio(false); cin.tie(NULL);
  7. #define debug(x)              cout<<#x<<" = "<<x<<"\n";
  8. #define error(args...)        { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
  9. using namespace std;
  10.  
  11. vector < pair<int64_t,int> > graph[(int)1e5 + 1];
  12. vector<int64_t> mx((int)1e5+1,0); //max in the path //i am storing max so in case it changes i can edit my distance
  13. vector<int64_t> dist((int)1e5 + 1,LLONG_MAX / 100LL);
  14.  
  15. void diskstra(int n)
  16. {
  17.   set<pair<int64_t,int>> s = {{0,1}};
  18.   dist[1] = 0;
  19.   mx[1] = 0; //max from one to
  20.  
  21.   while(s.size())
  22.   {
  23.     int64_t value = (*s.begin()).F;
  24.     int vertex = (*s.begin()).S;
  25.     s.erase(s.begin());
  26.  
  27.     for(auto child:graph[vertex])
  28.     {
  29.       //vetex -> child
  30.       //creating a new distance by discarding old coupon and creating new discount coupon as child.F > mx[vertex]    
  31.       if(child.F > mx[vertex] && dist[child.S] > value + ((mx[vertex]+1)/2) + (child.F/2) )
  32.       {
  33.         s.erase({dist[child.S],child.S});
  34.         dist[child.S] =  value + ((mx[vertex]+1)/2) + (child.F/2);
  35.         s.insert({dist[child.S],child.S});
  36.         mx[child.S] = child.F;
  37.       }
  38.       if(dist[child.S] >= dist[vertex] + child.F)
  39.       {
  40.         s.erase({dist[child.S],child.S});
  41.         //if the distance are same then minimize the maximum in the path
  42.         //so if new edge comes with greater value i can discard old maximum
  43.         // and create new discount coupon with minimum total
  44.         if(dist[child.S] == dist[vertex] + child.F)
  45.           mx[child.S] = min(mx[child.S],mx[vertex]);
  46.         else
  47.           mx[child.S] = mx[vertex];
  48.         dist[child.S] = dist[vertex] + child.F;
  49.         s.insert({dist[child.S],child.S});
  50.       }
  51.     }
  52.   }
  53.   cout<<dist[n];
  54. }
  55.  
  56. void solve()
  57. {
  58.   int n,m,u,v,cost;
  59.   cin>>n>>m;
  60.   while(m--)
  61.   {
  62.     cin>>u>>v>>cost;
  63.     graph[u].pb({cost,v});
  64.   }
  65.   diskstra(n);
  66. }
  67.  
  68. int main()
  69. {
  70.   IOfast;
  71.   solve();
  72.   return 0;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement