CTSC1999 Patch vs Error

Source: Internet
Author: User
Tags first string

  • Title Description Description
    Error is what people call a bug. Users always want to use the software as little as possible, preferably without errors. But it is almost impossible to launch a software without errors, so many software companies are frantically distributing patches (sometimes even for a fee). T company is one of them.
    Last month, T company launched a new word processing software, and then released a batch of patches. Recently, T company found that its patch has a fatal problem, that is, a patch in the exclusion of some errors, often add some other errors.
    Only n specific errors can occur in this word processing software, which is determined by the software itself. T Company currently issued a total of M patches, for each patch, there is a specific applicable environment, a patch can only be used when the current software contains some errors but also does not contain other errors, if it is used, it will fix some errors and add some errors at the same time. In addition, it takes a certain amount of time to use each patch (that is, when the patch runs).
    More accurate Description:
    To set the possible n errors in this word processing software as the elements in the collection B={b1,b2,..., Bn}, T company currently issued a total of M patches: p1,p2,..., pm. For each patch pi, there is a specific applicable environment, a patch can only be used if the software contains some errors but also does not contain other errors, in order to clarify, set the error set: bi+, bi-, when the software contains all the errors in bi+, and does not contain any errors in bi-, Patch pi can be used, otherwise it cannot be used, obviously bi+, bi-intersection is empty. Patch PI will fix some errors and add some errors at the same time, set the error set fi-, fi+, after using the patch pi, any errors in fi-will not appear in the software, and the software would contain all errors in fi+, the same fi-, fi+ intersection is empty. In addition, it takes a certain amount of time to use each patch (that is, when the patch runs).
    T company's problem is very simple, its initial version of the word processing software unfortunately contains all the n errors in set B, it is possible to use these patches (in any order, a patch can be used multiple times), so that the word processing software is an error-free software. If possible, you want to find the solution with the least amount of time.

  • Enter a description input Description
    The first line of the input file has two positive integers n and m, n indicates the total number of errors, and m represents the total number of patches. The next M-line gives the information about the M patch. Each line consists of a positive integer (which indicates that the patch pi runs time-consuming) and two strings of length n, separated by a space character.
    The first string, if the K-character is ' + ', indicates that BK belongs to bi+, if '-', it means BK belongs to bi-, if it is ' 0 ', then BK is neither bi+ nor bi-, that is, whether the software contains BK does not affect the availability of patch pi.
    The second string, if the K-character is ' + ', indicates that BK belongs to fi+, if '-', it means BK belongs to fi-, if it is ' 0 ', then BK is neither fi+ nor fi-, that is, whether the software contains BK will not be changed by using Patch pi.

  • outputs description output Description
    Output an integer, if the problem has a solution, the output is always time-consuming, otherwise output 0.

  • sample input to sample
    3 3
    1 000 00-
    1 00-0-+
    2 0–-++

  • sample output Sample outputs
    8

  • Data Size & Hint
    1≤N≤ -,1≤m≤ -
    But it is said that N is only 15 max. You can do it by 15.

  • Solving
    All error states with binary memory errors, a total of < Span class= "Mrow" id= "mathjax-span-5859" > 2 n Span style= "Display:inline-block; width:0px; Height:2.279em; " > Two In the following program, use Reqt[i] (require true) to indicate what errors (binary representations, same), Reqf[i] (require false) are required for the I patch to indicate that no errors are required for the I patch, with rest[i] (result in True) indicates which of the new errors occurred after the patch I, using resf[i] (result in false) to indicate which errors were eliminated after the first patch was installed, and Timi indicates when the patch I was running. Space complexity is about o ( 2 n ) , can bear it completely.
    The initial state is 2 n ? 1 (binary), the last state is 0.
    Enumerates all States and patches, and if a state can hold a patch, from that state, add a pointing edge to the state after the patch (do not add the inverse), and the Edge is the time the patch runs. This then starts from the initial state to run the heap optimization Dijkstra the shortest possible. Complexity of Time O((m+N) 2 n ) , n=20 may not be able to live. However, since the search can be too, then the algorithm has no reason.

  • Code

#include <cstdio>#include <algorithm>#include <cstring>#include <queue>using namespace STD;Const intMAXN = (1<< -), oo =1000000000, nil =0;intN, M, tim[ the], reqt[ the], reqf[ the], rest[ the], resf[ the];intE, PNT[MAXN], NXT[MAXN <<2], U[MAXN <<2], V[MAXN <<2], W[MAXN <<2];intD[MAXN];BOOLVIS[MAXN];structnode{intn, Dis; Nodeintn =0,intdis =0): N (n), dis (dis) {}BOOL operator< (Constnode& b)Const{returnDis > B.dis; }};voidAddedge (intAintBintc) {U[++e] = A; V[e] = b; w[e] = c; Nxt[e] = Pnt[a]; Pnt[a] = e;}voidInit () {Charch[ -];scanf("%d%d", &n, &m); for(inti =1; I <= M; ++i) {scanf("%d", &tim[i]);scanf('%s ', &ch); for(intj =0; J < N; ++J) {if(Ch[j] = =' + ') {Reqt[i] |= (1<< j); }if(Ch[j] = ='-') {Reqf[i] |= (1<< j); }        }scanf('%s ', &ch); for(intj =0; J < N; ++J) {if(Ch[j] = =' + ') {Rest[i] |= (1<< j); }if(Ch[j] = ='-') {Resf[i] |= (1<< j); }        }    } for(inti = (1<< N)-1; I >=0; -I.) { for(intj =1; J <= M; ++J) {if(((((I & reqt[j]) = = Reqt[j]) && ((I & reqf[j]) = =0) {Addedge (I, (I & (~resf[j]) | rest[j]), tim[j]); }        }    }}voidWork () {intS = (1<< N)-1, T =0;memset(d,0x3f,sizeof(d));memset(Vis,0,sizeof(VIS));    Priority_queue <node> Q; D[s] =0; Q.push (Node (S),0)); while(!        Q.empty ()) {node TMP = Q.top ();        Q.pop (); VIS[TMP.N] =true; for(intj = PNT[TMP.N]; J! = Nil; j = Nxt[j]) {if((!vis[v[j]) && D[v[j]] > Tmp.dis + w[j])                {D[v[j]] = Tmp.dis + w[j];            Q.push (Node (v[j], d[v[j])); }        }    }if(D[t] > oo) {puts("0"); }Else{printf("%d\n", D[t]); }}intMain () {init (); Work ();return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

CTSC1999 Patch vs Error

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.