include iostream include time include vector using namespace std void

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
void shirina(bool *pas, int u, int **mat, int n);
int main(){
setlocale(0, "");
int n,start,i,j;
cout << "Введите количество вершин: " ;
cin >> n;
cout << endl;
bool *vis=new bool[n];
srand(time(NULL));
int **mat = new int*[n];
for ( i = 0; i < n ; i ++)
{
mat[i] = new int[n];
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
mat[i][j]=mat[j][i] = rand()%2;
if(mat[i][j]) mat[i][j]=mat[j][i] = rand()%2;
if(mat[i][j]) mat[i][j]=mat[j][i] = rand()%2;
//if(mat[i][j]) mat[i][j]=mat[j][i] = rand()%2;
}mat[i][i]=0;
}
for ( i = 0; i < n ; i++)
{
for ( j = 0; j <n; j++)
{
mat[i][j] = mat[j][i];
mat[i][i] = 0;
}
}
cout<<"Матрица: "<<endl;
for (i=0; i<n; i++){
vis[i]=false;
for (j=0; j<n; j++)
cout << " " << mat[i][j];
cout<<endl;
}
cout<<"Введите стартовую вершину: ";
cin>>start;
// cout<<"Путь: " << endl;
shirina(vis, start, mat, n);
delete []vis;
for ( i = 0; i < n; i++)
{
delete[] mat[i];
}
delete[] mat;
return 0;
}
void shirina(bool *pas, int u, int **mat, int n)
{
vector <int> par;
int i = 0,j = 0;
int *q=new int[n];
int **skelet = (int**)calloc(n, sizeof(int*));
int * d = (int *) calloc(n, sizeof(int));
int count = 0, head = 0;
for (i=0; i<n; i++)
{
skelet[i] = (int*)calloc(n, sizeof(int));
q[i]=0;
}
q[count++]=u;
pas[u]=true;
while (head<count)
{
u=q[head++];
par.push_back(u);
for (i=0; i<n; i++)
if (mat[u][i] && !pas[i])
{
q[count++]=i;
d[i] = d[u] + 1;
skelet[u][i] = skelet[i][u] = 1;
pas[i]=true;
}
}
bool done = false;
int dd = 0;
while (!done)
{
done = true;
cout << " [ ";
for (int i = 0; i<n; ++i)
{
if (d[i] == dd && pas[i])
{
cout << i << ' ';
}
if (d[i] == dd + 1) done = false;
}
cout << " ] ";
if (!done) cout << endl;
++dd;
}
cout << endl;
vector <int> :: iterator r;
cout << "Каркас: \n" ;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << skelet[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
//for ( r = par.begin(); r < par.end(); r++)
/// {
//cout << *r << " ";
//}
cout << endl;
free(d);
delete []q;
}