5821: Clothes(暴力?)

本题描述了一个小男孩Gerald面临的问题:如何从一家售卖多种衣物的商店中选择三件相互搭配的衣物,并且使得花费最少。输入包括衣物总数、可搭配组合数及各衣物价格等信息。

5821: Clothes

时间限制: 2 Sec   内存限制: 256 MB
提交: 235   解决: 49
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

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.

输入

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 (3≤n≤100,0≤m≤n(n-1)/2).

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.

输出

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

样例输入

3 3
1 2 3
1 2
2 3
3 1

样例输出

6

提示

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.

来源

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;
 
int main()
{
    int m, n, a[105], minn=9999999, map[105][105], x, y, ans;
    scanf("%d%d", &n, &m);
    for(int i=1;i<=n;i++)
        scanf("%d", &a[i]);
    memset(map, 0, sizeof(map));
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d", &x, &y);
        map[x][y]=map[y][x]=1;
    }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(map[i][k] && map[k][j] && map[i][j])
                {
                    ans=a[i]+a[j]+a[k];
                    if(ans<minn)
                        minn=ans;
                }
    if(minn==9999999)
        printf("-1\n");
    else
        printf("%d\n", minn);
    return 0;
}
 
/**************************************************************
    Problem: 5821
    User: ldu_reserver201701
    Language: C++
    Result: 正确
    Time:68 ms
    Memory:1092 kb
****************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hhjian6666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值