Dummy Assignment

1 answer below »
Answered 18 days AfterFeb 10, 2021

Answer To: Dummy Assignment

Ibrahim answered on Mar 01 2021
143 Votes
Ans ​1​)

#include​
#include​
using​ ​namespace​ ​std​;
typedef​ pair<​double​,​int​> pr;
typedef​ vector vp
;
map mp;
int​ counter=-​1​;

class​ ​Comparator
{
​public:
​bool​ ​operator()​(​pr​&​ ​p1​,​pr​&​ ​p2​)
{
​return​ ​p1​.​first​>​p2​.​first​;
}
};
int​ ​getIndex​(​string​&​ ​city​)
{
​auto​ itr=​mp​.​find​(city);
​if​(itr==​mp​.​end​())
{
​mp​.​insert​(​make_pair​(city,++counter));
​return​ counter;
}
​return​ ​itr​->​second​;
}
class​ ​Graph
{
​public:
​int​ size;
vp* adj;
​Graph​(​int​ ​size​)
{
​this​->​adj​=​new​ ​vp​[size];
​this​->​size​=size;
}
​void​ ​addEdge​(​string​ ​city1​,​string​ ​city2​,​double​ ​w​)
{
​int​ u,v;
u=​getIndex​(city1);
v=​getIndex​(city2);
​this​->​adj​[u].​push_back​(​make_pair​(w,v));
​this​->​adj​[v].​push_back​(​make_pair​(w,u));
}
​void​ ​dfs​(​vector​<​int​>​&​ ​visited​,​int​ ​src​)
{
stack<​int​> stk;
​stk​.​push​(src);
​int​ node;
​visited​[src]=​true​;
​while​(!​stk​.​empty​())
{
node=​stk​.​top​();
​stk​.​pop​();
​for​(​auto​ itr=​adj​[node].​begin​();itr!=​adj​[node].​end​();++itr)
{
​if​(​visited​[​itr​->​second​]...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here