Question: give you n vertices, M undirected edges, and ask you a complete graph composed of N vertices. Without that m edges, the shortest path of a single source appears by S.
Solution: First of all, the violent graph cannot be created, and there are too many points. Then we will follow its rules to build m edges, but the created graph will not show any way, then we need to use something to save and remove the other points of these directly connected edges, and use set
Code:
# Include <iostream> # include <algorithm> # include <cstring> # include <set> # include <queue> # include <cstdio> using namespace STD; const int maxn = 200500; const int INF = 0x3f3f3f; struct edge {int next; int to;} edge [maxn]; int head [maxn]; int visit [maxn]; int Dist [maxn]; int CNT; int n, m; int start; set <int> S; void add (int u, int v) {edge [CNT]. next = head [u]; edge [CNT]. to = V; head [u] = CNT ++;} void Init () {memset (Head,-1, sizeof (H EAD); CNT = 0;} void BFS (int u) {fill (Dist + 1, DIST + 1 + N, INF); queue <int> q; q. push (U); set <int> S1, S2; // S2 stores unextended vertices, and does not reach any point that may be transitioned by other vertices; dist [u] = 0; For (INT I = 1; I <= N; I ++) if (I! = U) s1.insert (I); // Add all vertices to the while (! Q. Empty () {int now = Q. Front (); q. Pop (); For (INT I = head [now]; I! =-1; I = edge [I]. Next) {int v = edge [I]. To; If (! S1.count (V) // If This vertex is directly connected to an edge, this edge is not considered continue; s1.erase (V); s2.insert (v ); // throw this vertex into a point that cannot be expanded} For (set <int>: iterator it = s1.begin (); it! = S1.end (); It ++) {q. push (* It); Dist [* It] = DIST [now] + 1;} s1.swap (S2); s2.clear () ;}} int main () {int tt; int U, V; while (scanf ("% d", & TT )! = EOF) {While (TT --) {Init (); scanf ("% d", & N, & M); For (INT I = 1; I <= m; I ++) {scanf ("% d", & U, & V); add (u, v); add (V, u);} scanf ("% d", & START); BFS (start); // start int COT = 0; For (INT I = 1; I <= N; I ++) {if (I = Start) continue; cot ++; If (Dist [I] = inf) cout <-1; else cout <Dist [I]; If (COT <= n-2) cout <"" ;}cout <Endl ;}}}
Hdu-5786 (fill the Shortest Path)