思路:
比较典型的最小生成树的题目了、、在这里用求最小生成树的经典算法K(Kruskal)算法和P(Prim)算法。我的 K 算法用的是结构体来存图,P 算法用的是邻接矩阵来存图,K算法的复杂度是O(ElogE),比较适用于稀疏图,P算法的复杂的是O(V ^ 2),适合用稠密图。
以下是C++的K算法代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <algorithm>
using namespace std; const int MAXN = 2e3+ ;
int pre[MAXN];
int m,n; int Find(int x) //并查集
{
return x == pre[x] ? x :(pre[x] = Find(pre[x]));
} struct Node //存图
{
int u, v, w;
}cy[]; int mycmp(Node a,Node b)
{
return a.w < b.w;
} void mst()
{
for(int i = ; i < ; i++)
pre[i] = i;
} int kru()//算法的具体实现
{
int ans = ;
int cnt = ;
for(int i = ; i <= n; i++)
{
int fv = Find(cy[i].v);
int fu = Find(cy[i].u);
if(fv != fu)
{
pre[fv] = fu;
ans += cy[i].w;
cnt ++;
}
if(cnt == m -)
{
return ans;
break;
}
}
return -;
} int main()
{
//freopen("in.cpp","r",stdin);
while(~scanf("%d%d",&n,&m) && n)
{
mst();
for(int i = ; i <= n; i++)
scanf("%d%d%d",&cy[i].u, &cy[i].v, &cy[i].w);
sort(cy + , cy + n + , mycmp);
int ans = kru();
if(ans != -)
printf("%d\n",ans);
else
printf("?\n");
}
return ;
}
以下是C++的P算法代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <algorithm>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
int edge[MAXN][MAXN]; //用于存放图
int used[MAXN];//用于标记点是否已加入到最小生成树那个的集合中
int lowcost[MAXN]; //用于存放集合外的点到集合内的点的最短距离,每加入一个点需要更新一次
int N,M; int prim(int start,int maxn) //利用prim算法计算最小生成树
{
memset(used, , sizeof(used));
for(int i = ; i <= maxn; i++)
{
lowcost[i] = edge[start][i];
}
used[start] = ;
int sumweight = ;
int ok = ;
for(int i = ; i <= maxn; i++)
{
int minn = INF ;
int v = -;
for(int j = ; j <= maxn; j++)
{
if(used[j] == && lowcost[j] < minn)
{
minn = lowcost[j];
v = j;
}
}
//cout << v << " ";
if(v != -)
{
ok++;
used[v] = ;
sumweight += lowcost[v];
for(int j = ; j <= maxn; j++)
{
if(used[j] == && lowcost[j] > edge[v][j])
{
lowcost[j] = edge[v][j];
}
}
}
}
if(ok == maxn -)
return sumweight;
return -;
} int main()
{
while(cin >> N >> M && N)
{
memset(edge, 0x3f, sizeof(edge));
while(N--)
{
int u, v, w;
cin >> u >> v >> w;
edge[u][v] = edge[v][u] = w;
}
int ans = prim(, M);
if(ans < ) cout << "?" <<endl;
else cout << ans << endl;
}
return ;
}
以下是JAVA的K算法
import java.util.Scanner;
import java.util.Comparator;
import java.util.Arrays;
import java.text.DecimalFormat; class Vge{
int u;
int v;
int w;
} class mycmp implements Comparator<Vge>{
public int compare( Vge A, Vge B ){
if( A.w - B.w != 0 )
return A.w - B.w;
else
return A.w - B.w;
}
} public class Main{
final static int MAXN = 100 + 3;
static int[] pre = new int[ MAXN ];
static Vge[] clg = new Vge[ MAXN * MAXN ];
public static void main( String[] args ){
Scanner sc = new Scanner( System.in );
int n, m;
while( sc.hasNextInt() ){
n = sc.nextInt();
m = sc.nextInt();
if( n == 0 ) break;
for( int i = 0; i < n; i++ ){
clg[ i ] = new Vge();
clg[ i ].u = sc.nextInt();
clg[ i ].v = sc.nextInt();
clg[ i ].w = sc.nextInt();
}
mst( m );
int ans = ksu( n, m );
if( ans == -1 ) System.out.println( "?" );
else System.out.println( ans );
}
sc.close();
}
public static void mst( int n ){
for( int i = 0; i <= n; i++ ){
pre[i] = i;
}
}
public static int find( int x ){
return x == pre[ x ] ? x : ( pre[ x ] = find( pre[ x ] ) );
}
public static int ksu( int n, int m ){
Arrays.sort( clg, 0, n, new mycmp() );
int cnt = 0;
int ans = 0;
for( int i = 0; i < n; i++ ){
int fu = find( clg[ i ].u );
int fv = find( clg[ i ].v );
if( fu != fv ){
ans += clg[i].w;
cnt ++;
pre [ fv ] = fu;
}
if( cnt == m - 1 ) return ans;
}
return -1;
}
}