-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdijstras.cpp
More file actions
74 lines (66 loc) · 1.16 KB
/
dijstras.cpp
File metadata and controls
74 lines (66 loc) · 1.16 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
/*
* Name :sharad Dixit
* Institute :IIIT -A
*/
#include<iostream>
using namespace std;
int shortest ( int, int);
int cost[10][10]={0},dist[20],i,j,n,k,m,S[20],v,totcost,path[20],p;
int main()
{
int c;
cout <<"enter no of vertices";
cin >> n;
cout <<"enter no of edges";
cin >>m;
cout <<"\nenter\nEDGE Cost\n";
for ( k = 1; k <= m; k++)
{
cin >> i >> j >>c;
cost[i][j] = c;
}
for( i = 1; i <= n; i++)
for( j = 1; j <= n; j++)
if ( cost[i][j] == 0 )
cost[i][j]=99999;
cout << "enter initial vertex";
cin >> v;
cout << v << "\n";
shortest(v,n);
}
int shortest(int v,int n)
{
int min;
for(i = 1; i <= n; i++)
{
S[i] = 0;
dist[i] = cost[v][i];
}
path[++p] = v;
S[v] = 1;
dist[v] = 0;
for(i=2; i<=n-1; i++)
{
k = -1;
min = 999999;
for( j = 1; j <= n; j++)
{
if( dist[j] < min && S[j] != 1)
{
min = dist[j];
k = j;
}
}
if ( cost[v][k] <= dist[k] )
p = 1;
path[++p] = k;
for( j=1 ; j <= p; j++)
cout << path[j];
cout << "\n";
//cout <<k;
S[k] = 1;
for ( j = 1; j <= n; j++)
if ( cost[k][j] != 999999 && dist[j] >= dist[k] + cost[k][j] && S[j] != 1)
dist[j] = dist[k] + cost[k][j];
}
}