KFSTB - Help the Commander in Chief
Help the Commander in Chief
The Country of byteland is in a state of war with its neighboring nation bitland which is a small island. The king of byteland needs to send his soldiers across the sea to attack bitland. It is not easy for bytelandian soldiers to cross the sea between the two countries as there are soldiers of bitland who are constantly on patrol.
But, The commander of byteland through his secret sources has gathered some information about the camping spots in between, It is as follows. There are C camping spots in the sea between the two countries. There are B one way bridges, each bridge is between two camps. Bytelandian army can use these bridges without any danger of being attacked. The commander also knows that if the army leaves a camping spot along a bridge safely it is not possible to return back safely to the same camping spot.
The camping spot closest to the bytelandian shore is S and the camping spot closest to the bitlandian shore is T. The commander in chief of byteland wants to formulate a strategy for the army to safely move from the camping spot S to T. You have been assigned the task of finding the number of possible paths that a soldier could follow from camping spot S to T.
The first line of input contains the number of test cases D. For each test case the first line contains C, B, S and T number of camping spots, number of one way bridges, camping spot close to byteland and camping spot close to bitland respectively. The next B lines contain description of bridges. Each bridge is described on a single line by two integers X and Y denoting the starting and ending point of a bridge.
In each line print the number of possible paths modulo 1000000007
4 4 1 4
5 5 1 5
1<= D <= 10
1<= C <= 10000
1<= B <= 10000
1 <= X,Y <= C
1 <= S,T <= C
Nice DP+bfs question.
Dfs + dp AC in one go
nice problem i simply thinking to the soln using dfs with some optimization give accepted *****
The #dfs tag for this problem is almost misleading! It is actually memoization + general intuition! :PLast edit: 2016-10-13 20:22:40
In python if you are getting NZEC error, try to increase recursion limit (for DP top-down).
Problem description should say that the graph is a DAG
Think the problem description could've conveyed that the graph will be a DAG.That's crucial for solving this problem
Reverse Topological sort + DP
Using dfs.. but i keep getting TLE :(
Combination of DFS and DP can solve the problem , Don't forget to consider corner case when source is same as destination :)