CodeForces 102A
来源:互联网 发布:光伏美工图 编辑:程序博客网 时间:2024/04/30 00:11
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
3 31 2 31 22 33 1
6
n代表衣服件数,m代表有m对衣服是可以搭配的;
思路:用一个二维数组存放衣服之间的关系,若i和j可以搭配,则rel[i][j] == rel[j[i] == 1;
然后遍历
#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);
}
}
- CodeForces 102A - flody
- CodeForces 102A
- CodeForces-a
- Codeforces 903A A
- CodeForces 18A A - Triangle
- CodeForces 133A A. HQ9+
- codeforces 651A A. Joysticks
- 【Codeforces 597A】A. Divisibility
- 【 Codeforces 615A】A. Bulbs
- A. Divisibility CodeForces 597A
- codeforces 417A A. Elimination
- CodeForces 534A A. Exam
- codeforces#98 a
- codeforces#99 a
- codeforces 105 div2 A
- codeforces #121 A
- Codeforces 185A Plant
- codeforces 183A Headquarters
- 编写的windows程序,崩溃时产生crash dump文件的办法
- redis.conf中文版
- 用UltraEidt修改dmp文件把内容导到指定的表空间
- Python - 单例模式(Singleton)
- 烦
- CodeForces 102A
- ARM汇编中用宏和子程序实现1+2+。。。+N
- omap3530从SD卡启动Error: reading boot sector解决笔记
- m2eclipse issues ArtifactTransferException
- 关于go语言des加解密算法
- Unity3d赋予角色生命动画状态机动画状态 (Animation State)
- linux常用命令精华部分
- xmanager3.0使用Xstart不能连接redhat5.4
- spring,structs2和Mybatis整合以及单元测试