P10102: 买衣服
传统题
2.000s
时间限制
256MB
内存限制
1 提交
1 解决
【题目描述】
【
题目描述】
小男孩杰拉尔德走进一家服装店,发现了一件非常不愉快的事情:
并非所有的衣服都很相配。例如,杰拉尔德注意到他穿着吸烟服,戴着棒球帽,看起来相当可笑。
这家店总共卖了n
件衣服,正好有m对衣服相配。每件物品都有其价格,用整数卢布表示。杰拉尔德想买三件互相搭配的衣服。此外,他想花尽可能少的钱。找出他能花的最少的钱。
【
输入】
第一个输入文件行包含整数n
和m,分别表示
商店中服装的总数和服装的匹配对的总数。

下一行包含n
个整数ai(1≤ai≤106),
以卢布为单位的服装价格。
接下来的m
行每一行包含一对空格分隔的整数ui和vi(1≤ui,vi≤n,ui≠vi)。每一对这样的数字都意味着第ui
和第vi件衣服相互匹配。保证在每一对中ui和vi是不同的,并且所有的无序对(ui,vi)都是不同的。
【
输出】
输
出唯一的数字——杰拉尔德在商店里需要支付的最少的卢布金额。如果商店没有三件相互匹配的衣服,则输出
“-1”(不带引号)。
【
输入】
3 3
1 2 3
1 2
2 3
3 1
【
输出】
6
【
输入】
3 2
2 3 4
2 3
2 1
【
输出】
-1
【
输入】
4 4
1 1 1 1 1
1 2
2 3
3 4
4 1
【
输出】
-1
【样例说明】
在第一个测试中,只有三件衣服,它们都是相互匹配的。因此,只有一种方法——买这三件衣服;在这种情况下,他花了6卢布。
第二次测试也只有三件衣服,但是杰拉德买不到,因为第一件衣服和第三件衣服不匹配。因此,不存在三件相配的衣服。答案是-1
。
在第三个例子中,有4
件衣服,但杰拉德不能同时买其中的任何3件。答案是-1。
【提示】
Overall the shop sells n clothing items, and exactly m pairs of clothing items match. Each item has its price, represented by an integer number of rubles. Gerald wants to buy three clothing items so that they matched each other. Besides, he wants to spend as little money as possible. Find the least possible sum he can spend.
Output
Print the only number − the least possible sum in rubles that Gerald will have to pay in the shop. If the shop has no three clothing items that would match each other, print "-1" (without the quotes).
Note
In the first test there only are three pieces of clothing and they all match each other. Thus, there is only one way − to buy the 3 pieces of clothing; in this case he spends 6 roubles.
The second test only has three pieces of clothing as well, yet Gerald can't buy them because the first piece of clothing does not match the third one. Thus, there are no three matching pieces of clothing. The answer is -1.
In the third example there are 4 pieces of clothing, but Gerald can't buy any 3 of them simultaneously. The answer is -1.