Best Edge
InterviewTime limit2sMemory limit1024 MB
Given a directed weighted graph with unique shortest paths, find the edges that lie on the most shortest paths between vertex pairs and print their numbers.
- Level
Medium5 of 10
- Topics
- Shortest path, Graph, Tree, Heap
- Solved
- No attempts yet
Problem
You are given a directed graph with vertices and edges. The edges are numbered from to in the order they appear in the input.
Soyeong thinks it is impressive when many pairs of vertices are connected by shortest paths. She gives each edge a score and awards the Best Edge prize to the edges with the highest score. An edge's score is computed as follows.
- If a shortest path exists from vertex to vertex , every edge on that shortest path gets 1 point. The input only contains data where the shortest path is unique for every pair of vertices that has a path.
- If no path exists from to , no edge gets a point for the pair .
- The final score of an edge is the sum of its points over all pairs with and .
If several edges share the highest score, all of them receive the prize. Find the edges that receive the Best Edge prize.
Input
The first line contains two integers and , separated by a space.
Each of the next lines contains the start vertex , the end vertex , and the length of the -th edge, separated by spaces. No two edges connect the same pair of vertices, and no edge starts and ends at the same vertex.
Output
On the first line, print the number of edges that receive the prize. On the second line, print their numbers in increasing order, separated by spaces.