Encounter with the Lord
Practice
4.1 (39 votes)
Graphs
Medium
Problem
71% Success 182 Attempts 50 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Crossing all the hurdles, Gudi now reaches the roof of the castle and she finds Puchi, the Lord of Azkahar and the Protector of the castle, waiting for her. After having been through the stages in the castle, Gudi has had a change of heart and no longer wants the treasure. Instead, she expresses her desire to become the Lady of Azkahar. However, the position of the Lady can not be won that easily. Puchi decides to take a final test.
He weaves his magic and N Islands, spread across a vast sea , appear before them. He goes on the say :

The Lady needs to rule the masses and take important decisions. Let us see how well can you do that. These are N Islands, indexed from 1 to N. There are M bidirectional roads in total, wherein each road connects 2 distinct Islands and multiple roads may connect a pair of Islands. Each of the first K Islands i.e. Islands with index from 1 to K, have a soldier who fought valiantly in the battle and wants to get to a shelter. Each of the last K Islands i.e. Island with index from \( N - K + 1\) to N, have a shelter home that can house only 1 Soldier.
These soldiers await your commands. The cost effort of reaching a shelter for a soldier is the distance that has to be traveled. In addition, you have the option of using magic to transport the soldiers. One use of magic will allow you to move any solider from any island to any other island. Using your magic once costs \( 10^4 \) units of effort. You can always use your magic, but then again it comes at a huge cost. Give a target shelter to each of the soldiers and minimize the overall cost efforts of the scenario.


Help Gudi find the minimum cost efforts.

Input
First line contains an integer T.T testcases follow.
Each testcase consists of 3 space-separated integers N, M and K. M lines follow.
Each of these lines, contains 3 integers X, Y and C, denoting that there is a road between Island X and Island Y of distance C units.

Output
Print the answer to each testcase in a new line.

Constraints
\( 1 \le T \le 10\)
\( 1 \le K \le 80\)
\( 2 K +1 \le N \le 200\)
\( 1 \le M \le 1000\)
\( 1 \le X,Y \le N\)
\( 1 \le C \le 1000\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
AlgorithmsGraphsMediumMinimum Cost Maximum Flowmcmf
Points:50
3 votes
Tags:
AlgorithmsGraphsMediumMinimum Cost Maximum Flow