С Delphi на С++
28.04.2016, 16:59. Показов 406. Ответов 2
Всем привет!
В общем, нужно перенести с дефли, код на с++, я вроде все сделал, все учел, но почему-то не выводится результат.
Задание "нахождение максимального потока в сети" если кому поможет.
Вот код c++:
| C++ | 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Grids.hpp"
#include "Unit2.h"
#include "Unit3.h"
#include "System.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
const MaxV=1000; const MaxE=30000;
const free_ = 0;
const bisy_ = 1;
const Great=MaxLongint;
int a[51][3];
int i;
long int n,m,last,s,t,x,y,z;
long int v[MaxV], l[MaxV];
long int adj[MaxE],next[MaxE],c[MaxE],f [MaxE];
bool found;
long int MaxPOTOK;
long int prev [MaxV];
byte Marked [MaxV];
long int que[MaxE], poz [MaxE];
long int qb,qe;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void Init(long int n, long int m)
{
long int i;
for (i=1; i<n; i++)
{v[i]=free_;
l[i]=free_;}
for (i=1; i<(2*m); i++)
{
adj[i] = free_;
c[i] = free_;
f[i] = free_;}
last= 0;
}
void AddEdge(long int x, long int y, long int z)
{last++;
adj[last]= y;
c[last]= z;
if (v[x] == free_)
{
v[x]= last;}
else
{ try {
next[l[x]]= last;}
catch(Exception &exception){} ;
l[x]= last; }
}
void ErrorMes()
{
ShowMessage("Неверно введены входные данные!"); exit(1);
}
void Put(long int x)
{
qe++;
que[qe]=x;
Marked[x]=bisy_;
prev[x]=que[qb];
}
void InitQue(long int x)
{
long int i;
for (i=1;i<(2*m); i++)
{
Marked[i]=free_;
poz[i]=0;
}
qb=1;
qe=1;
que[qe]= x;
Marked[x]= bisy_;
}
void FindWay()
{long int x=t, Min=Great, cf;
InitQue(s);
while ((qb<=qe)&&(Marked[t]!=bisy_))
{
x=v[que[qb]];
while (adj[x]!=free_)
{
if ((Marked[adj[x]]!=bisy_)&&(c[x]-f[x]>0))
{
Put(adj[x]);
poz[adj[x]]=x;
}
x=next[x];
}
qb++;
}
if (Marked[t]==free_)
{
found=false;
}
x=t;
Min=Great;
while (prev[x]!=free_)
{
cf=c[poz[x]]-f[poz[x]];
if (cf<Min){ Min=cf;}
x=prev[x];
}
x=t;
while (prev[x]!=free_)
{
f[poz[x]]=f[poz[x]]+Min;
if (c[poz[x]]!=free_ )
{f[poz[x]+1]=-f[poz[x]];}
else {f[poz[x]-1]=-f[poz[x]];}
x=prev[x];
}
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{ int i;
StringGrid1->Cells[0][0]="Ребор";
StringGrid1->Cells[1][0]="Начало";
StringGrid1->Cells[2][0]="Конец";
StringGrid1->Cells[3][0]="Вес";
for (i=1; i<(StringGrid1->RowCount); ++i)
{ StringGrid1->Cells[0][i]=IntToStr(i); }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
n= StrToIntDef(Edit1->Text, 0);
m= StrToIntDef(Edit2->Text, 0);
if ((n == 0) || (m == 0)){ ErrorMes();}
Init(n,m);
for (i=1; i<m; i++)
{
x= StrToIntDef(StringGrid1->Cells[1][i], 0);
y= StrToIntDef(StringGrid1->Cells[2][i], 0);
z= StrToIntDef(StringGrid1->Cells[3][i], 0);
if ((x <= 0) || (y <= 0) ||(z <= 0)) ErrorMes();
AddEdge(x,y,z);
AddEdge(y,x,0);
}
s= StrToIntDef(Edit3->Text, 0);
t= StrToIntDef(Edit4->Text, 0);
if ((s > n)|| (s <= 0) || (t > n)|| (t <= 0)) {ErrorMes();
}
found=true;
while (found) { FindWay(); }
MaxPOTOK= 0;
x = v[s];
while (x!= free_)
{
if (f[x] > 0)
{MaxPOTOK= MaxPOTOK + f[x]; }
x=next[x];}
Memo1->Lines->Clear();
Memo1->Lines->Add("Максимальный поток в заданном графе равен:"+ IntToStr(MaxPOTOK));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2Change(TObject *Sender)
{int i;
long int m; m= StrToIntDef(Edit2->Text, 0);
if (m == 0)
{
ShowMessage("Неверно введены входные данные!"); exit(1);}
StringGrid1->RowCount=m+1;
for (i=1; i<(StringGrid1->RowCount); i++)
{StringGrid1->Cells[0][i]=IntToStr(i);
StringGrid1->RowCount=m+1;}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
Form3->Close();
}
//--------------------------------------------------------------------------- |
|
Вод код Delphi:
| Delphi | 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, Buttons, ExtCtrls, XPMan;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Memo1: TMemo;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
XPManifest1: TXPManifest;
Label5: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit2Exit(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
MaxV=1000;
MaxE=30000;
free_ = 0;
bisy_ = 1;
Great=MaxLongint;
var
// Form1: TForm1;
a:ARRAY[1..50,1..2]of integer;
procedure TForm1.FormCreate(Sender: TObject);
var i: integer;
begin
StringGrid1.Cells[0, 0] := 'Ребро';
StringGrid1.Cells[1, 0] := 'Начало';
StringGrid1.Cells[2, 0] := 'Конец';
StringGrid1.Cells[3, 0] := 'Вес';
for I := 1 to Pred(StringGrid1.RowCount) do
StringGrid1.Cells[0, i] := IntToStr(i);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
n, m, last, s, t, x, y, z: longint;
v,l: array[1..MaxV] of longint;
adj, next, c, f: array[1..MaxE] of longint;
found: boolean;
MaxPOTOK: longint;
prev: array [1..MaxV] of longint;
Marked: array [1..MaxV] of byte;
que,poz: array [1..MaxE] of longint;
qb,qe: longint;
procedure Init(n,m: longint);
var
i: longint;
begin
for i:=1 to n do
begin
v[i] := free_;
l[i] := free_;
end;
for i:=1 to 2*m do
begin
adj[i] := free_;
c[i] := free_;
next[i] := free_;
f[i] := free_;
end;
last := 0;
end;
procedure AddEdge(x,y,z: longint);
begin
inc(last);
adj[last] := y;
c[last] := z;
if v[x] = free_ then
begin
v[x] := last;
end else
try begin
next[l[x]] := last;
end;except end;
l[x] := last;
end;
procedure ErrorMes;
begin
ShowMessage('Неверно введены входные данные!');
exit;
end;
procedure Put(x: longint);
begin
inc(qe);
que[qe]:=x;
Marked[x]:=bisy_;
prev[x]:=que[qb];
end;
procedure InitQue(x: longint);
var
i: longint;
begin
for i:=1 to 2*m do
begin
Marked[i]:=free_;
poz[i]:=0;
end;
qb:=1;
qe:=1;
que[qe] := x;
Marked[x] := bisy_;
end;
procedure FindWay;
var
x,Min,cf: longint;
begin
InitQue(s);
while (qb<=qe)and(Marked[t]<>bisy_) do
begin
x:=v[que[qb]];
while adj[x]<>free_ do
begin
if (Marked[adj[x]]<>bisy_)and(c[x]-f[x]>0) then
begin
Put(adj[x]);
poz[adj[x]]:=x;
end;
x:=next[x];
end;
inc(qb);
end;
if Marked[t]=free_ Then
begin
Found:=False;
Exit;
end;
Min:=Great;
x:=t;
while prev[x]<>free_ do
begin
cf:=c[poz[x]]-f[poz[x]]{!};
if cf<Min Then Min:=cf;
x:=prev[x];
end;
x:=t;
while prev[x]<>free_ do
begin
f[poz[x]]:=f[poz[x]]+Min;
if c[poz[x]]<>free_ then
f[poz[x]+1]:=-f[poz[x]]
else f[poz[x]-1]:=-f[poz[x]];
x:=prev[x];
end;
end;
begin
//Ввод данных
//Количество вершин
n := StrToIntDef(Edit1.Text, 0);
m := StrToIntDef(Edit2.Text, 0);
if (n = 0) or (m = 0) then ErrorMes;
Init(n,m);
for i := 1 to m do
beginччччччччччччч
x := StrToIntDef(StringGrid1.Cells[1, i], 0);
y := StrToIntDef(StringGrid1.Cells[2, i], 0);
z := StrToIntDef(StringGrid1.Cells[3, i], 0);
if (x <= 0) or (y <= 0) or (z <= 0) then ErrorMes;
AddEdge(x,y,z);
AddEdge(y,x,0);
end;
//Сток и исток (начальная и конечная вершины)
s := StrToIntDef(Edit3.Text, 0);
t := StrToIntDef(Edit4.Text, 0);
if (s > n) or (s <= 0) or (t > n) or (t <= 0) then ErrorMes;
//Вычисление по алгоритму Форда-Фалкерсона
found:=true;
while found do FindWay;
MaxPOTOK := 0;
x := v[s];
while x <> free_ do
begin
if f[x] > 0 then
MaxPOTOK := MaxPotok + f[x];
x:=next[x];
end;
Memo1.Lines.Clear;
Memo1.Lines.Add('Максимальный поток в заданном графе равен: '+ IntToStr(MaxPotok))
end;
procedure TForm1.Edit2Exit(Sender: TObject);
var i: integer;
m: longint;
begin
m := StrToIntDef(Edit2.Text, 0);
if m = 0 then
begin
ShowMessage('Неверно введены входные данные!');
exit
end;
StringGrid1.RowCount := m + 1;
for I := 1 to Pred(StringGrid1.RowCount) do
StringGrid1.Cells[0, i] := IntToStr(i);
end;
end. |
|
Помогите понять, в чем проблема, может я глупый.
Заранее спасибо!
P.S. Извиняюсь что не "красиво" написано, в плане расставления скобок и прочего, надеюсь вы поймете
0
|