zoukankan      html  css  js  c++  java
  • hust 1045 Task Arrangement II

    题目描述

    There are N tasks and M resources, each task is asked to use some resources and each resource can only be used by at most one task. You are asked to find the best arrangement to maximum the total number of the compatible tasks.

    输入

    There are multi test cases. Each case contains a number of lines. The first line is N and M, 1 <= N, M <= 50. In the following N lines, each task contains one line with many integers separated by exactly one space, which specify the required resources. The index of the resources starts from 0.

    输出

    For each test case, output one line containing the maximum number of the compatible tasks.

    样例输入

    5 5
    0 3 1
    1 4
    2 1
    0
    3 2
    3 3
    1 0
    0 2
    1 2

    样例输出

    3
    1

    又是一道经典题目,先说说我对这个题的感觉吧!一开始认为是一道二分图匹配的题目,可是怎么弄都不清楚,后来又想是不是网络流的题目,唉!还是做不出来,在后来就直接搜索,肯定超时,怎么办,于是想到是不是最大团,结果还真是,两个没有公共资源的任务便可以连一条边,这样不就是求最大团吗?真的很经典
    #include<map>
    #include<set>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
    
    using namespace std;
    
    const double pi=acos(-1.0);
    const double eps=1e-8;
    typedef pair<int,int>pii;
    vector<int>task[51];
    
    void init(int n)
    {
         for (int i=0;i<=n;i++)
         task[i].clear();
    }
    
    struct max_clique
    {
        static const int N=100;
        bool G[N][N];
        int n,Max[N],Alt[N][N],ans;
    
        bool DFS(int cur,int tot)
        {
            if (cur==0)
            {
                if (tot>ans)
                {
                    ans=tot;
                    return true;
                }
                return false;
            }
            for (int i=0;i<cur;i++)
            {
                if (cur-i+tot<=ans) return false;
                int u=Alt[tot][i];
                if (Max[u]+tot<=ans) return false;
                int nxt=0;
                for (int j=i+1;j<cur;j++)
                if (G[u][Alt[tot][j]]) Alt[tot+1][nxt++]=Alt[tot][j];
                if (DFS(nxt,tot+1)) return true;
            }
            return false;
        }
    
        int maxclique()
        {
            ans=0;
            memset(Max,0,sizeof(Max));
            for (int i=n-1;i>=0;i--)//编号从0开始
            {
                int cur=0;
                for (int j=i+1;j<n;j++) if (G[i][j]) Alt[1][cur++]=j;
                DFS(cur,1);
                Max[i]=ans;
            }
            return ans;
        }
    };
    
    max_clique T;
    
    void read(int n)
    {
         getchar();
         char str[1000],str1[10];
         for (int i=0;i<n;i++)
         {
              gets(str);
              int k=0,j=0;
              int L=strlen(str);
              while(k<L)
              {
                   if (str[k]==' ')
                   {
                        str1[j]=NULL;
                        task[i].push_back(atoi(str1));
                        j=0;
                   }
                   else
                   {
                        str1[j]=str[k];
                        j++;
                   }
                   k++;
              }
              str1[j]=NULL;
              task[i].push_back(atoi(str1));
         }
    }
    void sovle(int n)
    {
         bool vis[51];
         T.n=n;
         for (int i=0;i<n;i++)
         for (int j=0;j<n;j++)
         T.G[i][j]=1;
         for (int i=0;i<n;i++)
         {
              for (int j=0;j<n;j++)
              {
                   memset(vis,0,sizeof(vis));
                   for (int k=0;k<task[i].size();k++)
                   {
                        int v=task[i][k];
                        vis[v]=true;
                   }
                   bool cut=true;
                   for (int k=0;k<task[j].size();k++)
                   {
                        int v=task[j][k];
                        if (vis[v])
                        {
                             cut=false;
                             break;
                        }
                   }
                   if (!cut) T.G[i][j]=0;
              }
         }
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
             init(n);
             read(n);
             sovle(n);
             printf("%d
    ",T.maxclique());
        }
        //fclose(stdin);
        return 0;
    }

    作者 chensunrise

  • 相关阅读:
    数组的复制
    二维数组的切片和索引
    一维数组的切片和索引
    is判断函数
    其他方式创建数组
    ndarray对象属性
    创建随机数组
    arange创建数组
    SmartBinding与kbmMW#3
    SmartBinding与kbmMW#2
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3821064.html
Copyright © 2011-2022 走看看