Giant Min Cost Bipartite Matching
Time limit4.2sMemory limit512 MB
Given a bipartite graph with N vertices per side where every vertex has degree at most 2, find the minimum total cost of a matching of each size from 1 to N.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
The bipartite graph has vertices on the left side, vertices on the right side, and edges. Every vertex has degree at most 2. Left and right vertices are each numbered from to , and two vertices are joined by at most one edge.
A matching is a set of edges where no two edges share an endpoint. For each size , , , , find the minimum cost of a matching of that size.
Input
The first line contains and , the number of vertices and the number of edges. (, )
Each of the next lines contains : is the left vertex, is the right vertex, and is the cost of that edge. (, )
Output
Print lines. Line contains the minimum cost of a matching of size . If no such matching exists, print -1.