Помогите пожалуйста перевести код, у самого не получилось, скину исходник С++ и то, что у меня получилось!
Код:
#include "stdafx.h"
#include <iostream>
#define inf 100000
using namespace std;
struct Edges{
int u, v, w;
};
const int Vmax=1000;
const int Emax=Vmax*(Vmax-1)/2;
int i, j, n, e, start;
Edges edge[Emax];
int d[Vmax];
//алгоритм Беллмана-Форда
void bellman_ford(int n, int s)
{
int i, j;
for (i=0; i<n; i++) d[i]=inf;
d[s]=0;
for (i=0; i<n-1; i++)
for (j=0; j<e; j++)
if (d[edge[j].v]+edge[j].w<d[edge[j].u])
d[edge[j].u]=d[edge[j].v]+edge[j].w;
for (i=0; i<n; i++) if (d[i]==inf)
cout<<endl<<start<<"->"<<i+1<<"="<<"Not";
else cout<<endl<<start<<"->"<<i+1<<"="<<d[i];
}
//главная функция
void main()
{
setlocale(LC_ALL, "Rus");
int w;
cout<<"Количество вершин > "; cin>>n;
e=0;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
cout<<"Вес "<<i+1<<"->"<<j+1<<" > "; cin>>w;
if (w!=0)
{
edge[e].v=i;
edge[e].u=j;
edge[e].w=w;
e++;
}
}
cout<<"Стартовая вершина > "; cin>>start;
cout<<"Список кратчайших путей:";
bellman_ford(n, start-1);
system("pause>>void");
}
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
const
inf=100000;
Vmax=1000;
Emax=Vmax*(Vmax-1) div 2;
type Edges=record
u, v, w: integer;
end;
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
ListBox1: TListBox;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1; e, n, w, start: integer;
edge: array[1..Emax] of Edges;
d: array[1..Vmax] of integer;
implementation
{$R *.dfm}
{Àëãîðèòì Ôîðäà-Áåëëìàíà}
procedure FB(n, s: integer);
var ListBox1: TListBox;
i,j:integer;
begin
for i:=1 to n do
d[i]:=inf;
d[s]:=0;
for i:=1 to n-1 do
for j:=1 to e-1 do
if d[edge[j].v]+edge[j].w<d[edge[j].u] then
d[edge[j].u]:=d[edge[j].v]+edge[j].w;
for i:=1 to n do
if d[i]=inf then
listbox1.items.Add(inttostr(start)+'->'+inttostr(i)+'= Not')
else listbox1.items.Add(inttostr(start)+'->'+ inttostr(i)+'='+inttostr(d[i]));
end;
procedure TForm1.Button1Click(Sender: TObject);
var i,j:byte;
{Îñíîâíîé Áëîê ïðîãðàììû}
begin
n:=strtoint(edit1.Text);
e:=1;
for i:=1 to n do
for j:=1 to n do
begin
w:=strtoint(stringgrid1.Cells[i,j]);
Listbox1.Items.Add('Âåñ '+ inttostr(i)+'=>'+inttostr(j)+'='+);
if w<>0 then
begin
edge[e].v:=i;
edge[e].u:=j;
edge[e].w:=w;
e:=e+1;
end;
end;
start:=strtoint(edit2.Text);
listbox1.Items.Add('Ñïèñîê êðàò÷àéøèõ ïóòåé: ');
FB(n, start);
end;
end.