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
sharif ullah: 2017-09-26 17:00:28

check these out
1. when i use INFINITY for upper value for calculating distance give wrong ans
2.when use cin,cout gives TLE fast i/o
3.while conntection a path with an edge onsider two case (a->b) && (b->a) (2 dijikstra approach)!!!!

sonudoo: 2017-06-19 17:57:47

A single dijkstra may also do. All you have to do is to create two graphs and the new roads will connect these two graphs.

Thotsaphon Thanatipanonda: 2017-06-04 06:23:04

length li or qj > 1000. It waste my time.

babur: 2017-05-26 08:40:45

Truely Classic...Twice dijkstra once straight and then reverse..

code_aim: 2017-05-25 22:13:51

One of the best problems of dijkstra.
Cost me 1WA.

sucide: 2017-04-02 20:39:12

Using fast input , using almost same code as an accepted solution still tle.
Dont know what is wrong

nky_007: 2017-03-18 11:40:23

nice question!!!

vengatesh15: 2017-02-10 12:08:20

easy one..

sonu: 2016-11-08 14:57:03

LLONG_MAX also WA but 1061109567 got accepted!

nabila ahmed: 2016-10-30 06:21:30

I think instead of "shortest path between s and t", it should be "shortest path from s to t".

Last edit: 2016-10-30 06:22:05

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