HDU 5098 Smart software Installer topology sort or memory search

Source: Internet
Author: User
Tags acos in degrees

Smart Software InstallerTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 416 Accepted Submission (s): 124


Problem DescriptionThe Software Installation is becoming + more complex. An automatic tool was often useful to manage this process. An IT company was developing a System Management utility to install a set of software packages automatically with the Depen Dencies. They found that reboot was often required to take effect after installing some software. A software package cannot is installed until all software packages it depends on is installed and take effect.

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

Now, could. Implement this algorithm for them to minimize the number of restart during the entire INS Tallation process?
Inputthe first line is a integer n (1 <= n <=), which is the number of the test cases. The second is blank. The input of both test cases is separated by a blank line.

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

Assume all packages this need to being installed and all referenced packages would be listed in a individual line to define T He reboot property. It should is noted that cyclic dependencies is not allowed in this problem.
Outputfor 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
2glibc:gcc*: glibcuefi*:gcc*:raid_util*: uefigpu_driver*: UEFIOPENCL_SDK:GPU_DRIVERGCC

Sample Output


Links: http://acm.hdu.edu.cn/showproblem.php?pid=5098

Reference Blog: http://www.cnblogs.com/naturepengchen/articles/4069796.html


Test instructions

Number of cases in the first row. Then each case is separated by a blank line.

Each case has several lines, the first word represents a software, if the name is followed by an * number, the installation of this software requires a reboot. Multiple software can restart the installation at the same time. Then the colon is followed by the software that installs the software that needs to be installed first.


Practice:

There are two ways to do this, but you need to build the diagram first.  Use the Get function to turn the string into a number. The ID represents the software before the colon, and Fu represents the software behind the colon. To save the ID in Vector son[fu], just like a tree. Then put the ID into the degree + +;


The first approach, the topological sort. The Q1 queue with an entry level of 0 and does not need to be restarted, and then the Q2 queue that needs to be restarted is placed into the 0. Then each time the Q1 queue of sons in the degree of--if a son into the degree of 0, then on behalf of the son's father has been installed, it can be installed, if he needs to restart, into the Q2, do not need to enter the Q1. Then after the Q1 is empty, it indicates that it can be installed, and that no reboot is required. Then install the Q2 queue, put the Q2 queue all the Q1 on the line, and then restart the number of + +, indicating a reboot, and then put them all installed.

This kind of practice feels like a simulation, which is to keep the degree of 0 in the process.


The second approach, memory search. Dp[i] Indicates how many steps are required to install the I software. Then DFS all into the 0, search all sons, search maximum path weights and. A weight value of 1 is required to restart, and a weight value of 0 is not required.



Code:

The first approach:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> #include <sstream> #define INF 999999999#define EPS 0.00001#define LL __int64d#define Pi ACOs ( -1.0) map<string,int> my;int reb[2010];int du[2010];//in degrees int dp[2010];// The minimum number of steps per point vector<int> son[2010];int cnt;int get (String na) {if (My.count (NA) ==0) {My[na]=cnt;return cnt++;} Elsereturn My[na];} int deal () {queue <int > q1,q2;//does not need to restart for (int i=0;i<cnt;i++) {if (du[i]==0) {if (reb[i]==0) Q1.push (i); Elseq2.push (i);}} int ans=0; while (!q1.empty () | |! Q2.empty ()) {while (!q1.empty ()) {int Num=q1.front (), Q1.pop (); for (int i=0;i<son[num].size (); i++) {Du[son[num][i]] --;if (du[son[num][i]]==0) {if (reb[son[num][i]]==0) Q1.push (Son[num][i]),//can be installed without restarting Elseq2.push (Son[num][i]);}} if (!q2.empty ()) Ans++;while (!q2.empty ()) {int Num=q2.front (); Q2.pop (); Q1.push (num);}} return ans; }int Main () {int t;int cas=1;string str;scanf ("%d%*c", &t), Getline (CIN,STR), while (t--) {my.clear (); Cnt=0;memset ( Reb,0,sizeof Reb); memset (du,0,sizeof du); for (int i=0;i<2000;i++) son[i].clear (), while (Getline (CIN,STR)) {if (Str.size () ==0) Break;stringstream sss;sss< <str;sss>>str;int mao=str.find (': '); int id;if (str[mao-1]== ' * ') id=get (Str.substr (0,mao-1)); Elseid=get ( Str.substr (0,mao)); if (str[mao-1]== ' * ') reb[id]=1;  while (SSS&GT;&GT;STR) {du[id]++;son[get (str)].push_back (ID);} }cout<< "Case" <<cas++<< ":" <<deal () <<endl;}  return 0;}


The second approach:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> #include <sstream> #define INF 999999999#define EPS 0.00001#define LL __int64d#define Pi ACOs ( -1.0) map<string,int> my;int reb[2010];int du[2010];//in degrees int dp[2010];// The minimum number of steps per point vector<int> son[2010];int cnt;int get (String na) {if (My.count (NA) ==0) {My[na]=cnt;return cnt++;} Elsereturn My[na];} int DFS (int id) {if (dp[id]!=-1) return dp[id];int ans=reb[id];for (int i=0;i<son[id].size (); i++) {DFS (son[id][i]); Ans=max (Dp[son[id][i]]+reb[id],ans); }return Dp[id]=ans;} int main () {int t;int cas=1;string str;scanf ("%d%*c", &t), Getline (CIN,STR), while (t--) {my.clear (); Cnt=0;memset ( Reb,0,sizeof Reb), memset (du,0,sizeof du), memset (dp,-1,sizeof dp), for (int i=0;i<2000;i++) son[i].clear (), while (Getline (CIN,STR)) {if (str.size () ==0) Break;stringstream sss;sss<<str;sss>>str;int mao=str.find (': '); int id;if (str[mao-1] = = ' * ') id=get (Str.substr (0,mao-1)); Elseid=get (Str.substr (0,mao)); if (str[mao-1]== ' * ') reb[id]=1; while (SSS&GT;&GT;STR) {du[id]++;//father First son[get (str)].push_back (ID);//cout<<str<<endl;}//cout<< str[mao]<<endl; cout<< "id" &LT;&LT;ID&LT;&LT;ENDL;} int ans=0;for (int i=0;i<cnt;i++) {if (du[i]==0) Ans=max (Ans,dfs (i));} cout<< "Case" <<cas++<< ":" <<ans<<endl;}  return 0;}












HDU 5098 Smart software Installer topology sort or memory search

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.