-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathBellmanFord.cpp
More file actions
86 lines (84 loc) · 1.72 KB
/
BellmanFord.cpp
File metadata and controls
86 lines (84 loc) · 1.72 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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int mindist(vector<bool>vis,vector<int>dist,int n)
{
int min=INT_MAX,node;
for(int i=0;i<n;i++)
{
if(!vis[i] && dist[i]<min)
{
min=dist[i];
node=i;
}
}
return node;
}
void bellman(vector<vector<int> >v,int n,int src)
{
vector<int>dist(n,INT_MAX);
vector<int>parent(n,-1);
vector<bool>vis(n,false);
dist[src]=0;
for(int i=0;i<n;i++)
{
int u=mindist(vis,dist,n);
vis[u]=true;
for(int j=0;j<n;j++)
{
if(v[u][j] && (dist[u]+v[u][j])<dist[j])
{
dist[j]=dist[u]+v[u][j];
parent[j]=u;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(dist[j]!=INT_MAX && v[i][j] && vis[j] && dist[i]+v[i][j]<dist[j])
{
cout<<"negative cycle exists at vertices between "<<i+1<<"and"<<j+1;
}
}
}
for(int i=0;i<n;i++)
{
if(i==src)
{
cout<<i+1<<":"<<dist[i]<<endl;
continue;
}
cout<<i+1<<" ";
int j=i;
while(parent[j]!=src)
{
cout<<parent[j]+1<<" ";
j=parent[j];
}
cout<<src+1<<":"<<dist[i]<<endl;
}
}
int main()
{
int n;
cin>>n;
vector<vector<int> > v;
//int v[n][n+1];
for(int i=0;i<n;i++)
{
vector<int>temp;
for(int j=0;j<n;j++)
{
int t;
cin>>t;
temp.push_back(t);
//cin>>v[i][j];
}
v.push_back(temp);
}
int src;
cin>>src;
bellman(v,n,src-1);
}