#include <iostream>
#define FIN "salesman.in"
#define BIG 999999
#define DIM 100
using namespace std;
int matrix[ DIM ][ DIM ], //define adjacency matrix
explored[ DIM ], //visited array
nodes; //define a variable that holds the number of the nodes;
void readMatrix() {
cin>>nodes;
for(int i = 1; i <= nodes; ++i) {
for(int j = 1; j <= nodes; ++j) {
cin>>matrix[i][j];
}
}
cout<<"Adjacency Matrix:\n";
for(int i = 1; i <= nodes; ++i) {
for(int j = 1; j <= nodes; ++j) {
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n";
}
void greedy() {
int node, start, min, next_node = 1, cost = 0;
for(int peak = 1; peak <= nodes; ++peak) explored[ peak ] = 0;
node = 1;
start = node;
explored[ node ] = 1;
cout<<"Road:\n";
cout<<node<<" ";
for(int i = 1; i <= nodes - 1; ++i) {
min = BIG;
for(int vertice = 1; vertice <= nodes; ++vertice) {
if(matrix[ node ][ vertice ] != 0 && explored[ vertice ] == 0 && matrix[ node ][ vertice ] < min) {
min = matrix[ node ][ vertice ];
next_node = vertice;
}
}
cout<<next_node<<" ";
explored[ next_node ] = 1;
cost = cost + matrix[ node ][ next_node ];
node = next_node;
}
cout<<start;
cost = cost + matrix[ start ][ node ];
cout<<"\nCost = "<<cost<<"\n";
}
int main(int argc, char const *argv[]) {
//freopen(FIN, "r", stdin);
readMatrix();
greedy();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojZGVmaW5lIEZJTiAic2FsZXNtYW4uaW4iCiNkZWZpbmUgQklHIDk5OTk5OQojZGVmaW5lIERJTSAxMDAKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWF0cml4WyBESU0gXVsgRElNIF0sIC8vZGVmaW5lIGFkamFjZW5jeSBtYXRyaXgKICAgIGV4cGxvcmVkWyBESU0gXSwgLy92aXNpdGVkIGFycmF5CiAgICBub2RlczsgLy9kZWZpbmUgYSB2YXJpYWJsZSB0aGF0IGhvbGRzIHRoZSBudW1iZXIgb2YgdGhlIG5vZGVz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9PSAwICYmIG1hdHJpeFsgbm9kZSBdWyB2ZXJ0aWNlIF0gPCBtaW4pIHsKCiAgICAgICAgICAgICAgICAgICAgICAgICBtaW4gPSBtYXRyaXhbIG5vZGUgXVsgdmVydGljZSBdOwoKICAgICAgICAgICAgICAgICAgICAgICAgIG5leHRfbm9kZSA9IHZlcnRpY2U7CiAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICBjb3V0PDxuZXh0X25vZGU8PCIgIjsKCiAgICAgICAgICAgICBleHBsb3JlZFsgbmV4dF9ub2RlIF0gPSAxOwoKICAgICAgICAgICAgIGNvc3QgPSBjb3N0ICsgbWF0cml4WyBub2RlIF1bIG5leHRfbm9kZSBdOwoKICAgICAgICAgICAgIG5vZGUgPSBuZXh0X25vZGU7CiAgICAgfQoKCiAgICAgY291dDw8c3RhcnQ7CgogICAgIGNvc3QgPSBjb3N0ICsgbWF0cml4WyBzdGFydCBdWyBub2RlIF07CgogICAgIGNvdXQ8PCJcbkNvc3QgPSAiPDxjb3N0PDwiXG4iOwp9CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciBjb25zdCAqYXJndltdKSB7CgogIC8vZnJlb3BlbihGSU4sICJyIiwgc3RkaW4pOwoKICByZWFkTWF0cml4KCk7CgogIGdyZWVkeSgpOwoKICByZXR1cm4gMDsKfQo=