Friends Calling Plan
Time limit1sMemory limit128 MB
Given call minutes between up to 16 employees, pair them all up to minimize total billing cost using bitmask DP over perfect matchings.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Bit manipulation, Combinatorics
- Solved
- No attempts yet
Problem
Clarke runs a small company. Because of the nature of the work, every employee was given a mobile phone so they can talk with other employees, and Clarke pays all of their phone bills. He wants to cut those bills as much as possible.
The phone company offers a 'friends' service: two people who often call each other may form a 'friends' pair, which makes the calls between the two of them cheaper. Each person may belong to at most one friends pair, and the price of talking with anyone else stays the same.
The price of a one-minute call is for a friends pair and otherwise. In other words, a single -minute call between two employees costs if the two are a friends pair, and otherwise. The total bill is the sum of the cost of every call.
Given last month's list of calls (who talked with whom and for how long), pair all employees into friends pairs so that the company's total phone bill is as small as possible. Compute the smallest possible total bill.
Input
The first line contains two integers and separated by a space (). is the price of a one-minute call for a friends pair, and is the price of a one-minute call for people who are not a friends pair.
The second line contains an even integer (), the number of employees, numbered from to .
The third line contains an integer (), the number of calls.
Each of the next lines contains three integers , , and separated by spaces (, , ), meaning that employees and had a call lasting minutes.
Output
Print, on a single line, the smallest possible total phone bill, i.e. the minimum total cost achievable by pairing all employees into friends pairs in the best possible way.