Polish version    English version  
  History of OI -> IV OI 1996/1997 -> Problems


 News
 About Olympic
 History of OI
XVII OI 2009/2010
XVI OI 2008/2009
XV OI 2007/2008
XIV OI 2006/2007
XIII OI 2005/2006
XII OI 2004/2005
XI OI 2003/2004
X OI 2002/2003
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
VI OI 1998/1999
V OI 1997/1998
IV OI 1996/1997
Stage III - results
Stage II - results
Stage I - results
Problems
Regulations
Organization
Hints
Schedule
III OI 1995/1996
II OI 1994/1995
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
IV Olimpiada Informatyczna 1996/1997

Task: ALI
Author: Piotr Chrz±stowski-Wachtel
ALI BABA

III stage contest  
Source fileALI.??? (e.g. PAS,C, CPP)
Executable fileALI.EXE
Input fileALI.IN
Output fileALI.OUT

To open sesame one needs a set of tokens containing at least z gold tokens, s silver tokens and m copper tokens.

Initially, Ali Baba has at his disposal a certain number of tokens of each kind. He can exchange them with the Guardian of sesame, according to given rules.

Every rule is of the form:

z1, s1, m1 -> z2, s2, m2 (zi, si, mi are from the set {0,1,2,3,4}).

Such a rule denotes that Ali Baba can exchange z1 gold tokens, s1 silver tokens and m1 copper tokens for z2 gold tokens, s2 silver tokens and m2 copper tokens.

Tokens received in a transaction can be exchanged in the next transactions.

Task

Write a program that:
  • reads sets of data from the text file ALI.IN; each set of data contains:
    • the number of gold, silver and copper tokens owned by Ali Baba initially;
    • the description of the set of tokens needed to open sesame;
    • the rules of transactions;
  • for each set of data, checks if there exists a finite sequence of transactions, after which Ali Baba can receive the set of tokens opening sesame and if so writes to the text file ALI.OUT the minimal length of such a sequence; if such a sequence does not exist writes to the same file the answer NIE (which means NO in Polish).

Input

In the first line of the text file ALI.IN there is one positive integer d<=10, which is the number of sets of data.

Then the sets of data follow. Each set of data consists of many lines.

In the first line there are three non-negative integers zp, sp, mp from the set {0,1,2,3,4}. They are the numbers of gold, silver and copper tokens respectively, owned by Ali Baba initially.

In the second line there are three integers z, s, m from the set {0,1,2,3,4}. They are the numbers of gold, silver and copper tokens needed to open sesame.

In the third line there is written the number of rules r, 1<=r<=10.

In each of the following r lines there is a sequence of six numbers z1, s1, m1, z2, s2, m2 from the set {0,1,2,3,4}. They describe a single rule: z1, s1, m1 -> z2, s2, m2.

The numbers in each line are separated by single spaces.

Output

In the i-th line of the text file ALI.OUT there should be written the result for the i-th set of data:
  • one non-negative integer, i.e. the minimal number of transactions that Ali Baba has to carry out in order to receive the set of tokens opening sesame,
  • or the word NIE (which means NO in Polish).

Example
For the text file ALI.IN:

2
2 2 2
3 3 3
3
0 1 1 2 0 0
1 0 1 0 2 0
1 1 0 0 0 2
1 1 1
2 2 2
4
1 0 0 0 1 0
0 1 0 0 0 1
0 0 1 1 0 0
2 0 0 0 2 2
the correct solution is the text file ALI.OUT:
NIE
9





Print friendly version