若要在n个城市之间建设通信网络,只需要架设n-1条线路即可。如何以最低的经济待价建设这个通信网,是一个网的最小生成树问题。
#include<stdio.h>
struct Edge
{
char v1;
char v2;
int length;
};
struct Headtype
{
Edge space[20];
int Elength;
};
struct Dgree
{
char v;
int flag;
};
struct Mark
{
Dgree biao[20];
int Glength;
};
void InitMark(Mark &M,int x)
{
int j;
M.Glength=x;
printf("Please input these nodes:\n");
for(j=1;j<=x;j++)
{
scanf("%s",&M.biao[j].v);
M.biao[j].flag=-1;
}
}
void InitHeadType(Headtype &H,int y)
{
int k;
H.Elength=y;
for(k=1;k<=y;k++)
{
printf("Please input relation between nodes and edges:\n");
scanf("%s%s%d",&H.space[k].v1,&H.space[k].v2,&H.space[k].length);
printf("%c->%c:%d\n",H.space[k].v1,H.space[k].v2,H.space[k].length);
}
}
void HeadAdjust(Headtype &H,int s,int m)
{
Edge rc;
int p;
rc=H.space[s];
for(p=2*s;p<=m;p*=2)
{
if(p<m&&(H.space[p].length<H.space[p+1].length))
++p;
if(rc.length>H.space[p].length)
break;
H.space[s]=H.space[p];
s=p;
}
H.space[s]=rc;
}
void HeadSort(Headtype &H)
{
int q;
Edge rb;
for(q=H.Elength/2;q>0;--q)
HeadAdjust(H,q,H.Elength);
for(q=H.Elength;q>1;--q)
{
rb=H.space[1];
H.space[1]=H.space[q];
H.space[q]=rb;
HeadAdjust(H,1,q-1);
}
}
void main()
{
int Gnumber,Enumber,a,b,c;
printf("Please input nodes' number:\n");
scanf("%d",&Gnumber);
Enumber=Gnumber*(Gnumber-1)/2;
Headtype H;
Mark M;
Dgree e1,e2;
InitMark(M,Gnumber);
InitHeadType(H,Enumber);
HeadSort(H);
printf("The mintree is;\n");
for(a=1;a<=Enumber;a++)
{
for(b=1;b<=Gnumber;b++)
{
if(M.biao[b].v==H.space[a].v1)
{
e1=M.biao[b];
break;
}
}
for(c=1;c<=Gnumber;c++)
{
if(M.biao[c].v==H.space[a].v2)
{
e2=M.biao[c];
break;
}
}
while(e1.flag!=-1)
{
b=e1.flag;
e1=M.biao[e1.flag];
}
while(e2.flag!=-1)
{
c=e2.flag;
e2=M.biao[e2.flag];
}
if(e2.v!=e1.v)
{
M.biao[b].flag=c;
printf("(%c,%c):%d\n",H.space[a].v1,H.space[a].v2,H.space[a].length);
}
}
}