There is a huge need for oxygen at Sata Hospital. The oxygen production industry Gorkha wishes to supply ‘N’ trucks of oxygen daily for ‘D’ days to Sata Hospital. There are ‘M’ paths numbered from 1 to ‘M’ between Gorkha and the hospital. Each truck requires a permit to travel on these paths.
The government has given ‘P’ permits to Gorkha Industry. Each permit consists of two integers ‘x’ and ‘y’. It says that trucks numbered ‘x’ are allowed to go through path number ‘y’.
Each path has a restriction on the number of trucks it can allow to travel on it. This restricted number is known as Capacity ‘C’.
Due to the poor construction, every day for the D days 1 particular path's capacity reduces by a number ‘R’.
For each day before the reduction takes place, you need to find the maximum number of trucks ‘T’ that can travel to SATA Hospital.
The first line of input contains four integers ‘N’, M, ‘P’, and ‘D’, denoting the number of trucks, number of paths, number of government permits and number of days Hospital Gorkha wanted to supply.
The next ‘P’ line contains two integers ‘T’ and ‘X’, denoting that ‘T’-th truck can travel through ‘X’-th path.
The next line contains ‘M’ space separated positive integers capacity[i], denoting the capacity of the 'i-th' path.
The next ‘D’ line contains two integers ‘X’ and ‘R’, denoting that the capacity of the ‘X’-th path is reduced by ‘R’.
Output Format:
For each day, print a single line containing one positive integer ‘T’ denoting the maximum number of trucks that can travel to SATA Hospital on day ‘i’.
The output of each day will be printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= N, M <= 500
1 <= D, P <= 500
1 <= T <= N
1 <= X <= M
1 <= capacity[i], R <= 20
Time Limit: 1 sec.
10 5 10 5
9 5
10 4
8 1
7 1
1 1
1 2
2 3
2 4
3 5
4 5
5 4 3 2 1
1 5
2 4
5 1
4 2
3 3
6
4
3
2
1
Before Day 1 reduction - > Send truck 1 on path 1, truck 2 on path 3, truck 3 on path 5, truck 7 on path 1, truck 8 on path 1, truck 10 on path 4. So a total of 6 trucks can travel.
Before Day 2 reduction - > Send truck 1 on path 2, truck 2 on path 3, truck 3 on path 5, truck 10 on path 4. So a total of 4 trucks can travel.
Before Day 3 reduction - > truck 2 on path 3, truck 3 on path 5, truck 10 on path 4. So a total of 3 trucks can travel.
Before Day 4 reduction - > truck 2 on path 3, truck 10 on path 4. So a total of 2 trucks can travel.
Before Day 5 reduction - > truck 10 on path 4. So a total of 1 truck can travel.
3 2 3 2
1 2
2 2
3 2
1 3
2 3
1 1
3
0
Before Day 1 reduction - > Send truck 1, 2 and 3 on path 2.
Before Day 2 reduction - > No paths are good for trucks to travel.
Try to relate the question to a graph by considering the edge between trucks and paths.
If queries were not given how to solve the problem?
The idea is to use the concept of maximum flow in a network.
Doing this every time will timeout. Can we do better?
Take the following global variable:
Let maxTruck(N, M, P, D, permits, cap, reduction) be the function that calculates the maximum number of trucks that can travel in D days. It accepts 7 parameters: the number of trucks, number of paths, number of permits, number of reduction days, permit array, the initial capacity of each path, and reduction array.
It returns the maximum flow from source s to sink (t).
This Is a general dfs function that takes two parameters (source and sink), finds the parent of each node in the Augmented path (if any), and returns whether the current graph network has an Augmented path or not.
This function finds the minimum value edge in the augmented path, updates the edge of the augmented path, and returns the current flow in the network.
O(D * N) where ‘D’ is the number of days, oxygen is supplied and ‘N’ is the number of trucks.
Since the time complexity of ‘maxFlow’ function in our case is O(N) and we are computing maxFlow for ‘D’ days so the overall time complexity is O(D * N).
O((N + M) ^ 2) where ‘N’ is the number of trucks and ‘M’ is the number of paths.
The space used to make a graph is O(P + N + M), space used to mark visited O(N + M ), space used to track parent O(N + M ), space used to store the capacity of each edge O(( N + M ) ^ 2)). So the overall time complexity used in this algorithm is O(P + N + M) + 2 * O(N + M ) + O(( N + M ) ^ 2)). = O((N + M) ^ 2).