mbah_bejo

save&found

May 4th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. # define INF 0x3f3f3f3f
  4.  
  5. // iPair ==> Integer Pair
  6. typedef pair<int, int> iPair;
  7.  
  8. // To add an edge
  9. void addEdge(vector <pair<int, int> > adj[], int u,
  10.                                      int v, int wt)
  11. {
  12.     adj[u].push_back(make_pair(v, wt));
  13.     adj[v].push_back(make_pair(u, wt));
  14. }
  15.    
  16.  
  17. // Prints shortest paths from src to all other vertices
  18. void shortestPath(vector<pair<int,int> > adj[], int V, int src, map<int,int*> *themap)
  19. {
  20.     // Create a priority queue to store vertices that
  21.     // are being preprocessed. This is weird syntax in C++.
  22.     // Refer below link for details of this syntax
  23.     // http://geeksquiz.com/implement-min-heap-using-stl/
  24.     priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
  25.     int* dist = (int*) malloc( V * sizeof(int));
  26.     memset(dist, INF, V * sizeof(int));
  27.  
  28.     // Insert source itself in priority queue and initialize
  29.     // its distance as 0.
  30.     pq.push(make_pair(0, src));
  31.     dist[src] = 0;
  32.  
  33.     /* Looping till priority queue becomes empty (or all
  34.     distances are not finalized) */
  35.     while (!pq.empty())
  36.     {
  37.         // The first vertex in pair is the minimum distance
  38.         // vertex, extract it from priority queue.
  39.         // vertex label is stored in second of pair (it
  40.         // has to be done this way to keep the vertices
  41.         // sorted distance (distance must be first item
  42.         // in pair)
  43.         int u = pq.top().second;
  44.         pq.pop();
  45.  
  46.         // Get all adjacent of u.  
  47.         for (auto x : adj[u])
  48.         {
  49.             // Get vertex label and weight of current adjacent
  50.             // of u.
  51.             int v = x.first;
  52.             int weight = x.second;
  53.  
  54.             // If there is shorted path to v through u.
  55.             if (dist[v] > dist[u] + weight)
  56.             {
  57.                 // Updating distance of v
  58.                 dist[v] = dist[u] + weight;
  59.                 pq.push(make_pair(dist[v], v));
  60.             }
  61.         }
  62.     }
  63.  
  64.     (*themap)[src] = dist;
  65.  
  66. }
  67.  
  68. void fastscan(int &number)
  69. {
  70.     //variable to indicate sign of input number
  71.     bool negative = false;
  72.     register int c;
  73.  
  74.     number = 0;
  75.  
  76.     // extract current character from buffer
  77.     c = getchar();
  78.     if (c=='-')
  79.     {
  80.         // number is negative
  81.         negative = true;
  82.  
  83.         // extract the next character from the buffer
  84.         c = getchar();
  85.     }
  86.  
  87.     // Keep on extracting characters if they are integers
  88.     // i.e ASCII Value lies from '0'(48) to '9' (57)
  89.     for (; (c>47 && c<58); c=getchar())
  90.         number = number *10 + c - 48;
  91.  
  92.     // if scanned input has a negative sign, negate the
  93.     // value of the input number
  94.     if (negative)
  95.         number *= -1;
  96. }
  97.  
  98. int main()
  99. {
  100.    ios::sync_with_stdio(false);
  101.     int n,i,j,a,b,jarak;
  102.    
  103.     fastscan(n) ; n++;
  104.  
  105.     map<int, int*> mymap;
  106.  
  107.     //  printf("Vertex Distance from Source\n");
  108.     // for (k = 0; k < n; ++k)
  109.     //     printf("%d \t\t %d\n", k, dist[k]);
  110.     vector<iPair > adj[n];
  111.  
  112.     i = n; i--;
  113.     while (i--)
  114.     {
  115.        // cin >> a >> b >> jarak;
  116.         fastscan(a);
  117.         fastscan(b);
  118.         fastscan(jarak);
  119.         addEdge(adj, a, b, jarak);
  120.     }
  121.     int* tempmem;
  122.     mymap.insert({0,tempmem});
  123.     shortestPath(adj, n, 0, &mymap);
  124.     //  printf("Vertex Distance from Source\n");
  125.     // for (i = 0; i < n; ++i)
  126.     //     printf("%d \t\t %d\n", i, dist[i]);
  127.     fastscan(j);
  128.     i=j;
  129.  
  130.     int source = 0;
  131.     while (i--)
  132.     {
  133.        // cin >> a >> b;
  134.         fastscan(a);
  135.         fastscan(b);
  136.         if(a==1){
  137.             if(mymap[source][b] == INF) cout << "#dirumahaja"<< endl ;
  138.             else  cout << mymap[source][b] << endl ;
  139.         } else
  140.         {
  141.             if(mymap.count(b) == 0){
  142.                 mymap.insert({b,tempmem});
  143.                 shortestPath(adj, n, b, &mymap);
  144.             }
  145.             source=b;
  146.             cout <<"Siap "<<b << endl ;
  147.         }
  148.     }
  149.  
  150.     return 0;
  151. }
Add Comment
Please, Sign In to add comment