zoukankan      html  css  js  c++  java
  • 分糖果(BFS)

    题目描述

    童年的我们,将和朋友分享美好的事物作为自己的快乐。这天,C小朋友得到了糖果,将要把这些糖果分给要好的朋友们。已知糖果从一个人传给另一个人需要1秒的时间,同一个小朋友不会重复接受糖果。由于糖果足够多,如果某时刻某小朋友接受了糖果,他会将糖果分成若干份,分给哪些在他身旁且还没有得到糖果的小朋友们,而且自己会吃一些糖果。由于嘴馋,小朋友们等不及将糖果发完,会在得到糖果后边吃边发。每个小朋友从接收糖果到吃完糖果需要m秒的时间。那么,如果第1秒C小朋友开始发糖,第几秒所有小朋友都吃完了糖呢?

    输入

    第1行为三个整数n、p、c,为小朋友数、关系数和C小朋友的编号。(1<=n<=100000)

    第2行为一个数m,表示小朋友吃糖的时间。

    下面p行每行两个整数,表示某两个小朋友在彼此身旁。

    输出

    一个整数,为所有小朋友都吃完了糖的时间。

    样例输入

    4 3 1
    2
    1 2
    2 3
    1 4

    样例输出

    5 
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 const int INF=0x3f3f3f3f;
    13 typedef long long LL;
    14 using namespace std;
    15 
    16 struct node
    17 {
    18     int num;
    19     int step;
    20 };
    21 int MAX;
    22 vector<int> E[100010];
    23 bool vis[100010];
    24 queue<node> qe;
    25 
    26 void BFS()
    27 {
    28     while(!qe.empty())
    29     {
    30         node now=qe.front();
    31         qe.pop();
    32         for(int i=0;i<E[now.num].size();i++)
    33         {
    34             node to;
    35             to.num=E[now.num][i];
    36             to.step=now.step+1;
    37             if(!vis[to.num])
    38             {
    39                 vis[to.num]=true;
    40                 qe.push(to);
    41                 if(to.step>MAX)
    42                     MAX=to.step;
    43             }
    44         }
    45     }
    46 }
    47 
    48 int main()
    49 {
    50     #ifdef DEBUG
    51     freopen("sample.txt","r",stdin);
    52     #endif
    53     
    54     int n,p,c,m;
    55     scanf("%d %d %d %d",&n,&p,&c,&m);
    56     for(int i=1;i<=p;i++)
    57     {
    58         int a,b;
    59         scanf("%d %d",&a,&b);
    60         E[a].push_back(b);
    61         E[b].push_back(a);
    62     }
    63     node st;
    64     st.num=c;
    65     st.step=1;
    66     qe.push(st);
    67     BFS();
    68     printf("%d
    ",MAX+m);
    69     
    70     return 0;
    71 }

    -

  • 相关阅读:
    感知机预测NBA总冠军
    java 一维数组
    2020-11-25
    2020-11-24学习日记
    Java语言概述
    人脸情绪识别系统---测试心得
    结对编程,问题不大
    结对编程之队友代码赏析
    项目测试心得——基于微信的图书销售小程序
    数据库设计心得
  • 原文地址:https://www.cnblogs.com/jiamian/p/12188202.html
Copyright © 2011-2022 走看看