Post on 05-Jan-2016
description
transcript
Lab Demo 0822 October 2015
PS4 Debrief• Precompute the MST of the original graph using
Prim or Kruskal
• Precompute all MiniMax to all vertices from the first ten vertices by running DFS/BFS
• Answer each query in O(1)
• Total time complexity O(E log V + 10 x V)
Intended solution for subtask D:
Build MST
Traverse from the first ten
vertices
PS4 Debrief• Precompute the MST of the original graph using
Prim or Kruskal
• Precompute all MiniMax to all vertices from the first ten vertices by running DFS/BFS
• Answer each query in O(1)
• Total time complexity O(E log V + 10 x V)
Intended solution for subtask D:
Why traversal only took O(V) time ?
PS4 Debrief
What if there is no constraint on the source vertex ?
PS4 Debrief1
2 3
10 4 6
5 7
9
8
Rooted tree at 1
PS4 Debrief1
2 3
10 4 6
5 7
9
8
Lowest Common Ancestor of 5 and 9
PS4 Debrief1
2 3
10 4 6
5 7
9
8
Lowest Common Ancestor (LCA) of 5 and 9
Ancestor of 5Ancestor of 9
PS4 Debrief
• Finding LCA of two vertices can be done in
• Finding maximum edge in path from a vertex to its ancestor can be done in
• Time complexity for each query becomes
• Total complexity O(E log V + Q log V)
O(log V)
O(log V)
O(log V)
PS5 is out :O
• Subtask C is quite tough to AC
• Need to use different codes for each subtask
• Try to start early
PS5 StatusName A B C D
- AC AC AC AC
Hubert AC AC AC
Rui Bin, Jiahao, Yun Shian, Danwen AC AC
Cindy, Bao Jun AC
The rest?
PS5• Given a graph, and Q queries
• Each query asks for shortest path from s to t that uses no more than k vertices
PS5 Subtask A• Given graph is an undirected weighted tree
• 1 ≤ V ≤ 1 000
• 1 ≤ Q ≤ 100 000
• k=V
PS5 Subtask A• Given graph is an undirected weighted tree
• 1 ≤ V ≤ 1 000
• 1 ≤ Q ≤ 100 000
• k=V Just ignore k
PS5 Subtask A• Given graph is an undirected weighted tree
• 1 ≤ V ≤ 1 000
• 1 ≤ Q ≤ 100 000
• k=V
Classic shortest path problem, use Dijkstra ?
Just ignore k
PS5 Subtask A• Given graph is an undirected weighted tree
• 1 ≤ V ≤ 1 000
• 1 ≤ Q ≤ 100 000
• k=V
Classic shortest path problem, use Dijkstra ?
Total complexity : O(Q * E log V) ~ 109 processes
Just ignore k
TLE👎
PS5 Subtask A• Given graph is an undirected weighted tree
• 1 ≤ V ≤ 1 000
• 1 ≤ Q ≤ 100 000
• k=V
Use property of a tree
Total complexity : O(Q * (V + E)) ~ 108 processes
run BFS/DFS from u to v
Just ignore k
TLE👎
PS5 Subtask A• Given graph is an undirected weighted tree
• 1 ≤ V ≤ 1 000
• 1 ≤ Q ≤ 100 000
• k=V
Precompute from each vertex
Total complexity : O(V * (V + E) + Q) ~ 106 processes
Notice the small V
AC👍
O(1) for each query
PS5 Subtask B• Given graph is a directed weighted graph
• 1 ≤ V ≤ 1 000
• 0 ≤ E ≤ 200 000
• 1 ≤ Q ≤ 10 000
• 0 ≤ s ≤ 9
• k=V
PS5 Subtask B• Given graph is a directed weighted graph
• 1 ≤ V ≤ 1 000
• 0 ≤ E ≤ 200 000
• 1 ≤ Q ≤ 10 000
• 0 ≤ s ≤ 9
• k=V
ignore k again
Shortest path on general graph
Dijkstra
Total complexity : O(Q * E log V) ~ 1010 processes
TLE👎
PS5 Subtask B• Given graph is a directed weighted graph
• 1 ≤ V ≤ 1 000
• 0 ≤ E ≤ 200 000
• 1 ≤ Q ≤ 10 000
• 0 ≤ s ≤ 9
• k=V
Shortest path on general graph
Dijkstra
Total complexity : O(Q * E log V) ~ 1010 processes
Looks like PS4 Subtask D
TLE👎
ignore k again
PS5 Subtask B• Given graph is a directed weighted graph
• 1 ≤ V ≤ 1 000
• 0 ≤ E ≤ 200 000
• 1 ≤ Q ≤ 10 000
• 0 ≤ s ≤ 9
• k=V
Precompute dijkstra from first ten vertices
Total complexity : O(10 * E log V + Q) ~ 2*107 processes
Looks like PS4 Subtask D
Query done in O(1)
AC👍
ignore k again
PS5 Subtask C• Given graph is a directed weighted graph
• 1 ≤ V ≤ 1 000
• 0 ≤ E ≤ 200 000
• 1 ≤ Q ≤ 20
• 1 ≤ k ≤ min(V, 20)
PS5 Subtask C• Given graph is a directed weighted graph
• 1 ≤ V ≤ 1 000
• 0 ≤ E ≤ 200 000
• 1 ≤ Q ≤ 20
• 1 ≤ k ≤ min(V, 20)
Need to use k now, cannot directly use Dijkstra
PS5 Subtask C• Given graph is a directed weighted graph
• 1 ≤ V ≤ 1 000
• 0 ≤ E ≤ 200 000
• 1 ≤ Q ≤ 20
• 1 ≤ k ≤ min(V, 20)
Need to somehow attach a counter to vertices
1
1,1 1,2 1,3 1,k-1 1,k…
Split each vertex to k new vertices
PS5 Subtask C• Given graph is a directed weighted graph
• 1 ≤ V ≤ 1 000
• 0 ≤ E ≤ 200 000
• 1 ≤ Q ≤ 20
• 1 ≤ k ≤ min(V, 20)
Need to somehow attach a counter to vertices
1
1,1 1,2 1,3 1,k-1 1,k…
Split each vertex to k new vertices
(u, k) moves to (v, k+1)
PS5 Subtask C0
1 2 3
Query from 0 to 3, when k = 3
PS5 Subtask C0
1 2 3
Query from 0 to 3, when k = 3
0,1 0,2 0,3
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3
source
destination
Optimisation• Modified Dijkstra algorithm should run faster
• Constructing a new adjacency list with 20V vertices probably will get TLE
• Stop Dijkstra when it reach the destination vertex (important!)
• Use primitive int type on IntegerPair object
Subtask C has a very strict time limit
AC👌
PS5 Subtask D
• Subtask D is beyond CS2010
• It is a classical problem that can be solved using maximum flow.
• Take CS3233 next sem if you are interested.
UVa 926
• This is part of CS2010 final exam question in the past 4 years
UVa 926• Given a grid, start position
and end position
• Find number of ways to go from start to end
• Cannot move south or west.
• Some roads are discontinued. (1,2) to (2,2) and (2,2) to (2,3)
UVa 926• Represent each intersection as a vertex
• There are no cycle in this graph, so this is a DAG (Directed Acyclic Graph)
UVa 926• Counting number of path is usually a DP problem.
• Since it is a DAG, so it is okay to use DP.
Let start position be (xs, ys)
Let end position be (xe, ye)
DP solutioncountPath(x, y): number of path from (x,y) to (xe, ye)
countPath(x,y) = 1
if (x,y) = (xe, ye) Base Case
DP solutioncountPath(x, y): number of path from (x,y) to (xe, ye)
countPath(x,y) =
if (x,y) <> (xe, ye)
countPath(x+1, y) countPath(x, y+1)
0 0
if (x,y) to (x+1,y) not under construction
+
otherwise otherwise
if (x,y) to (x,y+1) not under construction