W15 最小生成树(Prim)

Description
输入一个带权连通图,输出最小生成树

Input
输入包含如下内容:
第一行两个整型数N和M,分别表示结点的个数以及带权边的个数
第二行输入N个数,分别表示N个结点
第三行开始,一共输入M行,每行有三个整型数,分别表示边的两端结点以及权值;

Output
输出包含两个部分:
1、输出若干行,每行输出三个数 a,b,c(相邻两数中间空一个空格),分别表示最小生成树中边的两端结点以及权值,需要注意的是:
(1)请按照权值的顺序按行依次输出;
(2)每行前两个值a和b(边的两端结点)要满足a<=b。
2、最后一行输出一个整型数,表示最小生成树所有权值和,末尾不要留换行符。

邻接矩阵+Prim算法

#include<iostream>
using namespace std;
#define Infinity 65535
#define ERROR -1

typedef struct GNode
{
	int Nv;
	int Ne;
	int G[100][100];
	int Data[100];
} MGraph;

typedef struct ENode
{
	int V1,V2;
	int Weight;
} Edge;

MGraph* CreateGraph(int VertexNum)
{
	int V,W;
	MGraph* Graph;
	
	Graph=new MGraph;
	Graph->Nv=VertexNum;
	Graph->Ne=0;
	
	for (V=0;V<Graph->Nv;V++)
	{
		for (W=0;W<Graph->Nv;W++)
		{
			Graph->G[V][W]=Infinity;
		}
	}
	
	return Graph;
}

void InsertEdge(MGraph* Graph,Edge* E)
{
	Graph->G[E->V1-Graph->Data[0]][E->V2-Graph->Data[0]]=E->Weight;
	Graph->G[E->V2-Graph->Data[0]][E->V1-Graph->Data[0]]=E->Weight; 
}

MGraph* BuildGraph(MGraph* Graph)
{
	Edge* E;
	int V;
	int Nv,Ne,i;
	
	cin>>Nv>>Ne;
	Graph=CreateGraph(Nv);
	Graph->Ne=Ne;
	
	for (V=0;V<Nv;V++)
	{
		cin>>Graph->Data[V];
	}
	
	if (Graph->Ne!=0)
	{
		E=new Edge;
		for (i=0;i<Graph->Ne;i++)
		{
			cin>>E->V1>>E->V2>>E->Weight;
			InsertEdge(Graph,E);
		}
	}
	
	return Graph;
}

int FindMin(MGraph* Graph,int dist[])
{
	int MinV,V;
	int MinDist=Infinity;
	
	for (V=0;V<Graph->Nv;V++)
	{
		if (dist[V]!=0&&dist[V]<MinDist)
		{
			MinDist=dist[V];
			MinV=V;
		}
	}
	
	if (MinDist<Infinity)
	{
		return MinV;
	}
	else
	{
		return ERROR;
	}
}

void Sort(int a[],int b[],int c[],int k,MGraph* Graph)
{
	int i,j,t;
	for (i=1;i<k;i++)
	{
		for (j=0;j<k-i;j++)
		{
			if (a[j]>a[j+1])
			{
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
				t=b[j];
				b[j]=b[j+1];
				b[j+1]=t;
				t=c[j];
				c[j]=c[j+1];
				c[j+1]=t;
			}
		}
	}
	
	for (i=0;i<k;i++)
	{
		if (b[i]<=c[i])
		{
			cout<<b[i]+Graph->Data[0]<<" "<<c[i]+Graph->Data[0]<<" "<<a[i]<<endl;
		}
		else
		{
			cout<<c[i]+Graph->Data[0]<<" "<<b[i]+Graph->Data[0]<<" "<<a[i]<<endl;
		}
	}
}

int Prim(MGraph* Graph,MGraph* MST)
{
	int k=0;
	int a[100],b[100],c[100];
	int dist[100],TotalWeight;
	int parent[100],V,W;
	int VCount;
	Edge* E;
	
	for (V=0;V<Graph->Nv;V++)
	{
		dist[V]=Graph->G[0][V];
		parent[V]=0;
	}
	TotalWeight=0;
	VCount=0;
	
	MST=CreateGraph(Graph->Nv);
	E=new Edge;
	
	dist[0]=0;
	VCount++;
	parent[0]=-1;
	
	while(1)
	{
		V=FindMin(Graph,dist);
		if (V==ERROR)
		{
			break;
		}
		E->V1=parent[V];
		E->V2=V;
		E->Weight=dist[V];
		InsertEdge(MST,E);
		TotalWeight+=dist[V];
		dist[V]=0;
		VCount++;
		
		a[k]=E->Weight;
		b[k]=E->V1;
		c[k]=E->V2;
		k++;
		
		for (W=0;W<Graph->Nv;W++)
		{
			if (dist[W]!=0&&Graph->G[V][W]<Infinity)
			{
				if (Graph->G[V][W]<dist[W])
				{
					dist[W]=Graph->G[V][W];
					parent[W]=V;
				}
			}
		} 
	}
	
	if (VCount<Graph->Nv)
	{
		TotalWeight=ERROR; 
	}
	
	Sort(a,b,c,k,Graph);
	
	return TotalWeight;
}

int main()
{
	MGraph* Graph;
	MGraph* MST;
	int Total;
	
	Graph=BuildGraph(Graph);
	Total=Prim(Graph,MST);
 
	cout<<Total;
	
	return 0;
} 

Input 1

6 10
0 1 2 3 4 5
0 1 6
2 0 1
0 3 5
1 2 5
2 3 5
1 4 3
2 4 6
2 5 4
3 5 2
4 5 6

Output 1

0 2 1
3 5 2
1 4 3
2 5 4
1 2 5
15

Input 2

6 10
0 1 2 3 4 5
0 1 6
0 3 4
1 3 7
1 4 7
1 2 10
3 2 8
3 4 12
2 4 5
2 5 6
4 5 7

Output 2

0 3 4
2 4 5
0 1 6
2 5 6
1 4 7
28

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值