Antennas
Time limit2sMemory limit256 MB
Place carrier-specific or shared antennas on a line so each house interval meets a matching coverage interval at minimum cost.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Sorting, Intervals
- Solved
- No attempts yet
Problem
1D-Land is a single line. Every house is a closed interval on that line, and every resident is a point inside their own house. Each resident owns exactly one house.
The carriers MTC1 and MTC2 sell SIM cards, and every resident has already bought a card from one of them. Both carriers now install antennas so that every house of their own subscribers is covered.
An antenna installed at point has the coverage interval . The antenna covers a house when it supports the SIM type of that house's owner and its coverage interval shares at least one point with the house interval. An antenna can be installed at any point of the line, and is the same for every antenna.
Installing an antenna for MTC1 costs and installing one for MTC2 costs . The two carriers also install shared antennas. A shared antenna costs and supports both SIM types. The costs satisfy .
Given the houses, the SIM type of each owner, and the three costs, find the minimum total cost of a set of antennas that covers every house.
Input
The input holds several test cases. The first line of a test case has five positive integers , , , , . Here is the number of houses, is the coverage range of an antenna, is the installation cost of an MTC1 antenna, is the installation cost of an MTC2 antenna, and is the installation cost of a shared antenna. All three costs are at most . Each of the next lines describes one house with three integers , , , where is the house interval with , and is 1 or 2 and gives the owner's SIM type: 1 for MTC1, 2 for MTC2. The line after the last test case is 0 0 0 0 0 and must not be processed.
Output
For each test case, print one line with the minimum total cost of installing the antennas.