• 1076 Forwards on Weibo


    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤1000), the number of users; and L (≤6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:

    M[i] user_list[i]
    

    where M[i] (≤100) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.

    Then finally a positive K is given, followed by K UserID's for query.

    Output Specification:

    For each UserID, you are supposed to print in one line the maximum potential amount of forwards this user can trigger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.

    Sample Input:

    1. 7 3
    2. 3 2 3 4
    3. 0
    4. 2 5 6
    5. 2 3 1
    6. 2 3 4
    7. 1 4
    8. 1 5
    9. 2 2 6

    Sample Output:

    1. 4
    2. 5

    题目大意:转发微信,粉丝转发,粉丝的粉丝也可以转发,到达一定传递层数粉丝的粉丝就不会转发了,问对一个人来说最多传递几个。

    思路:可以将粉丝之间的关系看成城市间的距离,找到每个城市之间的最小距离,然后这个最小距离小于对应的层数就可以统计出对应的人数了,即用floyd算法,唯一需要注意的是在输入的格式是他关注的人,而不是关注他的人。

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. #define inf 9999999
    4. int main(){
    5. int n,m;
    6. scanf("%d%d",&n,&m);
    7. int i;
    8. int a[n+1][n+1];
    9. fill(a[0],a[0]+(n+1)*(n+1),inf);//初始化操做
    10. for(i=1;i<=n;i++)a[i][i]=0;
    11. int num,j;
    12. for(i=1;i<=n;i++){
    13. scanf("%d",&num);
    14. for(j=0;j<num;j++){//规定a[i][j]为i对j节点的贡献度。
    15. int val;
    16. scanf("%d",&val);
    17. a[i][val]=1;
    18. }
    19. }
    20. int k;
    21. for(i=1;i<=n;i++){//找到各个介点的最近关系
    22. for(j=1;j<=n;j++){
    23. for(k=1;k<=n;k++){
    24. if(a[j][k]>a[j][i]+a[i][k]){
    25. a[j][k]=a[j][i]+a[i][k];
    26. }
    27. }
    28. }
    29. }
    30. int cha;
    31. scanf("%d",&cha);
    32. int nums[n+1];
    33. memset(nums,0,sizeof(nums));
    34. for(i=1;i<=n;i++){//枚举每个节点对该点贡献度,即他关注的对象并转发的数量
    35. for(j=1;j<=n;j++){
    36. if(j!=i&&a[i][j]<=m)nums[j]++;
    37. }
    38. }
    39. for(i=0;i<cha;i++){
    40. scanf("%d",&num);
    41. printf("%d\n",nums[num]);
    42. }
    43. system("pause");
    44. }

  • 相关阅读:
    《数字图像处理-OpenCV/Python》连载(9)多帧图像的读取与保存
    Python 霸榜的一周,又有什么新 AI 力作呢?「GitHub 热点速览」
    java springboot application中设置正确的数字密码连不上数据库问题解决
    学习Java的第三十二天。。。(XML)
    折半搜索-oier复健练习题目
    GPT访问跨域如何解决呢?
    Pandas实现一列切分为多列
    [附源码]java毕业设计高校社团信息管理系统
    servlet 教程 1:环境搭建和新建 servlet 项目
    新品发布!OPT 3D激光轮廓扫描仪产品阵容再升级
  • 原文地址:https://blog.csdn.net/weixin_61168459/article/details/125521321