cyberland — APIO contest
Task type: batch | Time limit: 2.5 s | Memory limit: 2048 MB | Difficulty: 19
Algorithms: dijkstra, shortest path, graph theory

# Cyberland

The year 3742 has arrived, and now it is Cyberland's turn to host the APIO. In this world, there are *N* countries indexed from 0 to *N* − 1, along with *M* bidirectional roads (allowing travel in both directions) indexed from 0 to *M* − 1. The *i*-th road (0 ≤ *i* < *M*) connects two different countries, *x*[*i*] and *y*[*i*], and requires a certain amount of time *c*[*i*] to pass the road. All participants have gathered in Cyberland for the APIO, except for your country. You are living in country 0, and Cyberland is country *H*. As the cleverest person in your country, your assistance is urgently needed once again. To be more specific, you are asked to determine the minimum time required to reach Cyberland from your country.

Some countries can clear your total passing time. Also, some countries can divide your total passing time by 2 (divide-by-2 ability). You can visit a country repeatedly. Every time you visit a country, you may choose whether to use the special ability in the country. But you can use the special ability at most once in a single visit (which means that special ability can be used multiple times by visiting the country multiple times). Moreover, you can only use the divide-by-2 ability at most *K* times in case of being caught by Cyberland Chemistry Foundation. Once you reach Cyberland, you cannot move anywhere because the great APIO contest will be held soon.

An array *arr* is given, where *arr*[*i*] (0 ≤ *i* < *N*) shows the special abilities of country *i*. There are 3 types of special abilities:

- *arr*[*i*] = 0, means this country makes the passing time 0.
- *arr*[*i*] = 1, means the passing time remains unchanged at this country.
- *arr*[*i*] = 2, means this country divides the passing time by 2.

It is guaranteed that *arr*\[0] = *arr*\[*H*] = 1 holds. In other words, Cyberland and your country do not have any special abilities.

Your country does not want to miss any moment of APIO, so you need to find the minimum time to reach Cyberland. If you cannot reach to Cyberland, your answer should be -1.

## Implementation Details

You need to implement the following function:

```cpp
double solve(int N, int M, int K, int H, std::vector<int> x, std::vector<int>
y, std::vector<int> c, std::vector<int> arr);
```

- *N*: The number of countries.
- *M*: The number of bidirectional roads.
- *K*: The limit of divide-by-2 ability usage.
- *H*: The index of the country Cyberland.
- *x*, *y*, *c*: three arrays with a length of *M*. the tuple (*x*[*i*], *y*[*i*], *c*[*i*]) represents the *i*-th undirected edge which connects country *x*[*i*] and *y*[*i*], with time cost *c*[*i*].
- *arr*: an array with a length of *N*. *arr*[*i*] represents the special ability of country *i*.
- This procedure should return the minimum time to reach Cyberland from your country if you can reach Cyberland, and -1 if you cannot do so.
- This procedure can be called more than once.

Assume that return value of contestant's is *ans*<sub>1</sub>, and the return value of standard's is *ans*<sub>2</sub>, your return value is considered correct if and only if $\frac{|ans_1 - ans_2|}{max\{ans_2, 1\}} \leq 10^{-6}$.

Note: Since the procedure can be called more than once, contestants need to pay attention to the impact of the remaining data of the previous call on the current call.

## Examples

### Example 1

Consider the following call:

```cpp
solve(3, 2, 30, 2, {1, 2}, {2, 0}, {12, 4}, {1, 2, 1});
```

The only path to Cyberland is 0 → 2, because you can not move to anywhere after reaching Cyberland. The calculation of passing time is shown as below.

| country number | passing time                                                                 |
| :------------- | :--------------------------------------------------------------------------- |
| 0              | 0                                                                            |
| 2              | 0 + 4 → 4 (sum) → 4 (special ability)                                        |

Therefore, the procedure should return 4.

### Example 2

Consider the following call:

```cpp
solve(4, 4, 30, 3, {0, 0, 1, 2}, {1, 2, 3, 3}, {5, 4, 2, 4}, {1, 0, 2, 1});
```

