CCC '10 S4 - Animal Farm

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type
Canadian Computing Competition: 2010 Stage 1, Senior #4

You are running a farm which has N (1 \le N \le 100) animals. You went to the store and bought M = N pre-made pens that will house your animals. Pens satisfy the following conditions:

  • pens have between 3 and 8 edges;
  • an edge that is specified by two pens connects the two pens;
  • an edge that is specified only once connects that pen to the outside;
  • there is exactly one animal in each pen and no animals outside the pens, initially.

The animals, however, have a game they like to play called "Escape from the pen." They assign a cost to each edge of the pen, and they determine the minimum cost for all of the animals to meet in the same area by trampling over the edge of various pens. The animals may meet inside a particular pen or outside of all the pens. Also note that once an edge has been trampled, any animal may pass over it without incurring any cost.

You will be given a description of the pens, along with the placement of animals, and you are to figure out what the smallest cost is to move all the animals into the same area.

Input Specification

The first line of input will be the integer M, the number of pens. On the next M lines, there will be a description of each pen, with one description per line. The description is composed of three components, with each component separated by one space, as follows:

  • the first component is an integer e_p (3 \le e_p \le 8), which describes the number of edges for this particular pen p;
  • the second component is a sequence of e_p integers describing the corners of each pen, where each integer is less than or equal to 1\,000;
  • the third component is a sequence of e_p integers describing the cost of each edge, where each integer is less than or equal to 5\,000.

For the corner and edge cost description, the descriptions are given in cyclical order. For example, the following description of a pen

\displaystyle  3\ 1\ 2\ 3\ 7\ 4\ 6

means that there are three corners, and thus, three edges, where the edge (1, 2) has cost 7, the edge (2, 3) has cost 4 and the edge (3, 1) has cost 6. Note: at least 20% of the marks for this question have N \le 10 and no pen will have more than four edges in these cases.

Output Specification

On one line, output the minimal cost that will allow all the animals to gather in one pen or outside all of the pens.

Sample Input

3 1 2 3 7 4 6
4 1 2 4 5 7 7 2 6
4 4 7 6 5 4 8 9 2
5 3 2 4 7 8 4 7 4 7 7

Output for Sample Input


Explanation of Output for Sample Input

The diagram below explains the input data:

where the circled numbers are the corners, and the numbers in italics are the edge costs. Notice that if the edges (2, 3), (4, 5) and (4, 7) are removed, all the animals can meet in the pen which has five sides.


  • -5
    alihu264  commented on April 28, 2020, 5:44 a.m.

    This comment is hidden due to too much negative feedback. Click here to view it.

  • 3
    mango8023  commented on Aug. 16, 2018, 11:01 p.m.

    i study the code from 'aurpine', and find out that his/her code cannot run the sample input/output correctly! because the graphs in all of the test data are NOT connected, but the graph in sample input is connected. can u fix that?

  • 5
    OOOIII  commented on Feb. 20, 2017, 10:25 p.m.

    Passed all test cases but not SAMPLE explanation request

    • 0
      Kevy3030  commented on May 7, 2020, 11:31 p.m.

      In the sample all the pens were together in one group. It just so happened that in all of the test cases the pens were in two or more different clusters.

  • 4
    sunnylancoder  commented on Dec. 28, 2016, 3:48 p.m.

    Can costs be negative?

    • 1
      Arcslogger  commented on Oct. 26, 2019, 10:55 a.m.

      The question doesn't say that the cost has to be positive

    • 3
      USER18381  commented on Oct. 26, 2019, 9:18 a.m. edited

      No. I highly doubt that.

  • 2
    atarw  commented on April 25, 2016, 8:20 p.m.

    an edge that is specified only once connects that pen to the outside;

    I don’t understand what this means, what would a sample input of a pen connected to the outside be?

    • 2
      Roronoa_Zoro1540  commented on Feb. 9, 2020, 9:35 p.m.

      means that if you see the specific edge in the input only once that means it must connect the pen to the outside (i.e you don't see the edge again in another pen's description)

  • 6
    XIAOAGE  commented on Dec. 27, 2015, 9:20 p.m. edited

    _< What a great trap...........

    • 2
      bobhob314  commented on Dec. 27, 2015, 9:46 p.m.

      Lol Bryan didn't know you're moved to Thornhill S.S., welcome aboard, we meet Thursdays

      • 2
        XIAOAGE  commented on Dec. 27, 2015, 9:51 p.m.

        LOL, i m in Thornhill S.S. virtually :)

  • 2
    kobortor  commented on Feb. 27, 2015, 10:21 p.m.

    Image link is broken.

    • 3
      FatalEagle  commented on Feb. 28, 2015, 1:01 a.m.