TRAFFICN - Traffic Network

The city traffic network consists of n nodes numbered from 1 to n and m one-way roads connecting pairs of nodes. In order to reduce the length of the shortest path between two different critical nodes s and t, a list of k two-way roads are proposed as candidates to be constructed. Your task is to write a program to choose one two-way road from the proposed list in order to minimize the resulting shortest path between s and t.


The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

For each data set, the first line contains five positive integers n (n ≤ 10 000), m (m ≤ 100 000), k (k < 300), s (1 ≤ s ≤ n), t (1 ≤ t ≤ n) separated by space. The ith line of the following m lines contains three integers di, ci, li separated by space, representing the length li ( 0< li ≤ 1000) of the ith one-way road connecting node di to ci. The jth line of the next k lines contains three positive integers uj, vj and qj (qj ≤ 1000) separated by space, representing the jth proposed two-way road of length qj connecting node uj to vj.


For each data set, write on one line the smallest possible length of the shortest path after building the chosen one two-way road from the proposed list. In case, there does not exist a path from s to t, write -1.


Sample Input
4 5 3 1 4
1 2 13
2 3 19
3 1 25
3 4 17
4 1 18
1 3 23
2 3 5
2 4 25	

Sample Output

hide comments
Panagiotis Kostopanagiotis: 2013-01-08 12:47:55

Last edit: 2013-03-14 18:28:49
Rocker3011: 2012-10-10 01:38:54

can somebody provide me a tricky test case? all of them seems to work just fine :7

Pratham Khandelwal: 2010-08-25 11:00:04

You may or may not use one two-way road from the proposed list.

Lewin Gan: 2010-05-04 21:40:12

I wrote this in Java and got TLE, but rewrote the same exact algorithm in C++ and got AC
The time limit for Java users should be reconsidered (there are no AC java solutions at this moment)

Last edit: 2010-05-05 00:05:01
Jorge Bernadas: 2009-11-21 19:31:25

@writer: Might you add this case:

5 3 1 1 5
1 2 1
2 3 1
3 1 1
4 5 1

A friend's (danielmaxx) previous solution gave AC but TLE'd on this case.

Shaka Shadows: 2009-09-23 22:22:23

What if the "enhanced" shortest path isn't better than the regular one???

Added by:Duc
Time limit:0.140s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ACM Regional, Ho Chi Minh City 2008