zoukankan      html  css  js  c++  java
  • LA 6450 social advertising(dfs剪枝)

    6450 Social Advertising
    You have decided to start up a new social networking company. Other existing popular social networks
    already have billions of users, so the only way to compete with them is to include novel features no
    other networks have.
    Your company has decided to market to advertisers a cheaper way to charge for advertisements (ads).
    The advertiser chooses which users’ “wall” the ads would appear on, and only those ads are charged.
    When an ad is posted on a user’s wall, all of his/her friends (and of course the user himself/herself)
    will see the ad. In this way, an advertiser only has to pay for a small number of ads to reach many
    more users.
    You would like to post ads to a particular group of users with the minimum cost. You already have
    the “friends list” of each of these users, and you want to determine the smallest number of ads you have
    to post in order to reach every user in this group. In this social network, if A is a friend of B, then B
    is also a friend of A for any two users A and B.
    Input
    The input consists of multiple test cases. The first line of input is a single integer, not more than
    10, indicating the number of test cases to follow. Each case starts with a line containing an integer n
    (1 ≤ n ≤ 20) indicating the number of users in the group. For the next n lines, the ith line contains the
    friend list of user i (users are labelled 1, . . . , n). Each line starts with an integer d (0 ≤ d < n) followed
    by d labels of the friends. No user is a friend of himself/herself.
    Output
    For each case, display on a line the minimum number of ads needed to be placed in order for them to
    reach the entire group of users.
    Sample Input
    2
    5
    4 2 3 4 5
    4 1 3 4 5
    4 1 2 4 5
    4 1 2 3 5
    4 1 2 3 4
    5
    2 4 5
    2 3 5
    1 2
    2 1 5
    3 1 2 4
    Sample Output
    1
    2

    题目大意:打广告搞宣传,有许多朋友关系,一个人做一下广告可以让他的n个朋友还有他自己看到。求找最少的做广告的人。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <vector>
     5 using namespace std;
     6 
     7 const int maxn=25;
     8 int n,flag;
     9 vector<int> f[maxn];
    10 int vis[maxn];
    11 int cnt[maxn];
    12 
    13 bool is_ok(int s)
    14 {
    15     memset(vis,0,sizeof(vis));
    16     int i,j,sum=0;
    17     for(i=0;i<s;i++)
    18     {
    19         if(!vis[cnt[i]])
    20         { 
    21             vis[cnt[i]]=1;sum++;
    22         }
    23         for(j=0;j<f[cnt[i]].size();j++)
    24         {
    25             if(!vis[f[cnt[i]][j]])
    26             {
    27                 vis[f[cnt[i]][j]]=1;sum++;
    28             }
    29         }
    30     }
    31     if(sum==n) return 1;
    32     return 0;
    33 }
    34 
    35 void dfs(int now,int s,int dep)
    36 {
    37     if(now>n+1) return ;
    38     if(s==dep)
    39     {
    40         if(is_ok(s)) flag=1;
    41         return ;
    42     }
    43     cnt[s]=now;
    44     dfs(now+1,s+1,dep);
    45     dfs(now+1,s,dep);
    46 }
    47 int main()
    48 {
    49     int t,i,k,p;
    50     scanf("%d",&t);
    51     while(t--)
    52     {
    53         scanf("%d",&n);
    54         for(i=1;i<=n;i++) f[i].clear();
    55         for(i=1;i<=n;i++)
    56         {
    57             scanf("%d",&k);
    58             while(k--)
    59             {
    60                 scanf("%d",&p);
    61                 f[i].push_back(p);f[p].push_back(i);
    62             }
    63         }
    64         flag=0;
    65         for(i=1;i<=n;i++)
    66         {
    67             dfs(1,0,i);
    68             if(flag) break;
    69         }
    70         printf("%d
    ",i);
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    时间复杂度和空间复杂度
    七、vue计算属性
    六、vue侦听属性
    四、vue派发更新
    五、vue nextTick
    三、vue依赖收集
    二、vue响应式对象
    递归
    链表
    TypeScript类型定义文件(*.d.ts)生成工具
  • 原文地址:https://www.cnblogs.com/xiong-/p/3884786.html
Copyright © 2011-2022 走看看