求编程高手指教!一道有关数据结构的编程题,最好用c语言编。

图G有30个顶点,为v[1]…v[99],其中边i→j的权值用随机整数产生,随机数分布区间为[1,100],请利用Kruscal算法产生最小生成树G1,在屏幕上输出:(1... 图G有30个顶点,为v[1] … v[99],其中边i→j的权值用随机整数产生,随机数分布区间为[1,100],请利用Kruscal算法产生最小生成树G1,在屏幕上输出: (1)各条边的权值w[i][j] (2)G1中所有的边u[a][b]及总权重屏幕上输出可以用截图来表示。 展开
 我来答
菜刀撒
2014-01-03 · TA获得超过313个赞
知道小有建树答主
回答量:487
采纳率:0%
帮助的人:273万
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
#define MAXNUM_VERTEX 30 //最多有 30个顶点
#define MAXNUM_EDGE 31
typedef struct
{
int v,w;
int weight;
}Edge;
typedef struct
{
int vertex[MAXNUM_VERTEX];
Edge edge[MAXNUM_EDGE];
int num_vertex,num_edge;
}Graph;
Graph graph; //声明为全局变量
bool search_vertex(int ver)
{
for(int i=0; i<graph.num_vertex; i++)
if( ver == graph.vertex[i] )
return 1;
printf("the vertex %d you input is not existed! \n",ver);
return 0;
}
void create_graph()
{
//输入顶点信息
printf("input the number of vertex in the graph\n");
scanf(" %d",&graph.num_vertex);
printf("input the vertex of graph,like 1,2\n");
for(int i=0; i<graph.num_vertex; i++)
scanf(" %d",&graph.vertex[i]);
//输入边信息
printf("input the number of edge in the graph\n");
scanf(" %d",&graph.num_edge);
printf("intput the edge of graph and the weight of line,like 1-2 5\n");
for(int j=0; j<graph.num_edge; j++)
{
p1:int a,c,d;
char b;
scanf(" %d %c %d %d",&a,&b,&c,&d);
if( search_vertex(a) && search_vertex(c) )
{
graph.edge[j].v=a;
graph.edge[j].w=c;
graph.edge[j].weight=d;
}
else
goto p1;
}
}
void sort_by_weight()
{
for(int i=1; i<graph.num_edge; i++)
{
Edge temp=graph.edge[i];
for(int j=i-1; j>=0 && graph.edge[j].weight>temp.weight; j--)
graph.edge[j+1]=graph.edge[j];
graph.edge[j+1]=temp;
}
}
/*不相交集处理函数*/
inline void makeset(vector<int> & array)
{
for(int i=0; i<array.size(); i++)
array[i]=i;
}
int findset(vector<int> & parent,int i)
{
if(i != parent[i])
i=parent[i];
return i;
}
inline void merge(vector<int> & parent,int i,int j)
{
parent[i]=j;
}
/*不相交集处理函数*/
void kruskal()
{
int num_ver=graph.num_vertex;
int count=0;
vector<int> parent_ver;
parent_ver.resize(num_ver);
/*核心部分是用不相交集合成树*/
makeset(parent_ver);
printf("the edge of min tree as follow: \n");
for( int i=0; i<num_ver && count<num_ver-1; i++ ) //count表示合并(merge)的次数num_ver-1次
{
int m=graph.edge[i].v;
int n=graph.edge[i].w;
int w=graph.edge[i].weight;
if( findset(parent_ver,m) != findset(parent_ver,n)) //当属于不同的集合时,则将该边添加进树中
{
printf("(%d %d) %d\n",m,n,w);
merge(parent_ver,m,n);
count++;
}
}
}
void print_edge()
{
printf("the graph you input as follow: \n");
for(int i=0; i<graph.num_edge; i++)
printf("%d-%d the weight is: %d\n",graph.edge[i].v,graph.edge[i].w,graph.edge[i].weight);
}
void main()
{
create_graph();
sort_by_weight();
print_edge();
kruskal();
}
追问
这里面有第二问么?
可不可以有其他的方法~~~
满意的追加~~财富不是事~
上海勤革
2024-10-18 广告
这里小编推荐一款新的IT在线编程与面试题库平台:“超级码客”,超级码客是聚焦于各级别软件开发工程师,运维,测试等技术人员,更加侧重于实战面试考题与在线测试,提供海量面试题八股理论分析,辅助机考笔试,可以说是更加适合于面试求职路上的所有IT技... 点击进入详情页
本回答由上海勤革提供
甫皖然0K
2014-01-03 · 超过22用户采纳过TA的回答
知道答主
回答量:127
采纳率:100%
帮助的人:69.4万
展开全部
v[1] … v[99]只有30个顶点。。。。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式