This page is still under construction.

Parts of this page are still being built. What you see may change.

Journey

Time limit2sMemory limit512 MB

Summary
Two weighted graphs share vertices; alternate one edge per graph, each strictly decreasing that graph's distance to t, and find the longest total route or -1 if infinite.
Level

Hard8 of 10

Topics
Shortest path, Dynamic programming, Graph, Greedy
Solved
No attempts yet

Problem

An army marches from the city of Kostroma to the village of Domino. Two generals, Stefan and Konstantin, lead it.

The generals carry different maps of the same region. The villages sit at the same places on both maps, but Stefan's map records only the main roads and Konstantin's map records only the narrow side paths. Walking a main road by day is dangerous, so the generals move the army like this. At night they follow one main road from Stefan's map, and by day they follow one side path from Konstantin's map.

A spy named Susanin marches with the army. He studies both maps and decides which road each general picks. His aim is to make the march to Domino as long as he can. Moving in a direction that has nothing to do with Domino would expose him, so Susanin only picks a road along which the shortest distance to the destination strictly decreases. The road he picks for Stefan must decrease the shortest distance to Domino measured with the main roads alone, and the road he picks for Konstantin must decrease the shortest distance to Domino measured with the side paths alone.

Find the length of the longest route Susanin can produce.

Input

The first line contains the number of villages on the maps nn, the number ss of the city of Kostroma where the march starts, and the number tt of the village of Domino where the march ends. (2≤n≤10002 \le n \le 1000, 1≤s,t≤n1 \le s, t \le n, s≠ts \ne t) The villages are numbered from 11 to nn.

Two blocks follow in this order, one for Stefan's map and one for Konstantin's map.

The first line of each block contains the number of roads mm. (n−1≤m≤100000n-1 \le m \le 100000)

Each of the next mm lines contains three natural numbers aa, bb, ll. This means a bidirectional road of length ll joins village aa and village bb. (1≤a,b≤n1 \le a, b \le n, 1≤l≤10000001 \le l \le 1000000) Several roads may join the same pair of villages, and a road whose aa equals its bb may appear.

Every village is connected on each map, so the roads of a single map already reach every village from every other village. The army starts at village ss and makes its first move at night, so the first map it uses is Stefan's map. After that it takes one main road each night and one side path each day.

Output

Print the length of the longest route Susanin can produce before the army reaches Domino. The length of a route is the sum of the lengths of every main road and side path used along it. If Susanin can keep the army moving forever without ever reaching Domino, print -1.

Examples2

  1. Example 1

    Input
    5 1 5
    5
    1 2 2
    1 4 2
    2 3 1
    3 4 1
    5 3 1
    4
    1 2 2
    2 4 2
    2 3 1
    2 5 2
    
    Expected output
    -1
    
  2. Example 2

    Input
    3 1 3
    4
    1 2 10
    2 3 10
    1 3 20
    2 3 30
    4
    2 1 10
    1 3 10
    1 1 10
    2 3 10
    
    Expected output
    20