c++实习题

来源:百度知道 编辑:UC知道 时间:2024/07/02 08:21:13
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:
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 number P

下面的代码,已经确认过了,完全符合要求:
#include<iostream>
#define M 60

using namespace std;

int mar[M][M];
int egde[M];
bool visit[M];
int sum,p,r;

void prim()
{
int i,j;
int c=0;
int Q=0;

for(i=1;i<=p;++i)
{
if(mar[1][i]!=0)
egde[i]=mar[1][i];
else
egde[i]=999;
}
visit[1]=true;

for(i=1;i<p;++i)
{
int min=999;
for(j=1;j<=p;++j)
{
if(!visit[j]&&egde[j]<min)
{
c=j;
min=egde[j];
}
}
visit[c]=true;
Q+=min;

for(j=1;j<=p;++j)
{
if(!visit[j]&&egde[j]>mar[c][j]&&mar[c][j]!=0)
{
egde[j]=mar[c][j];
}
}
}
cout<<Q<<endl;
}

void input()
{
while(cin>>p>>r&&p!=0)
{
memset(mar,0,sizeof(mar));
memset(visit,false,si