Codeforces 238E. Meeting Her graph theory + Memory search
Note:
There is a directed graph with n nodes, and the edge weight is 1. Urapl wants to depart from a to B. There are p bus companies. In each
At the beginning of one second, the bus of company I randomly chooses a shortest path from s I to t I and then follows this path. If
When a bus passes through the intersection of Urpal, Urpal can get on the bus and he can end it at any point in the middle.
Click and get off.
At any time, Urpal only knows his location and appointment location. When he got on the bus, he only knew the bus.
Cars belong to the first companies. Of course Urpal knows the city map and each company's (s I, t I ).
In the worst case, he needs to take a bus.
Solution:
First, find the point that each bus must go through, and then search for the point that must pass through. Then, calculate the number of times that each bus will go through in the worst case.
E. Meeting Her time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output
Urpal lives in a big city. He has planned to meet his lover tonight.
The city hasNJunctions numbered from 1N. The junctions are connectedMDirected streets, all the roads have equal length. Urpal lives in junctionAAnd the date is planned in a restaurant in junctionB. He wants to use public transportation to get to junctionB. There areKBus transportation companies. At the beginning of every second, a bus fromI-Th company chooses a random shortest path between junctionSIAnd junctionTIAnd passes through it. There might be no path fromSIToTI. In that case no bus will leave fromSIToTI. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he shoshould take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. when he gets on the bus he knows only the index of the company of this bus. of course Urpal knows the city map and the pairs (SI, Bytes,TI) For each company.
Note that Urpal doesn' t know buses velocity.
Input
The first line of the input contains four integersN,M,A,B(2 cores ≤ CoresNMemory ≤ memory 100; 0 memory ≤ memoryMLimit ≤ limitN·(NLimit-limit 1); 1 limit ≤ limitA, Bytes,BLimit ≤ limitN;A =B).
The nextMLines contain two integers eachUIAndVI(1 digit ≤ DigitUI, Bytes,VILimit ≤ limitN;UI =VI) Describing a directed road from junctionUITo junctionVI. All roads in the input will be distinct.
The next line contains an integerK(0 bytes ≤ bytesKLimit ≤0000100). There will beKLines after this, each containing two integersSIAndTI(1 digit ≤ DigitSI, Bytes,TILimit ≤ limitN;SI =TI) Saying there is a bus route startingSIAnd endingTI. Please note that there might be no path fromSIToTI, This case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal shocould get on his way in the worst case. if it's not possible to reach the destination in the worst case print-1.
Sample test (s) input
7 8 1 71 21 32 43 44 64 56 75 732 71 45 7
Output
2
Input
4 4 1 21 21 32 43 411 4
Output
-1
/*************************************** * ******** Author: CKbossCreated Time: October Sunday 22: 53 minutes 11 seconds File Name: CF238E_2.cpp *************************************** * *********/# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; const int maxn = 110; const int INF = 0x3f3f3f; int n, m, a, B, t; int from [maxn], to [maxn]; int dist [maxn] [maxn]; int incur [maxn] [maxn]; bool vis [maxn]; int g [maxn], f [maxn]; int dfs (int cur, int aim) {if (cur = aim) return f [cur]; if (vis [cur] = true) return g [cur]; vis [cur] = true; g [cur] = 0; for (int I = 1; I <= n; I ++) {if (dist [cur] [I] + dist [I] [aim] = dist [cur] [aim] & dist [cur] [aim] = dist [i] [aim] + 1) g [cur] = max (g [cur], dfs (I, aim);} return g [cur] = min (g [cur], f [cur]);} int main () {// freopen(in.txt, r, stdin); // freopen(out.txt, w, stdout ); scanf (% d, & n, & m, & a, & B); memset (dist, 63, sizeof (dist )); for (int I = 1; I <= n; I ++) dist [I] [I] = 0; for (int I = 0, u, v; I
= INF) ans =-1; printf (% d, ans); return 0 ;}