Distance Vector Routing Algorithm (implemented by Bellman-Ford)
Source: Internet
Author: User
Using Namespace STD;
Const Int Maxn = 100 ;
Const Int Maxv = Maxn * Maxn;
Const Int INF = 2000000000 ;
Struct Edge
{
IntU, V;
} ;
Int G [maxn] [maxn];
Edge e [maxv];
Int Bellmanford ( Int Beg, Int End, Int Nnum, Int Enum)
{ // Nnum is the number of vertices, Enum is the number of edges, and complexity O (VE)
Int D [maxn];
Int I, K;
For (I = 0 ; I < Nnum; I ++ )
D [I] = INF;
D [beg] = 0 ;
Bool Ex = True ;
For (K = 1 ; K < Nnum && Ex; k ++ )
{
Ex = False ;
For (I = 0 ; I < Enum; I ++ )
If (D [E [I]. u] < INF && D [E [I]. V] > D [E [I]. u] + G [E [I]. u] [E [I]. V])
{
D [E [I]. V]=D [E [I]. u]+G [E [I]. u] [E [I]. V];
Ex=True;
}
}
Return D [end];
}
Int Main ()
{
Int I, J;
Int T = 0 ;
Int Enum = 0 ;
Int Nnum = 9 ;
For (I = 0 ; I < 4 ; I ++ )
For (J = 0 ; J < 4 ; J ++ )
{
If (I = J)
{
G [I] [J]=INF;
}
Else
{
G [I] [J] = ++ T;
E [Enum]. u = I;
E [Enum]. v = J;
Enum ++ ;
}
}
Cout < Bellmanford ( 2 , 3 , Nnum, Enum) < Endl;
Return 0 ;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service