Codeforces Round #318 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.

这个题目就是说 有个人要找三个火枪手 ,这三个火枪手必须互相认识,然后他有N个备胎。。。是备选的人

然后给你m行 ,是这些人互相认识的关系,然后让你求一个组合,使得这个三人组里面的人除了认识组内成员,认识的人数之和最少。因为,认识的人太对说不行会被基友相爱相杀。。。。。。。

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
using namespace std;
vector < int > p[4100];
int map1[4100][4100];
int dis[4100];
int n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i = 1; i <= n; i++)
           for(int j = 1; j <= n; j++)
             map1[i][j] = 0;
        for(int i = 1; i <= n; i++) { p[i].clear(); dis[i] = 0; }
        int x,y;
        for(int i = 1; i <= m; i++ )
        {
            scanf("%d%d",&x,&y);
            map1[x][y] = map1[y][x] = 1;
            p[x].push_back(y);
            p[y].push_back(x);
            dis[x]++;
            dis[y]++;
        }
        int ans = 1000000;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < p[i].size(); j++ )
            {
                for(int k = j+1; k < p[i].size(); k++)
                {
                    x = p[i][j];
                    y = p[i][k];
                    if(map1[x][y]){
                        ans = min(ans, dis[i] + dis[x] +dis[y] - 6);
                    }
                }
            }
        }
        if(ans == 1000000) ans = -1;
        printf("%d\n",ans);
    }
    return 0;
}
      
      
     
     
    
    
   
   

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
vector < int > p[4100];
int map1[4100][4100];
int dis[4100];
int n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i = 1; i <= n; i++)
           for(int j = 1; j <= n; j++)
             map1[i][j] = 0;
        for(int i = 1; i <= n; i++) { p[i].clear(); dis[i] = 0; }
        int x,y;
        for(int i = 1; i <= m; i++ )
        {
            scanf("%d%d",&x,&y);
            map1[x][y] = map1[y][x] = 1;
            p[x].push_back(y);
            p[y].push_back(x);
            dis[x]++;
            dis[y]++;
        }
        int ans = 1000000;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < p[i].size(); j++ )
            {
                for(int k = j+1; k < p[i].size(); k++)
                {
                    x = p[i][j];
                    y = p[i][k];
                    if(map1[x][y]){
                        ans = min(ans, dis[i] + dis[x] +dis[y] - 6);
                    }
                }
            }
        }
        if(ans == 1000000) ans = -1;
        printf("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值