zoukankan      html  css  js  c++  java
  • B. Bear and Three Musketeers

    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 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 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
    这题其实就是问你能不能找到三个彼此认识的人,然后那三个人认识的人数和要最少。。。暴力枚举三个成环的情况,然后更新最少的认识人数和
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    vector<int>V[5000]; 
    int f[5000][5000];
    int main()
    {
        int n,m;
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            int x,y;
            for(int i=1;i<=n;i++)
            V[i].clear();
            memset(f,0,sizeof(f));
            for(int i=0;i<m;i++)
            {
                scanf("%d %d",&x,&y);
                V[x].push_back(y);
                V[y].push_back(x);
                f[x][y]=f[y][x]=1;
            }
            int ans=100000000;
            int flag=0;
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<V[i].size();j++)
                {
                    int k=V[i][j];
                    for(int l=0;l<V[k].size();l++)
                    {
                        int a=V[k][l];
                        if(f[i][k]&&f[k][a]&&f[a][i])
                        {
                            int sum=V[i].size()+V[k].size()+V[a].size()-6;
                          if(sum<ans)
                          {
                              ans=sum;
                              flag=1;
                          }
                        }
                    }
                }
            } 
            if(flag)
        printf("%d
    ",ans);
        else
        printf("-1
    ");
        }
        return 0;
    }

  • 相关阅读:
    linux驱动---等待队列、工作队列、Tasklets【转】
    Pinctrl子系统之一了解基础概念【转】
    Linux内存管理(最透彻的一篇)【转】
    linux驱动学习笔记---实现中断下半部以及驱动编写规范(七)【转】
    一些网址下载【转】
    Linux /proc/$pid部分内容详解【转】
    Linux kernel workqueue机制分析【转】
    Linux进程核心调度器之主调度器schedule--Linux进程的管理与调度(十九)【转】
    Linux Kernel PANIC(三)--Soft Panic/Oops调试及实例分析【转】
    Linux内核调试的方式以及工具集锦【转】
  • 原文地址:https://www.cnblogs.com/NaCl/p/9580165.html
Copyright © 2011-2022 走看看