Codeforces 574 B. Bear and Three Musketeers(暴力枚举)详解新手进

Codeforces 574 B. Bear and Three Musketeers

Do you know a story about the three musketeers? Anyway, you will learn about its origins now.
Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys.
There are n warriors. Richelimakieu wants to choose three of them to become musketeers but it’s not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn’t be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers.
Help Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions.

Input
The first line contains two space-separated integers, n and m (3 ≤ n ≤ 4000, 0 ≤ m ≤ 4000) — respectively number of warriors and number of pairs of warriors knowing each other.
i-th of the following m lines contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Warriors ai and bi know each other. Each pair of warriors will be listed at most once.

Output
If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print “-1” (without the quotes).

Sample test(s)

Input
5 6
1 2
1 3
2 3
2 4
3 4
4 5

Output
2

Input
7 4
2 1
3 6
5 1
1 7

Output
-1

题意:

给你n个人,m条关系,找到三个互相认识的人,计算他们认识的人当中,除啦他们三个,认识的人的总和。如果找不到三个互相认识的人,就输出-1.

思路

利用二维数组存关系,然后用for循环暴力枚举,有点类似与Floyd算法,都是三个for循环枚举,就是一个无向图

在这里插入图片描述

我们要做的就是要用二维数组来标记人与人直接是否认识,这用二维数组就能搞定,然后还有开一个一维数组来记录每个人认识的人,然后用for循环遍历,找到三个相互认识的人,在把他们认识的人的个数加起来,在减去6(我一开始忘记减6,死活过不了样例,就是不知道哪错了)
上代码,有详细注释

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int map[5000][5000];
int main()
{
 int n,m;
 int x,y;
 int in[5000];
 while(~scanf("%d%d",&n,&m))
 { 
  int ans=inf;//认识的人 
  memset(map,0,sizeof(map));
  memset(in,0,sizeof(in));
  int flag=0;//记录是否有三个人互相认识的情况 
  for(int i=0;i<m;i++)
  {
   scanf("%d%d",&x,&y);
   if(!map[x][y])//防止重复输入 
   {
    map[x][y]=map[y][x]=1;//因为是无向图 
    in[x]++;
    in[y]++;
   }
  }
 
  for(int i=1;i<=n;i++)//i表示第一个人 
   for(int j=i+1;j<=n;j++)//j表示第二个人 
   {
    if(map[i][j])//当这两个人认识的时候才能找第三个人 
    {
     for(int k=j+1;k<=n;k++)
     {
      if(map[i][k]&&map[k][j])//第一个和第三个人认识,并且第三个和第二个也认识 
      {
       ans =min(ans,in[i]+in[j]+in[k]);//取最小值 
       flag=1;
      } 
     }
    }
   }
  if(flag)
   printf("%d\n",ans-6);//千万别忘了减6 
  else
   printf("-1\n");
 }
 
 return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值