Codeforces 574 B. Bear and Three Musketeers

Cilck Here~~

B. Bear and Three Musketeers

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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 hisrecognition 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 andm (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).
Warriorsai andbi 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个关系,(a,b)表示a和b认识,现在这个人想雇佣其中3个人,

1: 这3个人必须相互认识

2: 这3个人的识别度的总和最小(一个人的识别度为:除了另外两人认识的人的数,三个人的识别度相加最小)

解题思路:

类似于Floyd算法,但是不完全是,这其实是一个图。。。

上代码:

/*
2015 - 09 - 14 晚上

Author: ITAK  

Motto:  

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 4e3+5;
const double eps = 1e-7;

int x[maxn],  y[maxn];
int arr[maxn];
bool g[maxn][maxn];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d",&x[i],&y[i]);
        g[x[i]][y[i]] = g[y[i]][x[i]] = 1;
        arr[x[i]]++;
        arr[y[i]]++;
    }
    bool ok= false;
    int Min = 999999;
    for(int i=0; i<m; i++)
        for(int j=1; j<=n; j++)
            if(g[x[i]][j] && g[j][y[i]] )
                if(Min > arr[x[i]]+arr[j]+arr[y[i]])
                {
                    Min = arr[x[i]]+arr[j]+arr[y[i]];
                    ok = true;
                }
    if (!ok)
        puts("-1");
    else
        printf("%d\n",Min-6);
    return 0;
}
/*

南山南 (Live) - 张磊
词:马頔
曲:马頔
你在南方的艳阳里大雪纷飞
我在北方的寒夜里四季如春
如果天黑之前来得及
我要忘了你的眼睛
穷极一生 做不完一场梦
他不再和谁谈论相逢的孤岛
因为心里早已荒无人烟
他的心里再装不下一个家
做一个只对自己说谎的哑巴
他说你任何为人称道的美丽
不及他第一次遇见你
时光苟延残喘 无可奈何
如果所有土地连在一起
走上一生 只为拥抱你
喝醉了他的梦 晚安
你在南方的艳阳里大雪纷飞
我在北方的寒夜里四季如春
如果天黑之前来得及
我要忘了你的眼睛
穷极一生 做不完一场梦
大梦初醒 荒唐了一生
南山南 北秋悲
南山有谷堆
南风喃 北海北
北海有墓碑
南山南 北秋悲
南山有谷堆
南风喃 北秋悲
北海有墓碑
北海有墓碑

*/
时间: 2025-01-29 22:12:51

Codeforces 574 B. Bear and Three Musketeers的相关文章

Codeforces 574 A. Bear and Elections

cilck here ~~ ***A. Bear and Elections*** Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are n candidates, including Limak. We know how many citizens are going to vote

CodeForces 653 A. Bear and Three Balls——(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2))

传送门 A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each of

Codeforces 573 A. Bear and Poker

click here~~ A. Bear ***and Poker*** Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid

codeforces 336C Vasily the Bear and Sequence

点击打开codeforce 336C 思路; 位运算 分析: 1 题目要求找到k个数,使得这k个数的&的结果能够被2^v整除,并且v的值应该要尽量的大 2 一个数能够被2^v整数,说明这个数的二进制的最右边的1后面刚好是v个0,比如20的二进制为10100,那么刚好可以被2^2整除,那么就有最右边的1的后面0的个数刚好为2个 3 那么我们可以通过从大到小枚举v,然后我们去n个数里面找满足"这个数的二进制的最右边的1后面刚好是v个0",然后进行求&运算,只要我们找到一个满

Codeforces Round #157 (Div. 1) C. Little Elephant and LCM (数学、dp)

C. Little Elephant and LCM time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output The Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The

Codeforces B. Taxi 算法题解

简单总结题意: 有一个数组,数组中的数值不会大于4,即只能是1,2,3,4,要把这些数值装进一个容量最大为4的容器里面,使得所用到这样的容器的个数最小. 经测试数据很大,会有10万个数据,所以这里我并不用排序数据,而是使用counting sort的思想,根据特定的数据优化,使得题解时间复杂度为O(n). 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 程序如下: #include <iostream>

CodeForces 233B Non-square Equation

链接: http://codeforces.com/problemset/problem/233/B 题目: B. Non-square Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's consider equation: x2+s(x)·x-n=0, where x,n are positive

CodeForces:200C: Football Championship

地址链接: CF:  http://codeforces.com/problemset/problem/200/C HUST Virtual Judge: http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=28923 题目: C. Football Championship time limit per test 2 seconds memory limit per test 256 megabytes input s

Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)

Valera has array a, consisting of n integers a0,a1,...,an-1, and function f(x), taking an integer from 0 to 2n-1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number xconta