Divisibility Gradient

Given a simple undirected graph containing N vertices with numbers from 1 to N, each vertex contains a digit from {1,2, .. 7}. Starting from vertex 1 with an empty string S, we go through some vertices (without restrictions) to vertex N. For each vertex in the path, add the corresponding digit to the right of row S. Finally, we get S as a decimal integer. You are asked to find a method that satisfies S, divided by all its digits, and the sum of the digits S should be as small as possible.

Enter

There are several test cases (maximum fifteen), each of which is formed as follows:

The first line contains a positive integer N (N ≀ 100). The second line contains N digits (separated by spaces), the i-th digit is the value of the i-th vertex. N last lines, each contains N values of {0, 1} (separated by spaces), the j-th value of the i-th line is equal to 1 if there is an edge connecting two vertices (i, j), otherwise 0. 

The entry ends at N = 0.

Exit

For each test case, the minimum sum of the digits found is displayed on the line, or -1 if there is no solution.

Example

Input: 4

1 2 1 4

0 1 1 1

1 0 0 1

1 0 0 1

1 1 1 0

Conclusion: 7

please guide me

there can be native loops and loops such that node 1 and node N can be displayed any number of times

+4
source share
3 answers

If this graph is converted to some other graph where cycles are not allowed, this problem can be solved using the Dijkstra algorithm .

To do this, start by dividing the lines by 7. Look at this sequence: 1, 10, 100, ... (mod 7). Since 7 is a prime number, 10 7-1 = 1 (mod 7) due to the small Fermat's theorem . This means that the sequence 1, 10, 100, ... (mod 7) is periodic, and the period is 6. This will be used to transform the graph, and it also allows you to recursively calculate S n (mod 7) using S n- 1 (mod 7): S n = S n-1 + 10 n% 6 * n_th_digit (mod 7).

You need to start searching for the shortest path from node N, because this path can be completed on one of several nodes of the transformed graph. It also allows you to quickly determine (using the first 2 nodes of the path) if you are allowed to visit node "5", node "4" and other "even" nodes.

The open dialing algorithm (priority queue) must contain the priority itself (sum of digits) if 3 additional bits and 3 remainders are allowed: β€œ4”, β€œvisited 3”, β€œ7” visited, S % 3 , S % 7 and S.length % 6 .

The schedule should be converted as follows. Each vertex is expanded to 3 vertices, one is allowed only for S%3==0 , the other for S%3==1 and S%3==2 . Then each vertex decomposes to 7 (for S%7 ), and then each vertex decomposes to 6 (for S.length % 6 ). You can put all of these extensions into the source graph: just add a 3D array (size 3 * 7 * 6) of back pointers to each node. When searching for the shortest path, non-empty backward pointers define a closed set of algorithms (they prohibit loops). When the shortest path is found, reverse pointers allow you to restore the sequence of nodes in this path. And the moment when the shortest path is found is determined by visiting node 1 with (node_3_not_visited || S%3==0) && (node_7_not_visited || S%7==0) .

+2
source

First mathematically find the LCM numbers given in the set.

lemme rephrase the script .... given the set of numbers ... find LCM, then go through vetices so that their path does the number. Since its LCM is a number whose sum is mininum

For the set {0,1,2,3,4} LCM is 12, so traverses from 1 to 2 for the set {0,1,2,3,4,5,6,7} LCM are 420 .. (I think I'm right)

0
source

Use the A * search algorithm, where "cost" is the sum of the numbers, and divisibility determines which edges you can move to.

0
source

Source: https://habr.com/ru/post/1411155/


All Articles