Advertisement
shawon_majid

UVA 1599 - Ideal Path

Sep 21st, 2022
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | Source Code | 0 0
  1. //Bismillahir Rahman-ir Rahim
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define debug(x) cout << '>' << #x << " : " << x << endl;
  5. #define all(c) c.begin(), c.end()
  6. #define F first
  7. #define S second
  8. #define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  9. typedef unsigned long long ull;
  10. typedef long long ll;
  11.  
  12. const ll mx = 1e5+7;
  13.  
  14. vector < ll > adj[mx];
  15. map < pair < ll, ll >, ll > cost;
  16.  
  17. vector <ll> levels(mx);
  18. vector <ll> vis(mx);
  19. vector <ll > level(mx);
  20. vector < ll > mn(mx, LLONG_MAX);
  21.  
  22. void bfs(ll source, ll n){
  23.    
  24.     vis.assign(n+10, 0);
  25.     level.assign(n+10, 0);
  26.     vis[source] = 1;
  27.     queue < ll > q;
  28.     q.push(source);
  29.     while(!q.empty()){
  30.         ll u = q.front();
  31.         q.pop();
  32.  
  33.         for(auto v: adj[u]){
  34.             if(!vis[v]){
  35.                 vis[v] = 1;
  36.                 level[v] = level[u]+1;
  37.                 q.push(v);
  38.             }
  39.         }
  40.     }
  41. }
  42.  
  43. void bfsA(ll source){
  44.  
  45.     queue < ll > q;
  46.     q.push(source);
  47.  
  48.     while(!q.empty()){
  49.         ll u = q.front();
  50.         q.pop();
  51.  
  52.         // find min cost of active nodes in each level
  53.  
  54.         for(auto v: adj[u]){
  55.  
  56.             // if this node is not deleted and this node is on the next level of current node's level, and it's cost is not zero,
  57.             // then I'll find its cost and compare it to all the cost in the next level
  58.             if(!vis[v] && (levels[v] == levels[u]+1) && cost[{min(u, v), max(u, v)}] != 0){
  59.                 mn[levels[v]] =  min(mn[levels[v]], cost[{min(u, v), max(u, v)}]);
  60.             }
  61.         }
  62.  
  63.  
  64.  
  65.         for(auto v: adj[u]){
  66.             // if any node's cost is minimum, then I'll take it for checking  
  67.             if(!vis[v] && (levels[v] == levels[u]+1) && cost[{min(u, v), max(u, v)}] == mn[levels[v]]){
  68.                 q.push(v);
  69.             }
  70.         }
  71.  
  72.     }
  73.  
  74. }
  75.  
  76.  
  77. int main(){
  78.  
  79.  
  80.     ll n, m;
  81.     cin >> n >> m;
  82.  
  83.     for(ll i  = 0; i < m; i++){
  84.         ll u, v, c;
  85.         cin >> u >> v >> c;
  86.  
  87.         adj[u].push_back(v);
  88.         adj[v].push_back(u);
  89.        
  90.         // taking the minimum cost between two nodes
  91.  
  92.         if(cost[{min(u, v), max(u, v)}] != 0) cost[{min(u, v), max(u, v)}] = min(c, cost[{min(u, v), max(u, v)}]);
  93.         else cost[{min(u, v), max(u, v)}] = c;
  94.     }
  95.  
  96.  
  97.     bfs(1, n);
  98.  
  99.     for(ll i = 1; i <= n; i++){
  100.         levels[i] = level[i]; // levels is level from source
  101.     }
  102.  
  103.     bfs(n, n);
  104.  
  105.    
  106.  
  107.  
  108.     // deleting unnecessary nodes by making it visited
  109.     vis.assign(n+10, 0);
  110.     for(ll i = 2; i < n; i++){
  111.         if(levels[i] + level[i] != levels[n]){
  112.             // delete i'th node
  113.             vis[i] = 1;
  114.         }
  115.     }
  116.  
  117.     bfsA(1);
  118.  
  119.     cout << levels[n] << endl;
  120.  
  121.     for(ll i = 1; i <= levels[n]; i++){
  122.         cout << mn[i];
  123.         if(i != levels[n]){
  124.             cout << ' ';
  125.         }
  126.     }
  127.     cout << '\n';
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement