Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5876
Test instructions
There is an n-point graph, I'll give you the M-pair vertex, which means there is no edge between the M-vertices, except that there is an edge between each of the two points, and the weight is 1. Then there is a source point S, which allows you to calculate the shortest distance between the source points and the other points, and if not present, the output-1 The shortest path from the source point to the other points.
Ideas:
To find the shortest path on the map is a more classical problem. In this case, it is not necessary to use the Dijkstra algorithm, because the distance between each edge is 1, each edge of the same weight. Then you can think of this approach:
Step 1: Build the map according to test instructions. Then set up a queue, put the source point S into the queue, the other points to the other set V, and create an array to store the source point to other points of the shortest distance, initialized to 1, dis[s] = 0.
Step 2: Remove a node v from the queue header, access all nodes that are not adjacent to Node v u, press u into the tail of the queue, and remove U from the Set V, update dis[u] = Dis[v] + 1.
Step 3: Repeat step 2 repeatedly until the queue is empty. Then the dis array is saved as the answer.
After solving how to do, not finished, because the topic gives a very large number of points, so also need to optimize the next time. Here is the time to find the point in step 2 is not adjacent. Here you can use the set in the STL to maintain the set V, which is the point that has not been accessed. Initialize the Set1 for all points (except the source point S), when the point v is accessed, in the Set1 to remove the point u adjacent to Point V, and added to the Set2, then the remaining points in the Set1 is all the points of the Point V is not adjacent, sequentially traversing the indentation queue. Then copy the Set2 to Set1, then Set1 is the remaining inaccessible point, so repeat. You can see that it is accessed only once per edge. Each point is also queued only once. So the total time complexity is O (n * m), which can be achieved.
NOTES: When you create an image without a direction , you save two times for each side. So the size of the array must be two times the number of sides given by the original question! Two times! Two times!
Code:
1#include <iostream>2#include <cstdio>3#include <queue>4#include <Set>5#include <cstring>6#include <algorithm>7 8 using namespacestd;9typedefLong LongLL;Ten One Const intMAXN =200000; A Const intMaxe =20000; - intN, M, T, S; - intDIS[MAXN +3];//Save the final shortest distance the - intHEAD[MAXN +3], Len;//chain-forward star - structNODE {intto;intnext;}; -NODE edge[2* Maxe +3]; + - voidAddedge (intUintV) {//chain-forward star plus edge +Edge[len].to =v; AEdge[len].next =Head[u]; atHead[u] = len++; - } - - voidBFS () {//Starting from the beginning BFs -memset (DIS,-1,sizeof(DIS)); -Queue <int>Qu; inQu.push (S); Dis[s] =0;//start of initialization - Set<int> unsed, hep;//used to maintain points that have not been accessed to for(inti =1; I <= N; i++) Unsed.insert (i); + unsed.erase (S); - while( !Qu.empty ()) { the intTP =Qu.front (); Qu.pop (); * for(intk = HEAD[TP]; K! =-1; K = Edge[k].next) {//removes the point adjacent to the current expansion node from the unsed and joins the temporary auxiliary collection $ if(Unsed.find (edge[k].to)! =Unsed.end ()) {Panax Notoginseng unsed.erase (edge[k].to); - Hep.insert (edge[k].to); the } + } A for(Set<int>::iterator it = Unsed.begin (); It! = Unsed.end (); it++) {//Unsed temporarily preserves points that are not adjacent to the current expansion node theQu.push (*it); +Dis[*it] = DIS[TP] +1; - } $Hep.swap (unsed), hep.clear ();//Copy the remaining inaccessible points from the secondary collection. $ } - } - the intMain () { -scanf"%d", &T);Wuyi while(t--) { theMemset (Head,-1,sizeof(head)); -scanf"%d%d", &n, &m); Wu intU, v; Len =0; - for(inti =0, Len =0; I < m; i++) { Aboutscanf"%d%d", &u, &v); $ Addedge (U, v); Addedge (v, u); - } -scanf"%d", &S); - BFS (); A for(inti =1, j =0; I <= N; i++)if(I! = S) printf ("%d%c", Dis[i],"\ n"[++J = = N-1]); + } the return 0; -}
HDU 5876 Sparse Graph (fill map on BFS)