Pick at most k shops so that every item can be bought (original or knock-off) at a visited shop, minimizing total price.
Medium7Brute forceBit manipulationGreedyImplementationNo attempts yetTime limit10sMemory limit512 MBYou are throwing a party at your house tomorrow, and you want to decorate the place with antiques.
There are n antiques you want to buy, and the city has m antique shops. The antiques are so rare that exactly one shop in the city sells the original of any given antique. Shops also sell knock-off copies, and for each antique exactly one shop in the city sells the knock-off. The shop that sells the original is not always the shop that sells the knock-off.
Most people cannot tell an original from a knock-off, so either version decorates the house just as well. Each shop sets its own prices, so a knock-off is sometimes more expensive than the original. The party is tomorrow, so you have time to visit at most k shops. For each of the n antiques you must buy exactly one version, original or knock-off, and you can buy it only at a shop you visit.
Consider three shops and three antiques you want to buy.
If you have time for two shops, visit shops 1 and 3. Buy the original of antique 1 for 30 at shop 1, the knock-off of antique 2 for 10 at shop 3, and the original of antique 3 for 20 at shop 3. The total is 60, and no other pair of shops is cheaper. If you have time for one shop, no single shop carries a version of all three antiques, so the trip is impossible.
Find the minimum total cost of buying one version of every antique while visiting at most k shops.
The first line has three space separated integers n, m, and k (1≤n≤100, 1≤k≤m≤40): the number of antiques you want, the number of shops in the city, and the number of shops you have time to visit.
Each of the next n lines has four space separated integers a, p, b, and q describing one antique.
a and b can be the same shop.
Print the minimum total cost of buying one version of every antique while visiting at most k shops. If no set of at most k shops carries a version of every antique, print -1.