Given translators as weighted undirected edges, choose a set minimizing each target language's hop distance from English first, then total fee.
Medium6GraphBFSShortest pathGreedyNo attempts yetTime limit2sMemory limit512 MBYou have just finished writing a book called "How to Win a Programming Contest", and requests are coming in from several countries. You know plenty of programming languages, but almost no spoken ones. Asking around, you found a number of people who translate between two languages, each for a different fee. Sometimes several translations in a row are needed. Even if nobody translates English straight into Swedish, one person who turns English into French plus another who turns French into Swedish gets you the Swedish edition.
Keeping the total fee small matters, but the stronger condition is to minimise how many translations each target language sits away from English, because every extra translation adds errors. So first make the number of translations from English to each target language as small as it can be, and among the ways that satisfy that, pick the one whose total fee is smallest.
Hiring a translator costs the given fee once and lets you move text between that person's two languages in either direction.
The first line contains two integers n and m, the number of target languages and the number of translators available (1≤n≤100, 1≤m≤4500).
The second line contains the names of the n target languages, separated by spaces.
Each of the next m lines describes one translator in the form l1 l2 c. Here l1 and l2 are two different languages and c is an integer fee for translating between them in either direction (1≤c≤109).
Both l1 and l2 are always either English or one of the target languages, and any pair of languages appears at most once in the input. A language name is a string without spaces. The original book is always written in English.
Print on one line the minimum cost of translating your book into all of the target languages under the conditions above. If getting every target language is not possible, print Impossible.