HDU5098 Smart software Installer (top sort)

Source: Internet
Author: User
Tags cas

Title: There is a batch of software to install, ":" Before the software needs to be installed after the ":" Software Installation (":" There may be more than one software), "*" indicates that the software requires a reboot to complete the installation.

The minimum number of reboots required for all software installations to complete.

Problem Solving Ideas:

Double queue top sort, with two queue q1,q2 respectively to save the software that does not need to reboot and the software that needs to restart. First of all, 0 of the points into the queue, of course, do not need to restart the Q1, the need to restart the Q2 into the. Then delete all the nodes in the Q1, so that the node connected to them minus 1, if found to reduce the degree of 0, and then determine whether it needs to restart, and add Q1 or Q2. Once the Q1 is found to be empty, then add 1 to the answer (that is, restart once), and add all the elements in the Q2 to q1,q2 empty. This loops until the q1,q2 are empty.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < string> #include <vector> #include <queue> #include <set> #include <map> #include <cmath > #define INF 0xfffffff#define MAX 1000000#define CLR (b) memset ((a), (b), sizeof (a)) using namespace Std;typedef long L Ong ll;int edge[2005][2005];int vis[2005];int degree[2005];int tot,ans;map<string,int>mp;queue<int>q1,        Q2;void Top_sort () {for (int i=1;i<=tot;i++) if (!degree[i]) {if (!vis[i]) Q1.push (i);    else Q2.push (i); } while (!q1.empty () | |!            Q2.empty ()) {if (Q1.empty () &&!q2.empty ()) {ans++;                while (!q2.empty ()) {int X=q2.front ();                Q2.pop ();            Q1.push (x);            }} while (!q1.empty ()) {int X=q1.front ();            Q1.pop ();                for (int i=1;i<=tot;i++) if (Edge[i][x]) {degree[i]--; IF (!degree[i]) {if (!vis[i]) Q1.push (i);                else Q2.push (i);    }}}}}void init () {CLR (degree,0);    CLR (edge,0);    CLR (vis,0);    tot=0;    ans=0;    Mp.clear ();    while (!q1.empty ()) Q1.pop (); while (!q2.empty ()) Q2.pop ();}    String P,last[1005];int Main () {int A, B;    int t;    string S;    scanf ("%d", &t);    GetChar ();    GetChar ();        for (int cas=1;cas<=t;cas++) {init ();            while (Getline (cin,s)!=null) {if (s[0]== ' n ') break;            int len=s.size ();            int flag=0;            int havelast=0;            P= "";            int lastnum=-1;                    for (int i=0;i<len;i++) {if (s[i]== ') {last[++lastnum]= "";                Continue                    } if (s[i]== ' * ') {flag=1;                Continue                    } if (s[i]== ': ') {havelast=1; ContiNue                } if (havelast==0) p+=s[i];            else Last[lastnum]+=s[i];            } if (Mp.find (p) ==mp.end ()) Mp[p]=++tot;            Vis[mp[p]]=flag;            int m=0;                while (Last[m].size () > 0 && m <= lastnum) {if (Mp.find (Last[m]) ==mp.end ()) Mp[last[m]]=++tot;                Edge[mp[p]][mp[last[m]]]=1;            m++;        }} for (int i=1;i<=tot;i++) for (int j=1;j<=tot;j++) if (edge[i][j]) degree[i]++;        Top_sort ();    cout<< "Case" <<cas<< ":" <<ans<<endl; } return 0;}


HDU5098 Smart software Installer (top sort)

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.