Find every connected component using DFS or BFS. Within a component of size s, you have choices:
Build library and repair s - 1 roads (the minimum roads to connect s cities is a spanning tree). Cost: c_lib + (s - 1) * c_road.
Build a library in every city. Cost: s * c_lib.
Pick whichever is cheaper for each component. If c_road >= c_lib, option always wins. Otherwise, compare per component.
The total answer is the sum across all components.