Clothes
来源:互联网 发布:达芬奇14mac怎么改中文 编辑:程序博客网 时间:2024/05/01 20:57
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 exactlym 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 andm — 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 integersui andvi (1 ≤ ui, vi ≤ n, ui ≠ vi). Each such pair of numbers means that the ui-th and thevi-th clothing items match each other. It is guaranteed that in each pairui andvi 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 31 2 31 22 33 1
6
3 22 3 42 32 1
-1
4 41 1 1 11 22 33 44 1
-1
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.
大致题意:商店中有n件衣服,分别有n个价格,给出m组样例,分别表示每个衣服的所对应的衣服号数,问题是求能够匹配的三件衣服所需要的最少的钱数。
解决方法:直接用暴力搜索就可以求出,注意最大值的范围用无穷大oxffffff。
#include <stdio.h>int cloth[101][101], price[101];int main(){ int n, m, i, j, k, x, y, cost, min; while(scanf("%d%d", &n, &m)!=EOF) { cost=0; min=0xffffff; for(i=1; i<=n; i++) scanf("%d", &price[i]); for(i=0; i<m; i++) { scanf("%d%d", &x, &y); cloth[x][y]=cloth[y][x]=1; } for(i=1; i<=n; i++) { for(j=1; j<=n; j++) { if(i!=j) { for(k=1; k<=n; k++) { if(i!=k&&k!=j) { if(cloth[i][j]&&cloth[j][k]==1&&cloth[k][i]==1) { cost=price[i]+price[j]+price[k]; if(cost<min) min=cost; } } } } } } if(min==0xffffff) printf("-1\n"); else printf("%d\n", min); } return 0;}
- clothes
- Clothes
- Clothes Phrase
- POJ3211Washing Clothes
- no one wears clothes
- POJ 3211 Washing Clothes
- POJ 3211 Washing Clothes
- Poj 3211 Washing Clothes
- POJ 3211 Washing Clothes
- POJ3211 Washing Clothes
- poj 3211 Washing Clothes
- English expressions about clothes
- poj3211 Washing Clothes
- POJ 3211 Washing Clothes
- POJ 3211Washing Clothes
- poj 3211 Washing Clothes
- 【vector】#79 A. Clothes
- a change of clothes
- 开发线程安全的Servlet
- Java判断两个时间段是否有交集
- HTML5 Canvas绘图API
- PAT 1083. List Grades (25)
- QQ空间g_tk算法的JS脚本的获取和分析
- Clothes
- 代沐研:非农战幕终拉开,金银油空头强袭
- 忆昔悟今
- 应用程序的 DIY__(给程序加上新功能)
- 9.3 - 抗战胜利日
- 自我学习
- strcpy,strnpy和strlcpy的区别
- Codeforces 460d Littel victor and set
- usaco Longest Prefix