Fast Algorithm
시간 제한2초메모리 제한2048 MB
약하게 연결된 방향 그래프에서 간선 가중치 합이 최소인 사이클을 찾아 그 값을 출력한다. m - n은 1500 이하이다.
문제
Little I has invented an algorithm for finding the minimum cycle in a directed graph with the time complexity of , and now he wants to test your skills.
You are given a directed graph with vertices and edges. Each edge has a positive integer weight. Your task is to find a cycle in the graph such that the sum of edge weights on the cycle is minimized. Output the minimum value of this sum or report if there is no cycle.
Of course, since you may not know the algorithm for finding the minimum cycle, Little I has relaxed the conditions: the input graph is guaranteed to be weakly connected, and won't be very large. A graph is weakly connected if and only if it becomes a connected undirected graph after replacing directed edges with undirected edges.
입력
The first line contains two integers and (, ) representing the number of vertices and edges in the graph.
Each of the next lines contains three integers , , (, ) representing a directed edge from to with weight . The graph is guaranteed to be weakly connected.
출력
Print a line with a single integer: the length of the minimum cycle in the graph, or if there is no cycle.
힌트
In the first example, the minimum cycle is .