Door-to-Door Sales (Hard)
Time limit2sMemory limit1024 MB
A fixed deterministic order (lexicographically smallest topological order) is generated, then find the minimum number of customers to select so their summed x and y meet quotas X and Y, and report the last-selected customer's identity.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Topological sort, Greedy, Graph
- Solved
- No attempts yet
Problem
SG Group has just released two revolutionary products, X and Y. Leo, the top salesman working as a traveling salesperson in SG Group's sales department, must visit the homes of all customers and sell these two products in the given quotas and . Each customer is numbered from to , and if a visit to customer 's home results in a successful sale, the amounts of products X and Y that can be sold are given as and , respectively. However, some customers may refuse to buy the products even when visited, resulting in a failed sale.
When making door-to-door sales, the customers must be visited in order according to the manual set by the sales department, but Leo, having lost the manual, fell into deep thought because he cannot know the full visit order. However, Leo has a good memory and knows, for pairs of distinct customer numbers, which customer must be visited first. So Leo decides to visit all customers based on his memory using the following rules.
- Among the customers not yet visited, all customers who are currently visitable are chosen as visit candidates.
- All customers chosen as candidates are visited in ascending order of customer number, starting from the smallest.
- Steps 1 and 2 are repeated until all customers have been visited.
In step 1, "visitable" means that there is no customer who must be visited before them, or that all such customers have already been visited. In other words, if there is no customer who must be visited before customer , or if all such customers have already been visited, then customer becomes a visit candidate.
The car Leo carries while selling is filled with enough quantities of products X and Y so that they never run short.
Leo wonders how few customers he must sell both products X and Y to in this door-to-door sales trip to meet the given quotas. Find the minimum number of customers who must be made to buy the products in order to visit all customers in a way that satisfies the visit order and meet the quotas, and the number of the customer who succeeds in the sale last.
Input
The first line gives the number of customers Leo must visit, , the number of pieces of information about the customer visit order he remembers, , and the quotas of the two products X and Y to be sold, and , as integers. (, , )
From the second line, across lines, the ordered pairs , concerning the visit order Leo remembers are given in order. This means that the customer numbered must be visited before the customer numbered . The same ordered pair is not given more than once. (, , )
From line , across lines, for customers through , the amounts and of the two products X and Y that the customer buys when a visit results in a successful sale are given as integers. (, )
Output
Print the minimum number of customers who must have a successful sale in order to visit all customers in a way that satisfies the visit order and meet the quotas. Then, on the next line, print the number of the customer who succeeds in the sale last; if there are multiple possible numbers, print the number of the customer whose position in the visit order is earliest among them.
If the quotas cannot be met even if any customer buys the products, or if the visit order is contradictory and all customers cannot be visited so the door-to-door sales cannot be completed, print .