C++实习题 高手来

来源:百度知道 编辑:UC知道 时间:2024/05/14 10:08:46
You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input Format: (b4.in)
The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the nu

O good

# include<iostream>
# include<cstdio>
# include<cstring>

using namespace std;
const int inf=0x3f3f3f3f;
int a[52][52],len,vis[55],p;
int prim();

int main()
{
int r,i,j,temp;
while(1)
{
scanf("%d",&p);
if(p==0)
break;
scanf("%d",&r);

for(i=1;i<=p;i++)
for(j=1;j<=p;j++)
a[i][j]=inf;
memset(vis,0,sizeof(vis));
len=0;

while(r--)
{
scanf(" %d %d %d",&i,&j,&temp);
if(temp<a[i][j])
a[i][j]=a[j][i]=temp;
}
vis[1]=1;
printf("%d\n",prim());
}
return 0;

}

int prim()
{
int i,j,k,j_m,temp,ans;
for(k=1;k<p;k++)