-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtravelling.cpp
More file actions
63 lines (54 loc) · 1.39 KB
/
travelling.cpp
File metadata and controls
63 lines (54 loc) · 1.39 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
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int getmin(int a[],int n){
int min = INT_MAX,minindex;
for(int i=0;i<n;i++){
if(min>a[i]){
min = a[i];
minindex = i;
}
}
return minindex;
}
bool cover(bool p[],int n){
for(int i=0;i<n;i++){
if(p[i]==false)
return p[i];
}
return true;
}
int main()
{
int n;
cout<<"Enetr no of vertices ";
cin>>n;
int graph[n][n];
bool present[n];
for(int i =0;i<n;i++)
present[i] =false;
for(int i=0;i<n;i++){
graph[i][i] = INT_MAX;
for(int j=i+1;j<n;j++){
int x;
cout<<"weigth of edge bw "<<i+1<<" & "<<j+1<<" ";
cin>>x;
graph[i][j]=(x);
graph[j][i]=(x);
}
}
int i = 0;
while(!cover(present,n) && i<n){
cout<<i+1<<" ";
present[i]=true;
int minpos = getmin(graph[i],n);
graph[i][minpos] = INT_MAX;
i = minpos;
}
return 0;
}