Critical Path

Time limit2sMemory limit512 MB

Summary
On a DAG, find the longest path length from source to target, then count edges that lie on at least one such longest path.
Level

Medium6 of 10

Topics
Dynamic programming, Topological sort, Graph
Solved
No attempts yet

Problem

All roads lead to Rome.

A journey of a thousand ri begins with one step.

In World Country, every road is one-way, and there is no way to follow roads and return to the city where you started. In other words, the road network is a directed acyclic graph.

There are two special cities, One Step and Rome. From One Step, every city can be reached, and from every city there is a route to Rome. There are enough people to assign one person to every distinct route from One Step to Rome. They all start at the same time and run without resting. Each road has a fixed travel time.

How long does it take until every person reaches Rome?

Among the routes that take the longest time, every road used by at least one of those routes will be marked. Find how many roads must be marked.

Input

The first line contains the number of cities nn (1≤n≤10 0001 \le n \le 10\,000). The second line contains the number of roads mm (1≤m≤100 0001 \le m \le 100\,000).

Each of the next mm lines contains three integers uu, vv, and ww, describing a one-way road from city uu to city vv that takes ww time to traverse. Every road satisfies 1≤u,v≤n1 \le u, v \le n and 1≤w≤10 0001 \le w \le 10\,000.

The last line contains the city numbers of One Step and Rome, in that order.

Output

On the first line, print the time when every person has reached Rome.

On the second line, print the number of roads that belong to at least one longest route and therefore must be marked.

Examples1

  1. Example 1

    Input
    7
    9
    1 2 4
    1 3 2
    1 4 3
    2 6 3
    2 7 5
    3 5 1
    4 6 4
    5 6 2
    6 7 5
    1 7
    
    Expected output
    12
    5