#P10947. [BAPC 2006 Qualification] Sightseeing

[BAPC 2006 Qualification] Sightseeing

Problem Description

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city SS to another city FF. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.

Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from SS to FF. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city SS and the final city FF, though, are fixed. Two routes from SS to FF are considered different if there is at least one road from a city AA to a city BB which is part of one route, but not of the other route.

There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from SS to FF. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.

For example, for the above road map, there are two minimal routes from S=1S = 1 to F=5F = 5: 1251 \to 2 \to 5 and 1351 \to 3 \to 5, both of length 66. There is one route that is one distance unit longer: 13451 \to 3 \to 4 \to 5, of length 77.

Now, given a (partial) road map of the Benelux and two cities SS and FF, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.

Input Format

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with two integers NN and MM, separated by a single space, with 2N10002 \leq N \leq 1000 and 1M100001 \leq M \leq 10000: the number of cities and the number of roads in the road map.

  • MM lines, each with three integers AA, BB and LL, separated by single spaces, with 1A,BN1 \leq A, B \leq N, ABA \neq B and 1L10001 \leq L \leq 1000, describing a road from city AA to city BB with length LL.The roads are unidirectional. Hence, if there is a road from AA to BB, then there is not necessarily also a road from BB to AA. There may be different roads from a city AA to a city BB.

  • One line with two integers SS and FF, separated by a single space, with 1S1 \leq S, FNF \leq N and SFS \neq F: the starting city and the final city of the route. There will be at least one route from SS to FF.

Output Format

For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 109=100000000010^9 = 1000000000.

2
5 8 
1 2 3 
1 3 2 
1 4 5 
2 3 1 
2 5 3 
3 4 2 
3 5 4 
4 5 3 
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2 
5 2 7 
5 2 7 
4 1
3
2