1807. [NOIP2014] search for P2296 and noip2014p2296

Description

In directed graph G, the length of each edge is 1. Given the start point and end point, please find a path from the start point to the end point in the graph. This path meets the following conditions:

1. All vertices in the path are directly or indirectly connected to the endpoints.

2. Minimize the path if condition 1 is met.

Note: duplicate edges and self-rings may exist in graph G. The question ensures that there is no outbound edges at the end.

Please output the length of the qualified path.

Input/Output Format

**Input Format:**
The input file name is road. in.

The first line contains two integers n and m separated by a space, indicating that the graph has n points and m edges.

In the next m row, two integers x and y are separated by a space, indicating that an edge points from x to y.

There are two integers s and t separated by a space in the last line, indicating that the starting point is s and the ending point is t.

**Output Format:**
The output file name is road. out.

The output contains only one row and an integer, indicating the length of the shortest path that meets the requirements described in the topic. If this path does not exist, output-1.

Input and Output sample

**Input example #1:**
3 2 1 2 2 1 1 3

**Output sample #1:**
-1

**Input example #2:**
6 6 1 2 1 3 2 6 2 5 4 5 3 4 1 5

**Output sample #2:**
3

Description

Explanation 1:

As shown in, arrows indicate directed roads, and dots indicate cities. Start 1 and end 3 are not connected.

The specified path does not exist. Therefore, output-1 is displayed.

Interpretation 2:

As shown in, the path that meets the conditions is 1-> 3-> 4-> 5. Note that point 2 cannot be in the answer path, because point 2 connects an edge to point 6, and point 6 does not connect to end 5.

For 30% of data, 0 <n ≤ 10, 0 <m ≤ 20;

For 60% of data, 0 <n ≤ 100,0 <m ≤ 2000;

For 100% of data, 0 <n ≤ 10,000, 0 <m ≤ 200,000, 0 <x, y, s, t ≤ n, x = t.

We can use reverse thinking to think about this question.

If a point can reach the end point, the end point will certainly reach this point.

This is simple.

Run BFS at the end to calculate the number of visits to each point.

Then, delete the inaccessible vertex.

Finally spfa is taken away

A very interesting way to find out the number of visits without endless Loops

1 int to = edge [I]. v;

2 if (cs [to] ++) continue; 3 q. push (to); complete code

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # define INF 0x7ffffff 7 using namespace std; 8 int read (int & n) 9 {10 int flag = 0, x = 0; char c = '/'; 11 while (c <'0' | c> '9') {c = getchar (); if (c = '-') flag = 1 ;} 12 while (c> = '0' & c <= '9') x = x * 10 + (c-48), c = getchar (); 13 if (flag) n =-x; else n = x; 14} 15 const int MAXN = 200001; 16 int n, m, bgx, bgy; 17 int rudu [MAXN]; 18 Struct node 19 {20 int u, v, w, nxt; 21} edge [MAXN]; 22 int num = 1; 23 int head [MAXN]; 24 int flag [MAXN]; // record whether each value can reach the end point 25 int cs [MAXN]; 26 int dis [MAXN]; 27 int vis [MAXN]; 28 void add_edge (int ll, int rr, int ww) 29 {30 edge [num]. u = ll; 31 edge [num]. v = rr; 32 edge [num]. w = ww; 33 edge [num]. nxt = head [ll]; 34 head [ll] = num ++; 35} 36 void bfs () 37 {38 queue <int> q; 39 int tot = 0; 40 q. push (bgx), tot ++; 41 wh Ile (q. size ()! = 0) 42 {43 int p = q. front (); 44 q. pop (); 45 for (int I = head [p]; I! =-1; I = edge [I]. nxt) 46 {47 int to = edge [I]. v; 48 if (cs [to] ++) continue; 49 q. push (to); 50} 51} 52 // rudu [bgy] = 0; 53 for (int I = 1; I <= n; I ++) 54 if (rudu [I]! = Cs [I] & I! = Bgy) 55 flag [I] = 1; 56} 57 void dele () 58 {59 for (int I = 1; I <= num; I ++) 60 {61 if (flag [edge [I]. u]! = 0) 62 {63 edge [I]. u =-1; 64 edge [I]. v =-1; 65 edge [I]. w =-1; 66 edge [I]. nxt =-1; 67} 68} 69} 70 void spfa () 71 {72 queue <int> q; 73 q. push (bgx); 74 dis [bgx] = 0; 75 while (q. size ()! = 0) 76 {77 int p = q. front (); 78 q. pop (); 79 vis [p] = 0; 80 for (int I = head [p]; I! =-1; I = edge [I]. nxt) 81 {82 if (edge [I]. u =-1) continue; 83 int to = edge [I]. v; 84 if (dis [to]> dis [p] + edge [I]. w) 85 {86 dis [to] = dis [p] + edge [I]. w; 87 if (vis [to] = 0) 88 {89 vis [to] = 1; 90 q. push (to); 91} 92} 93} 94} 95 if (dis [bgy] = INF) 96 printf ("-1 "); 97 else 98 printf ("% d", dis [bgy]); 99} 100 int main () 101 {102 freopen ("roadb. in "," r ", stdin); 103 freopen (" roadb. out "," w ", stdout); 104 read (n); read (MB); 105 for (int I = 1; I <= n; I ++) head [I] =-1, dis [I] = INF; 106 for (int I = 1; I <= m; I ++) 107 {108 int x, y; 109 read (x); read (y); 110 add_edge (y, x, 1); 111 rudu [x] ++; 112} 113 read (bgy ); read (bgx); 114 bfs (); 115 dele (); 116 spfa (); 117 return 0; 118}