题目链接:
Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently all his neighbors have decided to connect themselves over a network (actually all of them want to share a broadband internet connection :-)). But he wants to minimize the total cost of cable required as he is a bit fastidious about the expenditure of the project. For some unknown reasons, he also wants a second way left. I mean, he wants to know the second best cost (if there is any which may be same as the best cost) for the project. I am sure, he is capable of solving the problem. But he is very busy with his private affairs(?) and he will remain so. So, it is your turn to prove yourself a good programmer. Take the challenge (if you are brave enough)...
Input
Input starts with an integer t ≤ 1000 which denotes the number of test cases to handle. Then follows t datasets where every dataset starts with a pair of integers v (1 ≤ v ≤ 100) and e (0 ≤ e ≤ 200). v denotes the number of neighbors and e denotes the number of allowed direct connections among them. The following e lines contain the description of the allowed direct connections where each line is of the form ‘start end cost’, where start and end are the two ends of the connection and cost is the cost for the connection. All connections are bi-directional and there may be multiple connections between two ends.
Output
There may be three cases in the output 1. No way to complete the task, 2. There is only one way to complete the task, 3. There are more than one way. Output ‘No way’ for the first case, ‘No second way’ for the second case and an integer c for the third case where c is the second best cost. Output for a case should start in a new line.
Sample Input 4 5 4 1 2 5 3 2 5 4 2 5 5 4 5 5 3 1 2 5 3 2 5 5 4 5 5 5 1 2 5 3 2 5 4 2 5 5 4 5 4 5 6 1 0
Sample Output
Case #1 : No second way
Case #2 : No way
Case #3 : 21
Case #4 : No second way
题解:
1.求次小生成树。但是题目要求可以有重边,而prim算法处理的是点与点的关系,不能(至少很难)处理有重边的图。
2.求最小生成树,除了prim算法之外,还有kruskal算法,且因为它是直接依据边来进行操作的,所以就能很好地处理重边了。
3.步骤:先用kruskal算法求出最小生成树,同时记录下最小生成树边。然后枚举删除每一条最小生成树边,再去求最小生成树,最终即可得到次小生成树。
4.复杂度分析:O(n*m),计算次数为2e4,再乘上case数,计算次数为2e7,所以可行。
代码如下:
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 typedef long long LL; 7 const double EPS = 1e-6; 8 const int INF = 2e9; 9 const LL LNF = 9e18;10 const int MOD = 1e9+7;11 const int MAXN = 1e2+10;12 13 struct Edge14 {15 int u, v, w;16 bool operator<(const Edge &a)const{17 return w