There are two paths from your country to Cyberland. They are: 0 → 1 → 3 and 0 → 2 → 3.

If your path is 0 → 1 → 3, the calculation of passing time is shown as below.

| country number | passing time                                                                 |
| :------------- | :--------------------------------------------------------------------------- |
| 0              | 0                                                                            |
| 1              | 0 + 5 → 5 (sum) → 0 (special ability)                                        |
| 3              | 0 + 2 → 2 (sum) → 2 (special ability)                                        |

If your path is 0 → 2 → 3, the calculation of passing time is shown as below.

| country number | passing time                                                                 |
| :------------- | :--------------------------------------------------------------------------- |
| 0              | 0                                                                            |
| 2              | 0 + 4 → 4 (sum) → 2 (special ability)                                        |
| 3              | 2 + 4 → 6 (sum) → 6 (special ability)                                        |

Therefore, the procedure should return 2.

## Constraints

- 2 ≤ *N* ≤ 10<sup>5</sup>, and ∑*N* ≤ 10<sup>5</sup>.
- 0 ≤ *M* ≤ min{10<sup>5</sup>, $\frac{N(N-1)}{2}$}, and ∑*M* ≤ 10<sup>5</sup>.
- 1 ≤ *K* ≤ 10<sup>6</sup>.
- 1 ≤ *H* < *N*.
- 0 ≤ *x*[*i*], *y*[*i*] < *N*, and *x*[*i*] ≠ *y*[*i*].
- 1 ≤ *c*[*i*] ≤ 10<sup>9</sup>.
- *arr*[*i*] ∈ {0, 1, 2}.
- It is guaranteed that every pair of countries is connected by at most one road.

## Subtasks

- 1. (5 points): *N* ≤ 3, *K* ≤ 30.
- 2. (8 points): *M* = *N* − 1, *K* ≤ 30, *arr*[*i*] = 1, you can travel from any countries to another via the *M* edges.
- 3. (13 points): *M* = *N* − 1, *K* ≤ 30, *arr*[*i*] ∈ {0, 1}, you can travel from any countries to another via the *M* edges.
- 4. (19 points): *M* = *N* − 1, *K* ≤ 30, *x*[*i*] = *i*, *y*[*i*] = *i* + 1.
- 5. (7 points): *K* ≤ 30, *arr*[*i*] = 1.
- 6. (16 points): *K* ≤ 30, *arr*[*i*] ∈ {0, 1}.
- 7. (29 points): *K* ≤ 30.
- 8. (3 points): No additional constraints.

## Sample Grader

The sample grader reads the input in the following format:

- line 1: *T*

For each of the following *T* test cases:

- line 1: *N M K*
- line 2: *H*
- line 3: *arr*[0] *arr*[1] *arr*[2] ... *arr*[*N* − 1]
- line 4 + *i* (0 ≤ *i* ≤ *M* − 1): *x*[*i*] *y*[*i*] *c*[*i*]

The sample grader prints your answers in the following format:

For each test case:

- line 1: the return value of solve

Starter code:
stub.cpp: #include "cyberland.h"

#include <cassert>
#include <cstdio>

#include <vector>

int main() {
  int T;
  assert(1 == scanf("%d", &T));
  while (T--){
    int N,M,K,H;
    assert(4 == scanf("%d %d %d\n%d", &N, &M, &K, &H));
    std::vector<int> x(M);
    std::vector<int> y(M);
    std::vector<int> c(M);
    std::vector<int> arr(N);
    for (int i=0;i<N;i++)
      assert(1 == scanf("%d", &arr[i]));
    for (int i=0;i<M;i++)
      assert(3 == scanf("%d %d %d", &x[i], &y[i], &c[i]));
    printf("%.12lf\n", solve(N, M, K, H, x, y, c, arr));
  }
}

cyberland.h: #include <vector>

double solve(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr);

cyberland.cpp: #include "cyberland.h"

#include <vector>

double solve(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr) {
    return -1;
}