POJ 1364 King

Source: Internet
Author: User
Tags integer numbers time limit

Original question:
King
Time limit:1000ms Memory limit:10000k
Total submissions:11680 accepted:4263
Description

Once, in one kingdom, there is a queen and that Queen was expecting a baby. The Queen prayed: "If my child is a son and If only he is a sound king." After nine months she is born, and indeed, she gave birth to a nice son.
Unfortunately, as it used to happen in royal families, the son is a little retarded. After many years of study he were able just to add integer numbers and to compare whether the result are greater or less tha n a given integer number. In addition, the numbers had to being written in a sequence and he is able to sum just continuous subsequences of the Sequen Ce.

The old King is very unhappy of his son. But he is ready-to-make everything-to-enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had to decide about had to being presented in a form of a finite sequence of integer numbers and the decision about it would is done by stating an integer constraint (i.e. an Upper or lower limit) for the sum of that sequence. In the This is there is at least some hope, his son would is able to make some decisions.

After the old king died, the young king began to reign. But very soon, a lot of the people became very unsatisfied with he decisions and decided to dethrone him. They tried to does it by proving, decisions were wrong.

Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems is in the form of subsequences Si = {aSi, asi+1, ..., asi+ni} of a sequence S = {A1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + asi+1 + ... + asi+ni of each subsequence Si an int Eger constraint Ki (i.e. asi + asi+1 + ... + asi+ni < Ki or asi + asi+1 + ... + asi+ni > Ki resp.) and declared these Co Nstraints as his decisions.

After a while he realized this some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to him advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the King and write a program this decides whether such a sequence exists or not.
Input

The input consists of blocks of lines. Each block except the last corresponds to one set of problems and King's decisions about them. The first line of the block there be integers n, and m where 0 < n <= is length of the sequence S and 0 < M <= is the number of subsequences Si. Next m lines contain particular decisions coded in the form of Quadruples si, ni, oi, ki, where Oi represents operator ; (coded as GT) or operator < (coded as LT) respectively. The symbols si, ni and ki have the meaning described above. The last block consists of just one line containing 0.
Output

The output contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy if such a sequence does not exist. Otherwise It contains text lamentable kingdom. There is no line in the output corresponding to the last "null" block of the input.
Sample Input

4 2
1 2 GT 0
2 2 lt 2
1 2
1 0 GT 0
1 0 LT 0
0
Sample Output

Lamentable Kingdom
Successful conspiracy
Source

Central Europe 1997
Effect:
Give you a sequence s={a1,a2,a3...an} and now tell you a bunch of ai+...+aj< K or ai+....+aj>k. Now ask you if there is such a sequence s.

#include <bits/stdc++.h> #include <iostream> #include <cstring> using namespace std;
FStream in,out;
const int max_e=2005;
const int max_v=1005;
const int inf=999999;
struct Edge {int from,to,cost;};
Edge Es[max_e];
int D[max_v];
int v,e;
    BOOL Find_negative_loop () {for (int i=0;i<v;i++) D[i]=inf;
            for (int i=0;i<v;i++) {for (int j=0;j<e;j++) {edge e=es[j];
                if (d[e.to]>d[e.from]+e.cost) {d[e.to]=d[e.from]+e.cost;
            if (I==v-1) return true;
}}} return false;
    } int main () {Ios::sync_with_stdio (false);
    int f,t,c;
string S;
 In.open ("Data.txt");
    Out.open ("Answer.txt");
        while (cin>>v,v) {cin>>e;
        v++;
            for (int i=0;i<e;i++) {cin>>f>>t>>s>>c;
            f++;
if (s== "LT") {                Es[i].from=f+t;
                Es[i].to=f-1;
            Es[i].cost=c;
                } else {es[i].from=f-1;
                Es[i].to=f+t;
            Es[i].cost=-c-2;
        }} if (Find_negative_loop ()) cout<< "Successful Conspiracy" <<endl;
    else cout<< "Lamentable Kingdom" <<endl;
}//In.close ();
    Out.close ();
return 0; }

Answer:
The problem and UVA 515 is a topic, but UVA data is too strong, I ran 500 of the data are over, but the UVA is but.

The problem with the obvious difference constraint is that the data given in the form is given to you four values s,t,o,k. Represents as+...+at>k or as+...+at< K.
This string is deformed, set ai+....+aj< K for si-sj-1< K. If t==0, then it becomes si-si-1< K. Cases greater than k also become less than the case of negative K. After the building, according to the question to determine whether there are negative ring and 0 ring. Normal differential constraints should be ai+...+aj<=k in order to connect all the formulas, so you can let the ai+....+aj>k situation into a left-open right-closed interval, into sj-1-si<-k-1. Because the AI is required to be integers here, so similar to 1< AI < 2 is not possible, so can only become sj-1-si<-k-2, in addition, the figure has a hidden source point S0, you need to move all the points back one. Then determine if there is a negative ring, you can use Bellman-ford or SPFA.

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.