HDOJ 5098 Smart Software Installer 拓扑排序
拓扑排序:
两个队列,一个放不须要重新启动入度为0的,一个放须要重新启动入度为0的....从不须要重新启动的队列開始,每弹出一个数就更新下入度,遇到入读为0的就增加到对应队列里,当队列空时,记录重新启动次数+1,交换队列..一直到两个队列都为空
Smart Software Installer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 78 Accepted Submission(s): 32
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?
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.
2 glibc:
gcc*: glibc uefi*:
gcc*:
raid_util*: uefi
gpu_driver*: uefi
opencl_sdk: gpu_drivergcc
Case 1: 1
Case 2: 2
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <sstream>
#include <map>
#include <queue> using namespace std; const int maxn=2000; map<string,int> mSI;
int nm;
bool restart[maxn];
int degree[maxn]; int hash(string name)
{
int ret=mSI[name];
if(ret==0)
{
mSI[name]=nm++;
ret=nm-1;
}
return ret;
} struct Edge
{
int to,next;
}edge[maxn*maxn]; int Adj[maxn],Size; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void add_edge(int u,int v)
{
edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;
} int TUOPU()
{
queue<int> q[2];
int a=0,b=1;
int n=nm-1;
for(int i=1;i<=n;i++)
{
if(degree[i]==0)
{
if(restart[i]==true) q[b].push(i);
else q[a].push(i);
}
}
int time=0;
while(!q[a].empty()||!q[b].empty())
{
while(!q[a].empty())
{
int u=q[a].front(); q[a].pop();
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
degree[v]--;
if(degree[v]==0)
{
if(restart[v]==true) q[b].push(v);
else q[a].push(v);
}
}
}
if(q[b].empty()==true) continue;
time++;swap(a,b);
}
return time;
} int main()
{
int T_T,cas=1;
scanf("%d",&T_T);
getchar(); getchar();
while(T_T--)
{
init();
mSI.clear(); nm=1;
memset(restart,false,sizeof(restart));
memset(degree,0,sizeof(degree)); string line,name;
while(getline(cin,line))
{
if(line[0]==0) break;
istringstream sin(line);
sin>>name;
bool flag=false;
int sz=name.size();
if(name[sz-2]=='*')
{
flag=true;
name[sz-2]=0;
name.resize(sz-2);
}
else
{
name[sz-1]=0;
name.resize(sz-1);
} int to=hash(name);
restart[to]=flag;
while(sin>>name)
{
int from=hash(name);
add_edge(from,to);
degree[to]++;
}
}
printf("Case %d: %d\n",cas++,TUOPU());
}
return 0;
}
HDOJ 5098 Smart Software Installer 拓扑排序的更多相关文章
- 双拓扑排序 HDOJ 5098 Smart Software Installer
题目传送门 /* 双拓扑排序:抄的,以后来补 详细解释:http://blog.csdn.net/u012774187/article/details/40736995 */ #include < ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDOJ 1285 确定比赛名次(拓扑排序)
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
- HDOJ 2647 Reward 【逆拓扑排序+分层】
题意:每一个人的基础工资是888. 因为一部分人要显示自己水平比較高,要求发的工资要比其它人中的一个人多.问你能不能满足他们的要求,假设能的话终于一共要发多少钱,假设不能就输出-1. 策略:拓扑排序. ...
- hdoj 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 5098 双队列拓扑排序
http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...
- hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06
一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...
- 拓扑排序/DFS HDOJ 4324 Triangle LOVE
题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记 ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
随机推荐
- vue项目,封装api并使用
封装api index.js let uploadBase = '' if(process.env.NODE_ENV === 'production'){ uploadBase = 'https:// ...
- P1017 进制转换 (负进制转换)
和平常的转化差不多 加多一步 如果余数 < 0, 那么余数减去除数(此时除数是负),商数加1 #include<cstdio> #define _for(i, a, b) for(i ...
- 《Spring技术内幕》笔记-Spring的设计理念和总体架构
1.Spring的主要子项目: -1.Spring Framework(Core):Spring项目的核心.提供IoC,AOP,MVC等核心功能. -2.Spring Web Flow ...
- cpc,a wonderful concert
做完这道题突然就感觉自己脑子是不是已经秀逗了,tle到死后才想起来找规律, 就是求排列数的题目,按插入点对状态进行分类,可以暴力tle... #include<iostream> #inc ...
- bzoj1070: [SCOI2007]修车(费用流)
1070: [SCOI2007]修车 题目:传送门 题解: 一道挺简单的费用流吧...胡乱建模走起 贴个代码... #include<cstdio> #include<cstring ...
- 如何将网站升级为HTTPS协议(整理)
如何将网站升级为HTTPS协议(整理) 一.总结 一句话总结: 获取证书(有免费有付费):证书是一个二进制文件,里面包含经过认证的网站公钥和一些元数据,要从经销商购买. 安装证书:证书可以放在/etc ...
- laravel中的数据迁移和数据填充
laravel中的数据迁移和数据填充 标签(空格分隔): php 生成迁移文件两种方式: 1 新建一个表的迁移文件 php artisan make:migration create_students ...
- SVN在vs2013中使用
http://download.csdn.net/download/show_594/9112963 内包含VisualSVN 5.0.1的官方原版安装包及破解文件VisualSVN.Core.L.d ...
- childNodes.length和form.length的不同
我们知道,DOM里面提供了element.childNodes.length属性,childNodes 属性返回节点的子节点集合,以 NodeList 对象. 那么childNodes包含哪些节点呢? ...
- PostgreSQL Replication之第四章 设置异步复制(5)
4.5 使流复制更健壮 当连接到master时,slave要做的第一件事情是赶上master.但是,这会一直工作吗?我们已经看到,我们可以使用由基于流和基于文件组成的混合设置.这给了我们一些额外的安全 ...