codeForces 574b Bear and Three Musketeers

寻找最优三剑客
本文介绍了一个关于选择三位战士作为三剑客的问题。通过构建无向图并利用暴力枚举的方法来找出三个互相认识且知名度之和最小的战士。文章详细展示了如何遍历所有可能的组合,并最终确定最优解。
一种巧妙到暴力方式,这题到抽象化:在一个无向图中,找一个度数和最小到三阶到圈。
首先对边进行枚举,这样确定了两个顶点,然后在对点进行枚举,找到一个可以构成三元圈到点,则计算他们到度数和。
最后保存最小到度数和到三元圈即可。
 
Bear and Three Musketeers
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

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 ≤ nai ≠ 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 Input

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


#include<iostream>
#include<stdio.h>
using namespace std;
const int maxn = 4005;
struct Node{
    int a,b;
}edg[maxn];
int mapp[maxn][maxn];
int deg[maxn];
int main(){
     int n,m;
     scanf("%d%d",&n,&m);
    for(int i=0;i<=n;i++)
      for(int j=0;j<=n;j++)
        mapp[i][j]=0;
    for(int i=0;i<=n;i++) deg[i]=0;
    for(int i=0;i<m;i++){
        int tmp1,tmp2;
        scanf("%d%d",&tmp1,&tmp2);
        edg[i].a=tmp1;
        edg[i].b=tmp2;
        mapp[tmp1][tmp2]=mapp[tmp2][tmp1]=1;
        deg[tmp1]++;
        deg[tmp2]++;
    }
    int inf=0x7fffffff;
    int ans=inf;
    for(int i=0;i<m;i++){
        for(int j=1;j<=n;j++){
            if(mapp[j][edg[i].a]&&mapp[j][edg[i].b]){
                ans=min(ans,(deg[j]+deg[edg[i].a]+deg[edg[i].b]));
            }
        }
    }
    if(ans<inf){
        printf("%d\n",ans-6);
    }else{
        printf("-1\n");
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/superxuezhazha/p/5678722.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值