codeforces-102A Clothes(暴力)

链接:http://codeforces.com/problemset/problem/102/A
题意:给定n件衣服,每件衣服有价格,有m中配对关系,求最小的价格从中选取三件衣服,使得它们之间能两两配对

题解:n很小直接暴力

#include<iostream>
#include<cstring>
#include<map>
#include<cstdio>
#include<algorithm>
using namespace std;

int n, m;
int ma[110][110];
int a[110];

int main(){
    cin>>n>>m;
    for(int i = 1; i<=n; i++) cin>>a[i];
    for(int i = 1; i<=m; i++){
        int u, v;
        cin>>u>>v;
        ma[u][v] = ma[v][u] = 1;
    }
    int res = 0x3f3f3f3f;
    for(int i = 1; i<=n; i++){
        for(int j = i+1; j<=n; j++){
            for(int k = j+1; k<=n; k++){
                if(ma[i][j] && ma[j][k] && ma[i][k]) res = min(res, a[i]+a[j]+a[k]);
            }
        }
    }

    if(res == 0x3f3f3f3f){
        cout<<"-1"<<endl;
    }
    else cout<<res<<endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/grimcake/article/details/79966364