SHARE
    TWEET
    Vince14

    Small Multiple BFS

    Feb 25th, 2023
    90
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    C++ 2.09 KB | Source Code | 0 0
    1. #include <iostream>
    2. #include <string>
    3. #include <cstring>
    4. #include <algorithm>
    5. #include <cmath>
    6. #include <vector>
    7. #include <set>
    8. #include <map>
    9. #include <stack>
    10. #include <queue>
    11. #include <deque>
    12. #include <unordered_map>
    13. #include <numeric>
    14. #include <iomanip>
    15. using namespace std;
    16. #define pii pair<int, int>
    17. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
    18. const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    19. const int dl[2] = {1, -1};
    20. const int MOD = 1e9;
    21. const int MAX = 100005;
    22. int n, m, dist[MAX], par[MAX];
    23. vector<pii> v[MAX]; // {node, weight}
    24. priority_queue<pii> pq;
    25. void init_dist(){
    26. for(int i = 0; i <= n; i++){
    27. dist[i] = 2e9;
    28. }
    29. dist[1] = 0;
    30. }
    31. void Dijkstra(){
    32. pq.push({-1, 1});
    33. while(!pq.empty()){
    34. int cur = pq.top().second;
    35. int d = -pq.top().first;
    36. // cout << cur << " " << d << "\n";
    37. pq.pop();
    38. for(pii a : v[cur]){
    39. int nxt = a.first;
    40. int nd = a.second;
    41. if(d + nd < dist[nxt]){
    42. dist[nxt] = d + nd;
    43. par[nxt] = cur;
    44. pq.push({-(d + nd), nxt});
    45. }
    46. }
    47. }
    48. }
    49. void BFS(){
    50. deque<pii> q;
    51. q.push_front(make_pair(1, 1));
    52. while(!q.empty()){
    53. int cur = q.front().first;
    54. int d = q.front().second;
    55. q.pop_front();
    56. if(dist[cur] != 0){
    57. continue;
    58. }
    59. dist[cur] = d;
    60. for(auto x : v[cur]){
    61. int nxt = x.first;
    62. int nd = x.second;
    63. if(nd == 1){
    64. q.push_back(make_pair(nxt, d + nd));
    65. }
    66. else{
    67. q.push_front(make_pair(nxt, d + nd));
    68. }
    69. }
    70. }
    71. cout << dist[0];
    72. }
    73. int main() {
    74. FAST;
    75. //freopen("gravity.in", "r", stdin);
    76. //freopen("gravity.out", "w", stdout);
    77. cin >> n;
    78. for(int i = 1; i <= n; i++){
    79. v[i % n].push_back(make_pair((i + 1) % n, 1));
    80. }
    81. for(int i = 1; i <= n; i++){
    82. v[i % n].push_back(make_pair((i * 10) % n, 0));
    83. }
    84. BFS();
    85. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /