codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1255B.md (3224B)


      1 Source: https://codeforces.com/problemset/problem/1255/B
      2 
      3 B. Fridge Lockers
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Hanh lives in a shared apartment. There are $n$ people (including Hanh) living there, each has a private fridge.
     22 
     23 $n$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $n$ people can open it.
     24 
     25  ![](https://espresso.codeforces.com/68e1abf04b28da31cf7c02ee4a2c85e921afb6c3.png) For exampe, in the picture there are $n=4$ people and $5$ chains. The first person knows passcodes of two chains: $1-4$ and $1-2$. The fridge $1$ can be open by its owner (the person $1$), also two people $2$ and $4$ (acting together) can open it. 
     26 
     27 The weights of these fridges are $a_1, a_2, \ldots, a_n$. To make a steel chain connecting fridges $u$ and $v$, you have to pay $a_u + a_v$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
     28 
     29 Hanh's apartment landlord asks you to create exactly $m$ steel chains so that all fridges are private. A fridge is private if and only if, among $n$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $i$ is not private if there exists the person $j$ ($i \ne j$) that the person $j$ can open the fridge $i$.
     30 
     31 For example, in the picture all the fridges are private. On the other hand, if there are $n=2$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
     32 
     33 Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $m$ chains, and if yes, output any solution that minimizes the total cost.
     34 
     35 Input
     36 
     37 Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 10$). Then the descriptions of the test cases follow.
     38 
     39 The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 1000$, $1 \le m \le n$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
     40 
     41 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^4$) — weights of all fridges.
     42 
     43 Output
     44 
     45 For each test case:
     46 
     47 * If there is no solution, print a single integer $-1$.
     48 * Otherwise, print a single integer $c$ — the minimum total cost. The $i$-th of the next $m$ lines contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$), meaning that the $i$-th steel chain connects fridges $u_i$ and $v_i$. An arbitrary number of chains can be between a pair of fridges.
     49 
     50 If there are multiple answers, print any.
     51 
     52 Example
     53 
     54 Input
     55 
     56 ```
     57 3
     58 4 4
     59 1 1 1 1
     60 3 1
     61 1 2 3
     62 3 3
     63 1 2 3
     64 ```
     65 
     66 Output
     67 
     68 ```
     69 8
     70 1 2
     71 4 3
     72 3 2
     73 4 1
     74 -1
     75 12
     76 3 2
     77 1 2
     78 3 1
     79 ```