-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpairchecker.cpp
More file actions
126 lines (117 loc) · 4.04 KB
/
Copy pathpairchecker.cpp
File metadata and controls
126 lines (117 loc) · 4.04 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <bits/stdc++.h>
using namespace std;
#define lli long long
#define bM 100000000
struct node
{
double val;
bool isInList;
int index;
};
void findVeqn(vector<pair<node, node>> totEquations, int n, double *b, node *x)
{
int iter = 0, indx;
//we know that x[0] = 0, after that, we have to find out x[i], for i = 1,2,..., n
while (iter++ < 10)
{
for (int i = 0; i < n; i++)
{
if (totEquations[i].first.isInList)
{
totEquations[i].second.val = b[i] - totEquations[i].first.val;
totEquations[i].second.isInList = true;
indx = totEquations[i].second.index;
x[indx - 1].val = totEquations[i].second.val;
for (int j = 0; j < n; j++)
{
if (indx == totEquations[j].first.index)
{
totEquations[j].first.val = totEquations[i].second.val;
totEquations[j].first.isInList = true;
x[indx - 1].val = totEquations[i].second.val;
}
else if (indx == totEquations[j].second.index)
{
totEquations[j].second.val = totEquations[i].second.val;
totEquations[j].second.isInList = true;
x[indx - 1].val = totEquations[i].second.val;
}
}
}
else if (totEquations[i].second.isInList)
{
totEquations[i].first.val = b[i] - totEquations[i].second.val;
totEquations[i].first.isInList = true;
indx = totEquations[i].first.index;
x[indx -1].val = totEquations[i].first.val;
for (int j = 0; j < n; j++)
{
if (indx == totEquations[j].first.index)
{
totEquations[j].first.val = totEquations[i].first.val;
totEquations[j].first.isInList = true;
x[indx -1].val = totEquations[i].first.val;
}
else if (indx == totEquations[j].second.index)
{
totEquations[j].second.val = totEquations[i].first.val;
totEquations[j].second.isInList = true;
x[indx -1].val = totEquations[i].first.val;
}
}
}
}
}
for (int i = 0; i < n; i++)
{
cout<<"x_"<<i<<" = "<<x[i].val<<"\n";
if (totEquations[i].first.isInList && totEquations[i].second.isInList)
cout << "x_" << totEquations[i].first.index << " + x_" << totEquations[i].second.index << " = " << totEquations[i].first.val << " + " << totEquations[i].second.val << "\n";
}
}
int main()
{
node *x = new node[6];
for (int i = 0; i < 6; i++)
{
x[i].isInList = false;
x[i].val = 0.0;
}
pair<node, node> eqn;
vector<pair<node, node>> totEquations;
eqn.first.index = 0;
eqn.first.isInList = true;
eqn.first.val = 0.0;
eqn.second.index = 3;
eqn.second.isInList = false;
totEquations.push_back(eqn);
eqn.first.index = 0;
eqn.first.isInList = true;
eqn.first.val = 0.0;
eqn.second.index = 4;
eqn.second.isInList = false;
totEquations.push_back(eqn);
eqn.first.index = 1;
eqn.first.isInList = false;
eqn.second.index = 4;
eqn.second.isInList = false;
totEquations.push_back(eqn);
eqn.first.index = 1;
eqn.first.isInList = false;
eqn.second.index = 6;
eqn.second.isInList = false;
totEquations.push_back(eqn);
eqn.first.index = 2;
eqn.first.isInList = false;
eqn.second.index = 5;
eqn.second.isInList = false;
totEquations.push_back(eqn);
eqn.first.index = 2;
eqn.first.isInList = false;
eqn.second.index = 6;
eqn.second.isInList = false;
totEquations.push_back(eqn);
double b[] = {10, 5, 2, 6, 4, 8};
findVeqn(totEquations, 6, b, x);
return 0;
}