Hdu 5098 Smart Software Installer(记忆化搜索)

题目链接

Smart Software Installer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 25    Accepted Submission(s): 13


Problem Description
The software installation is becoming more and more complex. An automatic tool is often useful to manage this process. An IT company is developing a system management utility to install a set of software packages automatically with the dependencies. They found that reboot is often required to take effect after installing some software. A software package cannot be installed until all software packages it depends on are installed and take effect.

In the beginning, they implemented a simple installation algorithm, but the system would reboot many times during the installation process. This will have a great impact on the user experience. After some study, they think that this process can be further optimized by means of installing as much packages as possible before each reboot.

Now, could you please design and implement this algorithm for them to minimize the number of restart during the entire installation process?
 

Input
The first line is an integer n (1 <= n <= 100), which is the number of test cases. The second line is blank. The input of two test cases is separated by a blank line.

Each test case contains m (1 <= n <= 1000) continuous lines and each line is no longer than 1024 characters. Each line starts with a package name and a comma (:). If an asterisk (*) exists between the package name and the comma, the reboot operation is required for this package. The remaining line is the other package names it depends on, separated by whitespace. Empty means that there is no dependency for this software. For example, “a: b” means package b is required to be installed before package a. Package names consist of letters, digits and underscores, excluding other special symbols.

Assume all packages here need to be installed and all referenced packages will be listed in an individual line to define the reboot property. It should be noted that cyclic dependencies are not allowed in this problem.
 

Output
For each test case, you should output a line starting with “Case #: " (# is the No. of the test case, starting from 1) and containing the reboot count for this test case. (Refer to the sample format)
 

Sample Input
  
  
2 glibc: gcc*: glibc uefi*: gcc*: raid_util*: uefi gpu_driver*: uefi opencl_sdk: gpu_drivergcc
 

Sample Output
  
  
Case 1: 1 Case 2: 2
 

Source

题解:如果一个软件a需要先安装软件b,则从a到b连一条有向边。图一定是个DAG。对于在同一条链上需要重启的软件来说,一定只有一个一个的重启。对于不在同一条链上的要重启的软件是没有相互影响的。所以重启的次数最少就是重启软件最多的一条链。由于图是DAG,用dp[i]表示在以i个点为起点的链,最多要重启多少次,直接记忆化搜索即可。

代码如下:

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<bitset>
#define nn 1100
using namespace std;
char s[11000];
struct node
{
    int st,en,next;
}E[nn*nn];
int p[nn],num;
void init()
{
    memset(p,-1,sizeof(p));
    num=0;
}
void add(int st,int en)
{
    E[num].en=en;
    E[num].next=p[st];
    p[st]=num++;
}
map<string,int>ma;
bool vis[nn];
bool use[nn];
int dp[nn];
int dfs(int id)
{
    if(vis[id])
        return dp[id];
    vis[id]=true;
    int i,w;
    dp[id]=0;
    if(use[id])
        dp[id]=1;
    int re=0;
    for(i=p[id];i+1;i=E[i].next)
    {
        w=E[i].en;
        re=max(re,dfs(w));
    }
    dp[id]+=re;
    return dp[id];
}
int main()
{
    int i,j,t,cas=1;
    scanf("%d",&t);
    getchar();
    gets(s);
    string ix;
    int lx;
    while(t--)
    {
        memset(use,false,sizeof(use));
        init();
        ma.clear();
        int cnt=0;
        string my;
        while(gets(s))
        {
            if(strlen(s)==0)
                break;
            ix="";
            int id;
            for(i=0;i<(int)strlen(s);i++)
            {
                if(s[i]==':')
                {
                    lx=ix.size();
                    my="";
                    for(j=0;j<lx-1;j++)
                    {
                        my+=ix[j];
                    }
                    if(ix[lx-1]!='*')
                        my+=ix[lx-1];
                    if(ma.count(my)==0)
                        ma[my]=++cnt;
                    id=ma[my];
                    if(ix[lx-1]=='*')
                        use[id]=true;
                    ix="";
                }
                else if(s[i]==' ')
                {
                    if(ix.size())
                    {
                        lx=ix.size();
                        my="";
                        for(j=0;j<lx-1;j++)
                        {
                            my+=ix[j];
                        }
                        if(ix[lx-1]!='*')
                            my+=ix[lx-1];
                        if(ma.count(my)==0)
                            ma[my]=++cnt;
                        add(id,ma[my]);
                        if(ix[lx-1]=='*')
                            use[ma[my]]=true;
                    }
                    ix="";
                }
                else
                    ix+=s[i];
            }
            if(ix.size())
            {
                lx=ix.size();
                my="";
                for(j=0;j<lx-1;j++)
                {
                    my+=ix[j];
                }
                if(ix[lx-1]!='*')
                    my+=ix[lx-1];
                if(ma.count(my)==0)
                    ma[my]=++cnt;
                add(id,ma[my]);
                if(ix[lx-1]=='*')
                    use[ma[my]]=true;
            }
        }
        int ans=0;
        memset(vis,false,sizeof(vis));
        for(i=1;i<=cnt;i++)
        {
            if(!vis[i])
                dfs(i);
            ans=max(ans,dp[i]);
        }
        printf("Case %d: ",cas++);
        printf("%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值