Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- # define INF 0x3f3f3f3f
- // iPair ==> Integer Pair
- typedef pair<int, int> iPair;
- // To add an edge
- void addEdge(vector <pair<int, int> > adj[], int u,
- int v, int wt)
- {
- adj[u].push_back(make_pair(v, wt));
- adj[v].push_back(make_pair(u, wt));
- }
- // Prints shortest paths from src to all other vertices
- void shortestPath(vector<pair<int,int> > adj[], int V, int src, map<int,int*> *themap)
- {
- // Create a priority queue to store vertices that
- // are being preprocessed. This is weird syntax in C++.
- // Refer below link for details of this syntax
- // http://geeksquiz.com/implement-min-heap-using-stl/
- priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
- int* dist = (int*) malloc( V * sizeof(int));
- memset(dist, INF, V * sizeof(int));
- // Insert source itself in priority queue and initialize
- // its distance as 0.
- pq.push(make_pair(0, src));
- dist[src] = 0;
- /* Looping till priority queue becomes empty (or all
- distances are not finalized) */
- while (!pq.empty())
- {
- // The first vertex in pair is the minimum distance
- // vertex, extract it from priority queue.
- // vertex label is stored in second of pair (it
- // has to be done this way to keep the vertices
- // sorted distance (distance must be first item
- // in pair)
- int u = pq.top().second;
- pq.pop();
- // Get all adjacent of u.
- for (auto x : adj[u])
- {
- // Get vertex label and weight of current adjacent
- // of u.
- int v = x.first;
- int weight = x.second;
- // If there is shorted path to v through u.
- if (dist[v] > dist[u] + weight)
- {
- // Updating distance of v
- dist[v] = dist[u] + weight;
- pq.push(make_pair(dist[v], v));
- }
- }
- }
- (*themap)[src] = dist;
- }
- void fastscan(int &number)
- {
- //variable to indicate sign of input number
- bool negative = false;
- register int c;
- number = 0;
- // extract current character from buffer
- c = getchar();
- if (c=='-')
- {
- // number is negative
- negative = true;
- // extract the next character from the buffer
- c = getchar();
- }
- // Keep on extracting characters if they are integers
- // i.e ASCII Value lies from '0'(48) to '9' (57)
- for (; (c>47 && c<58); c=getchar())
- number = number *10 + c - 48;
- // if scanned input has a negative sign, negate the
- // value of the input number
- if (negative)
- number *= -1;
- }
- int main()
- {
- ios::sync_with_stdio(false);
- int n,i,j,a,b,jarak;
- fastscan(n) ; n++;
- map<int, int*> mymap;
- // printf("Vertex Distance from Source\n");
- // for (k = 0; k < n; ++k)
- // printf("%d \t\t %d\n", k, dist[k]);
- vector<iPair > adj[n];
- i = n; i--;
- while (i--)
- {
- // cin >> a >> b >> jarak;
- fastscan(a);
- fastscan(b);
- fastscan(jarak);
- addEdge(adj, a, b, jarak);
- }
- int* tempmem;
- mymap.insert({0,tempmem});
- shortestPath(adj, n, 0, &mymap);
- // printf("Vertex Distance from Source\n");
- // for (i = 0; i < n; ++i)
- // printf("%d \t\t %d\n", i, dist[i]);
- fastscan(j);
- i=j;
- int source = 0;
- while (i--)
- {
- // cin >> a >> b;
- fastscan(a);
- fastscan(b);
- if(a==1){
- if(mymap[source][b] == INF) cout << "#dirumahaja"<< endl ;
- else cout << mymap[source][b] << endl ;
- } else
- {
- if(mymap.count(b) == 0){
- mymap.insert({b,tempmem});
- shortestPath(adj, n, b, &mymap);
- }
- source=b;
- cout <<"Siap "<<b << endl ;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment