Under construction

Still being built. Things may change

Edges and Divisors

Time limit1sMemory limit2048 MB

Summary
Given a weighted DAG and weights a_1 to a_(n-1), choose a chain of paths whose lengths are multiples of i+1 in round i, maximizing the weighted average score.
Level

Hard10 of 10

Topics
Graph, Dynamic programming, Number theory
Solved
No attempts yet

Problem

Time limit: 1000 ms. Memory limit: 2048 MB.

We play a game on a directed acyclic graph G=(V,E)G = (V, E). We start at vertex s_1∈Vs\_1 \in V. The rules are as follows:

  • The game consists of rounds numbered from 11.
  • In the ii-th round, the player selects a path p_ip\_i that starts at s_is\_i and contains at least one edge, such that the sum of the weights of its edges is an exact multiple of (i+1)(i + 1). If no such path exists, the player fails and scores no points. Otherwise, the round ends successfully, and the endpoint of p_ip\_i is recorded as s_i+1s\_{i + 1}.
  • After a successful round, the player can either end the game or continue with the next round. If the player ends the game, the selected ii paths p_1,…,p_ip\_1, \ldots, p\_i are called doubling paths, and the score is calculated.

If the player has not failed, then when the game ends, the score for the selected doubling paths p_1,…,p_kp\_1, \ldots, p\_k is ∑_i=1ka_i∣p_i∣/k\sum\_{i = 1}^{k} a\_i \left|p\_i\right| / k, where ∣p_i∣|p\_i| is the number of edges in path p_ip\_i, and a_ia\_i is the weight given in the input. Since the graph is acyclic, at most (n−1)(n-1) paths can be selected, so the input provides only the weights a_1,…,a_n−1a\_1, \ldots, a\_{n-1}.

Given the graph and the starting vertex, compute the maximum achievable score.

Input

The first line contains three integers nn, mm, and s_1s\_1: the number of vertices, the number of edges, and the index of the starting vertex (2≤n≤1002 \le n \le 100; 1≤m≤n(n−1)21 \le m \le \frac{n (n - 1)}{2}; 1≤s_1≤n1 \le s\_1 \le n).

The second line contains (n−1)(n-1) integers a_1,…,a_n−1a\_1, \ldots, a\_{n-1}, the weights used to calculate the score (1≤a_1≤a_2≤…≤a_n−1≤1091 \le a\_1 \le a\_2 \le \ldots \le a\_{n - 1} \le 10^9).

Each of the next mm lines contains three integers u_iu\_i, v_iv\_i, and w_iw\_i, describing a directed edge from u_iu\_i to v_iv\_i with weight w_iw\_i (1≤u_i,v_i≤n1 \le u\_i, v\_i \le n; u_i≠v_iu\_i \ne v\_i; 1≤w_i≤1091 \le w\_i \le 10^9). The graph is acyclic, connected when edges are treated as undirected, and has no multiple edges.

Output

Output a single line containing two integers separated by a space.

If at least one path can be selected, an optimal selection scheme exists, and the optimal score can be written as p/qp/q where pp and qq are coprime integers and q>0q > 0. In this case, output pp and qq. Otherwise, if no path can be selected, output -1~-1.

Hint

The selected doubling paths are p_1=((1,2))p\_1 = ((1, 2)) and p_2=((2,5))p\_2 = ((2, 5)).

Examples2

  1. Example 1

    Input
    5 5 1
    1 11 21 1211
    1 2 4
    1 3 11
    2 5 9
    3 4 12
    4 5 13
    
    Expected output
    6 1
    
  2. Example 2

    Input
    9 16 1
    1 10 100 1000 10000 100000 1000000 10000000
    1 2 2
    1 3 3
    2 3 5
    2 5 7
    3 4 11
    3 5 13
    3 6 17
    3 7 19
    4 7 23
    5 6 29
    5 8 31
    6 7 37
    6 8 41
    6 9 43
    7 9 47
    8 9 53
    
    Expected output
    221 3