ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2829. Clothes

2021-08-03 19:59:47  阅读:205  来源: 互联网

标签:temp int items price result Clothes clothing 2829


time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A little boy Gerald entered a clothes shop and found out something very unpleasant: not all clothes turns out to match. For example, Gerald noticed that he looks rather ridiculous in a smoking suit and a baseball cap.

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.

Input

The first input file line contains integers n and m − the total number of clothing items in the shop and the total number of matching pairs of clothing items (

).

Next line contains n integers ai (1≤ai≤106) − the prices of the clothing items in rubles.

Next m lines each contain a pair of space-separated integers ui and vi (1≤ui,vin,uivi). Each such pair of numbers means that the ui-th and the vi-th clothing items match each other. It is guaranteed that in each pair ui and vi are distinct and all the unordered pairs (ui,vi) are different.

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).

Examples

Input

3 3
1 2 3
1 2
2 3
3 1

Output

6

Input

3 2
2 3 4
2 3
2 1

Output

-1

Input

4 4
1 1 1 1
1 2
2 3
3 4
4 1

Output

-1

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.

 这个题目。真的。太暴力搜索了,数目也没有很大,可以完全不要技巧哈哈,三重循环

注意它搜索的时候一队的两个元素都要比较,代码复制粘贴就可以

#include<iostream>
using namespace std;
//暴力搜索 
int main(){
	int n,m;
	cin>>n>>m;
	int price[n];
	for (int i=0; i<n; i++)
	cin>>price[i];
	int result=-1;
	int u[m][2];
	for (int i=0;i<m;i++)
	{
		cin>>u[i][0]>>u[i][1];
	}
	for (int i=0; i<m; i++){
		int temp1,temp2,temp3;
		temp1=u[i][0],temp2=u[i][1];
		for (int j=0; j<m; j++){
			if (u[j][0]==temp2)
			{
				temp3=u[j][1];
				for (int k=0; k<m; k++){
					if(u[k][0]==temp3){
						if (u[k][1]==temp1)
						{
							int temp=price[temp1-1]+price[temp2-1]+price[temp3-1];
							if (result==-1)
								result=temp;
							else if (result>temp)
								result=temp;
							break;
						}
					}
					if(u[k][1]==temp3){
						if (u[k][0]==temp1)
						{
							int temp=price[temp1-1]+price[temp2-1]+price[temp3-1];
							if (result==-1)
								result=temp;
							else if (result>temp)
								result=temp;
							break;
						}
					}
				}
				break;
			}
			if (u[j][1]==temp2)
			{
				temp3=u[j][0];
				for (int k=0; k<m; k++){
					if(u[k][0]==temp3){
						if (u[k][1]==temp1)
						{
							int temp=price[temp1-1]+price[temp2-1]+price[temp3-1];
							if (result==-1)
								result=temp;
							else if (result>temp)
								result=temp;
							break;
						}
					}
					if(u[k][1]==temp3){
						if (u[k][0]==temp1)
						{
							int temp=price[temp1-1]+price[temp2-1]+price[temp3-1];
							if (result==-1)
								result=temp;
							else if (result>temp)
								result=temp;
							break;
						}
					}
				}
				break;
			}
		}
	} 
	cout<<result<<endl;
	return 0;
} 

END

标签:temp,int,items,price,result,Clothes,clothing,2829
来源: https://blog.csdn.net/qq_51671152/article/details/119357604

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有