PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000098. Mua sắm và đường đi tối ưu (Shopping)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Mua sắm và đường đi tối ưu (Shopping)

Shopping

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected graph has nn intersections indexed from 00. Home is node 00 and a set of shops must be visited. Find the shortest route that starts at home, visits every shop and returns home.

Input

First n,mn,m, then mm roads u,v,wu,v,w using zero-based IDs; then ss and ss shop vertices.

Output

Print the minimum total distance or -1 if impossible.

Subtasks

  • Subtask 1 (40 points): 3≤n≤9,  1≤s≤43\le n\le 9,\;1\le s\le 4. All input-format and graph conditions above apply.

  • Subtask 2 (60 points): 3≤n≤28,  1≤s≤83\le n\le 28,\;1\le s\le 8. All input-format and graph conditions above apply.

Examples

Input

2 1
0 1 4
1
1

Output

8

Explanation

Go from home 0 to store 1 at distance 4 and return along the same road at distance 4. The round trip costs 4+4=84+4=8.