Advertisement
Falak_Ahmed_Shakib

bfs any wait

May 12th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. // In the name of Allah.
  2. // We're nothing and you're everything.
  3. // Ya Ali!
  4. /*
  5.                   ,     \    /      ,
  6.                  / \    )\__/(     / \
  7.                 /   \  (_\  /_)   /   \
  8.            ____/_____\__\@  @/___/_____\____
  9.           |             |\../|              |
  10.           |              \VV/               |
  11.           |        ------___-------         |
  12.           |__________Chuta Dragon___________|
  13.            |    /\ /      \\       \ /\    |
  14.            |  /   V        ))       V   \  |
  15.            |/     `       //        '     \|
  16.            `              V                '
  17. */
  18.  
  19. #include<bits/stdc++.h>
  20. using namespace std;
  21. typedef long long  ll;
  22. typedef pair<ll,ll> pll;
  23. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  24. #define fi first
  25. #define se second
  26. #define pb push_back
  27. ll const MOD=1000000007;
  28. const int M= 2e5 + 10;
  29. ///-------------------------------------------------------------------------------------------------///
  30. ///                KARMA IS LIKE 69,,, YOU GET WHAT YOU GIVE                                        ///
  31. ///-------------------------------------------------------------------------------------------------///
  32. #define eb emplace_back
  33.  
  34. vector<pll>v[M+10];
  35. ll n,m;
  36. bool vis[M+10];
  37. ll cost[M+1];
  38. void BFS(ll s)
  39. {
  40.     vis[s]=true;
  41.  
  42.     cost[s]=0;
  43.  
  44.     queue<ll>q;
  45.  
  46.     q.push(s);
  47.  
  48.  
  49.     while(!q.empty())
  50.     {
  51.         ll u=q.front();
  52.         q.pop();
  53.  
  54.        for(auto x:v[u])
  55.        {
  56.            ll node=x.fi;
  57.            ll w=x.se;
  58.  
  59.            if(!vis[node])
  60.            {
  61.                cout<<node<<" "<<u<<endl;
  62.                
  63.                cost[node]=cost[u]+w;
  64.  
  65.                vis[node]=true;
  66.                q.push(node);
  67.  
  68.            }
  69.  
  70.  
  71.        }
  72.  
  73.     }
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. void input()
  93. {
  94.  
  95.      cin>>n>>m;
  96.  
  97.     ll a,b,c;
  98.     for(ll i=0;i<m;i++)
  99.     {
  100.         cin>>a>>b>>c;
  101.  
  102.        v[a].eb(b,c);
  103.  
  104.        v[b].eb(a,c);
  105.  
  106.     }
  107. }
  108.  
  109.  
  110. int main()
  111. {
  112.     fastread();
  113.  
  114.     input();
  115.  
  116.  
  117.     ll s;
  118.  
  119.  
  120.    cin>>s;
  121.  
  122.  
  123.  
  124.     BFS(s);
  125.  
  126.  
  127.  cout<<endl<<endl;
  128.  
  129.     for(ll i=1;i<=n;i++)
  130.     {
  131.         cout<<i<<" "<<cost[i]<<endl;
  132.     }
  133.  
  134.  
  135.  
  136.  
  137. }
  138. /*
  139.  
  140. 6 6
  141. 1 2 5
  142. 1 3 4
  143. 2 4 5
  144. 2 6 5
  145. 3 5 4
  146. 3 4 4
  147.  
  148. 3
  149.  
  150. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement