c语言题目 给出完整代码
发布网友
发布时间:2023-04-25 18:26
我来回答
共1个回答
热心网友
时间:2023-10-16 09:05
#include <stdio.h>
#include <stdlib.h>
typedef struct point
{
int data;
struct point *next;
} point;
typedef struct node
{
int weight;
point *next;
} node;
int main(void)
{
int length,degree,sum=0,low,anti;
point *newNode,*temp;
scanf("%d %d",&length,°ree);
node *record=(node *)malloc(length*sizeof(node));
for(int k=0;k<length;k++)
{
scanf("%d",&(record[k].weight));
record[k].next=NULL;
}
for(int j=0,m,n;j<degree;j++)
{
scanf("%d %d",&m,&n);
newNode=(point *)malloc(sizeof(point));
newNode->data=n-1,newNode->next=record[m-1].next;
record[m-1].next=newNode;
newNode=(point *)malloc(sizeof(point));
newNode->data=m-1,newNode->next=record[n-1].next;
record[n-1].next=newNode;
}
for(int h=0;h<length-1;h++)
{
low=0;
for(int y=1;y<length;y++)
{
if(record[y].next==NULL)
continue;
else if(record[y].next->next==NULL&&record[low].weight>=record[y].weight)
low=y;
}
sum+=record[low].weight;
anti=record[low].next->data;
newNode=record[record[low].next->data].next;
if(newNode->data==low)
{
record[record[low].next->data].next=newNode->next;
free(newNode);
}
else
{
while(newNode->next!=NULL)
{
if(newNode->next->data==low)
{
temp=newNode->next;
newNode->next=temp->next;
free(temp);
break;
}
newNode=newNode->next;
}
}
free(record[low].next);
record[low].next=NULL;
}
printf("%d\n",sum);
free(record);
return 0;
}