All Tracks Algorithms Graphs Minimum Cost Maximum Flow Problem

Encounter with the Lord

Bipartite Graph, Graph Theory, Medium-Hard


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.

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.

Print the answer to each testcase in a new line.

$$ 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$$

6 8 2
1 2 2
1 6 5
1 4 1
4 3 1
3 6 1
3 5 2
4 5 1
2 6 2
6 5 2
3 1 2
1 4 1
3 5 3
3 6 1
4 3 2

For the testcases, Soldiers on Island 1,2 and Shelters on Island 5,6.
In the first testcase, the soldier on Island 1 can travel to Island 5 with cost=2 and the soldier on Island 2 can travel to Island 6 with cost = 2. Hence, total cost=4.
In the second testcase, the soldier on Island 1 can travel to Island 6 with cost=3 and the soldier on Island 2 has to be transported magically to Island 5 with cost= 10000, thereby total cost= 10003

Time Limit: 5.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Scala 2.11.8, Swift, Visual Basic


Initializing Code Editor...
Your Rating:


This Problem was Asked in


Challenge Name

July Easy '16

View All Notifications