这道题还是比较有考虑的价值的
求:判断是否有最小生成树,次小生成树,如果有次小生成树,则输出次小生成树的总权值
要注意的是,点和点之间是有重边的,要求次小生成树,就一定要保留所有重边
考虑到即要判断图是不是连通的,又要保存重边,所以Kruskal是首选,prim算法或许有解,但是本人实在是没有想出来怎么样比Kruskal能更加简单,如果大牛经过,求指点
次小生成树一定是最小生成树减一条边,再加一条边,枚举掉最小生成树中的边,一次求少一条边的图的最小生成树即可
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
const int M = 220;
const int INF = 1000000000;
int n, m, T, ans1, ans2, ise[N], f[N];
struct edge {
int u, v, cost;
}e[M];
bool cmp( edge a, edge b ) {
return a.cost < b.cost;
}
int find ( int x ) {
return f[x] == x ? x: f[x] = find(f[x]);
}
int Kru() {
int ans = 0, num = 0, id = 0;
for ( int i = 1; i <= n; ++i ) f[i] = i;
for ( int i = 0; i < m; ++i ) {
int x = e[i].u;
int y = e[i].v;
int a = find(x);
int b = find(y);
if ( a != b ) ise[id++] = i, f[a] = b, ans += e[i].cost;
}
for ( int i = 1; i <= n; ++i ) if ( i == find(i) ) num++;
if ( num > 1 ) return INF;
else return ans;
}
int Kru_1( int del ) {
int ans = 0, num = 0;
for ( int i = 1; i <= n; ++i ) f[i] = i;
for ( int i = 0; i < m; ++i ) {
if ( i == del ) continue;
int x = e[i].u;
int y = e[i].v;
int a = find(x);
int b = find(y);
if ( a != b ) f[a] = b, ans += e[i].cost;
}
for ( int i = 1; i <= n; ++i ) if ( i == find(i) ) num++;
if ( num > 1 ) return INF;
else return ans;
}
int main()
{
int idx = 1;
scanf("%d", &T);
while ( T-- ) {
scanf("%d%d", &n, &m);
for ( int i = 0; i < m; ++i )
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].cost);
sort( e, e+m, cmp );
ans1 = Kru(), ans2 = INF;
printf("Case #%d : ", idx++);
if ( ans1 == INF ) {
printf("No way\n");
continue;
}
for ( int i = 0; i < n-1; ++i ) {
int x = ise[i];
ans2 = min( ans2, Kru_1(x) );
}
if ( ans2 == INF ) printf("No second way\n");
else printf("%d\n", ans2);
}
}