CodeForces 102A

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, vi ≤ n, ui ≠ vi). 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).

Sample Input

Input
3 3
1 2 3
1 2
2 3
3 1
Output
6
  题意:向上服装店买三件衣服,使得任意两件可以搭配,并且花的钱最少

 n代表衣服件数,m代表有m对衣服是可以搭配的;


思路:用一个二维数组存放衣服之间的关系,若i和j可以搭配,则rel[i][j] == rel[j[i] == 1;

然后遍历




#include <iostream>
#include <string.h>
#include <stdio.h>


#define maxn 100 + 10
int rel[maxn][maxn];


using namespace std;


int main()
{
    long long  int n,m;
    int x,y;
    int ans;
    int a[110];
    while(cin>>n>>m)
    {
        ans = 0;
        memset(rel,0,sizeof(rel));
        for( int i = 1 ; i <= n; i++)
            scanf("%d",&a[i]);
        for( int i = 1; i  <= m ; i++)
        {
            scanf("%d%d",&x,&y);
            rel[y][x] = rel[x][y] = 1;
        }
        for( int i = 1; i <= n-2; i++)
            for( int j = i+1; j <= n-1; j++)
                for( int k = j+1; k <= n; k++)
                {
                    int sum = 0;
                    if(rel[i][j]&& rel[i][k]&& rel [j][k])
                    {


                        sum = a[i] + a[j] + a[k];


                        if(ans == 0)
                            ans = sum;
                        else if(sum < ans)
                            ans = sum;
                    }
                }
        if(ans == 0)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值