zoukankan      html  css  js  c++  java
  • 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
    ",ans-6);
        }else{
            printf("-1
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    指针系统学习5-对使用字符指针变量和字符数组的讨论
    指针系统学习4-字符串与指针
    指针系统学习3-多维数组与指针
    指针系统学习2
    指针系统学习1
    你视为意见领袖的大 V,可能只是个僵尸号
    phpinfo()
    用shell脚本守护后台进程
    mysql命令(command)
    MySQL数据库视图(view),视图定义、创建视图、修改视图
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5678722.html
Copyright © 2011-2022 走看看