Routes

시간 제한4초메모리 제한512 MB

요약
기차 노선과 k개의 열기구 구역으로 덮인 도시들에서 모든 도시 쌍의 최단 이동 시간 합을 구한다.
난이도

어려움10점 중 8점

유형
그래프, 최단 경로, 비트 연산
정답자
아직 제출이 없습니다

문제

A long long time ago, there were n cities and m railways in a kingdom. Each railway contained many cites, and each city was located on exactly one railway. Since some cities were not connected by a railway, people often made an arduous journey when moving from one city to another. The inconvenient traffic had greatly blocked the development of the kingdom’s economy, and therefore its king soon made a decision to launch several airline routes.

Under his directive, the cities were divided into k districts, and thus people were able to travel between every two cities belonging to the same district by hot air balloons. Furthermore, when planning to travel to any other city, people in the kingdom could choose to go by the combination of trains and hot air balloons without any other kinds of transportation, which was also a reason why the economy continued to prosper.

The king wanted to know if the policy he made had benefited everyone. However, he didn’t find it out before he died, as a huge amount of calculation was required. Now that the computing ability is better, the problem is called back to solve again. Assuming that moving from a city to another city directly by hot air balloon will spend one hour, and traveling from a city to an adjacent city on a railway will spend one hour as well, you are asked to determine the average time cost needed to travel from a city to another city by using only these two kinds of transportation, and then report the product of n(n−1)/2 and the average value, which ought to be an integer.

To avoid huge input data, each city is represented as one of the first k lowercase letters, representing a station on a railway, such that cities represented by the same letter are in the same district.

입력

The input contains several test cases. The first line contains an integer T indicating the number of test cases. The following describes all test cases. For each test case:

The first line contains three integers n, m and k.

Each of the following m lines contains a non-empty string, consisting of the first k lowercase letters, describing a railway’s stations in order.

출력

For each test case, output a line containing “Case #x: y” (without quotes), where x is the test case number starting from 1, and y is the answer (in hours) to this test case.

제한

  • 1 ≤ T ≤ 1000
  • 1 ≤ m ≤ n ≤ 106
  • 1 ≤ k ≤ 16
  • Each district contains at least one city.
  • The sum of n in all test cases does not exceed 5 × 106.
  • There are at most 5 test cases with k > 8.

예제1

  1. 예제 1

    입력
    3
    2 1 2
    ab
    2 2 1
    a
    a
    5 2 3
    abb
    ac
    
    예상 출력
    Case #1: 1
    Case #2: 1
    Case #3: 20