-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm_with_input.cpp
More file actions
112 lines (96 loc) · 2.02 KB
/
algorithm_with_input.cpp
File metadata and controls
112 lines (96 loc) · 2.02 KB
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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
const int Inf = 1000000000;
void graf(){
int n = 0;
cin >> n;
ofstream f;
f.open("test.txt");
if (f.is_open()){
f << n << endl;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (i!=j){
f << i << " " << j << " " << (rand()%2000+1) - 1000 << endl;
}
else{
f << i << " " << j << " " << Inf << endl;
}
}
}
}
}
vector<vector<int>> input(int *n);
int main() {
int n;
int i, j, k = 0;
vector < vector <int> > a(n, vector <int> (n, Inf) );
vector < vector <int> > b(n, vector <int> (n, Inf) );
graf();
a = input(&n);
b = a;
a[0][0] = 0;
for (i = 0; i < n; ++i){
for (j = 0; j < n; ++j){
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (int k = 1; k < n; ++k)
{
for (int i = 0; i < n; ++i)
{
a[k][i] = a[k - 1][i];
for (int j = 0; j < n; ++j){
if ((a[k - 1][j] + b[j][i] < a[k][i]) && (b[j][i] != Inf) && (a[k - 1][j] != Inf)){
a[k][i] = a[k - 1][j] + b[j][i];
}
}
}
}
for (i = 0; i < n; ++i){
for (j = 0; j < n; ++j){
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (j = 0; j < n; ++j){
cout << "До " << j << " вершины min растояние " << a[n-1][j] << endl;
}
return 0;
}
vector<vector<int>> input(int *n){
fstream file ("test.txt");
char name[]("test.txt");
try
{
if (!file.is_open())
{
throw name;
}
else {
string s,s1,s2,s3;
getline(file,s);
*n = stoi(s);
vector < vector <int> > a(*n, vector <int> (*n, Inf));
while(!file.eof())
{
file >> s1;
file >> s2;
file >> s3;
a[stoi(s1)-1][stoi(s2)-1] = stoi(s3);
}
return(a);
}
}
catch(char*name)
{
cout<<"Невозможно открыть файл "<<name;exit(1);
}
file.close();
}