#P16153. [ICPC 2016 NAIPC] Fancy Antiques

[ICPC 2016 NAIPC] Fancy Antiques

题目描述

You are hosting a fancy party for fancy friends. And, like any fancy party, you need to buy some fancy antiques to put up around the venue (your house).

There is a set of nn fancy antiques that you need to buy. And there is a set of mm fancy antique shops in the city. Because these antiques are extremely rare, each fancy antique can only be found at a single fancy antique shop. However, the fancy antique shops can also sell “knock-off” (duplicate) versions of some of the antiques. And of course, for any fancy antique, there is only a single fancy antique shop in the city that holds a knock-off version of that antique (this is to maintain the rareness of the antiques). The shop that sells the original is not always the same shop that holds the knock-off.

It turns out that even though you can tell the difference, most people cannot tell the original version from the knock-off version of any given antique. And, because the shops can get away with it, sometimes the knock-off is more expensive than the original! Since the party is tomorrow, you only have time to visit kk shops. You would like to buy one version (either the original or the knock-off) of each of the nn antiques.

Suppose that there are three shops, and three antiques we would like to buy.

  • Antique #1 sells for 3030 at shop #1. Its knockoff sells for 5050 at shop #2.
  • Antique #2 sells for 7070 at shop #2. Its knockoff sells for 1010 at shop #3.
  • Antique #3 sells for 2020 at shop #3. Its knockoff sells for 8080 at shop #1.

Suppose you only have time to go to two shops. You can go to shops 1 and 3. You can buy the original of antique 1 with cost 3030 at shop 1, the original of antique 3 with cost 2020 at shop 3, and the knock-off of antique 2 at shop 3 with cost 1010. The total cost to buy these items is 6060, which is the minimum possible.

If you only have time to visit one shop, then it is impossible. You cannot buy a version of all three items by visiting a single shop.

Given the costs of the antiques/knock-offs at the shops, what is the minimum total cost to buy one version of each antique?

输入格式

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input will consist of three space-separated integers: nn, mm, and kk (1n1001 \leq n \leq 100, 1km401 \leq k \leq m \leq 40). The next nn lines will each have four space separated integers, aa, pp, bb and qq, describing an antique, where:

  • aa is the index of the shop that sells the original version of the antique (1am1 \leq a \leq m)
  • pp is the price of the original version of the antique at shop aa (1p1071 \leq p \leq 10^7)
  • bb is the index of the shop that sells the knock-off version of the antique (1bm1 \leq b \leq m)
  • qq is the price of the knock-off version of the antique at shop bb (1q1071 \leq q \leq 10^7)

输出格式

If it is possible to collect all of the antiques while visiting no more than kk stores, then output the minimum cost. If it is not possible, output 1-1.

3 3 2
1 30 2 50
2 70 3 10
3 20 1 80
60
3 3 1
1 30 2 50
2 70 3 10
3 20 1 80
-1