-
Notifications
You must be signed in to change notification settings - Fork 10
/
HackerEarth-Smart travel agent.cpp
99 lines (90 loc) · 2.39 KB
/
HackerEarth-Smart travel agent.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Here exactly , U have to find out the route from s to d where maximum peaople can travel
( minimum people capacity of this route ) ,
because minimum number of trips are needed ,when maximum people can travel then trips will be reduced !!
Now , use dijkstra slightly changed like as we will take the maximum capacity edges keep the track of minimum of this route
and make this distance of the destination (d ) and keep the track of parent of the nodes .
By traversing parent from destination we will reach our source ...
Here all time one extra man who will guide the travellers .Keep this mind .My normal code
*/
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
const int mx=1e6;
bool vis[mx];
int parent[mx];
vector<pii>adj[mx];
int dist[mx];
void dijkstra(int source)
{
priority_queue<pii>q;
dist[source]=1000000007;
parent[source]=source;
q.push(make_pair(dist[source],source));
while(!q.empty())
{
pii pq=q.top();
q.pop();
int u=pq.second;
if(vis[u])
{
continue;
}
vis[u]=true;
vector<pii>::iterator it;
for(it=adj[u].begin();it!=adj[u].end();it++)
{
int v=(*it).second;
int capacity=(*it).first;
int lwmn=min(dist[u],capacity);
if(lwmn>dist[v])
{
parent[v]=u;
dist[v]=lwmn;
q.push(make_pair(dist[v],v));
}
}
}
}
int main()
{
int n,m,u,v,w;
cin>>n>>m;
for(int i=0;i<=n;i++)
{
parent[i]=-1;
dist[i]=0;
vis[i]=false;
}
int s,d,c;
while(m--)
{
cin>>u>>v>>w;
adj[u].push_back(make_pair(w,v));
adj[v].push_back(make_pair(w,u));
}
cin>>s>>d>>c;
dijkstra(s);
vector<int>ans;
ans.push_back(d);
int t=d;
while(t!=s)
{
t=parent[t];
ans.push_back(t);
}
for(int j=ans.size()-1;j>=0;j--)
{
cout<<ans[j]<<" ";
}
cout<<endl;
int total_people_can_be_travel_this_route = dist[d]-1;
if(c%total_people_can_be_travel_this_route==0)
{
cout<<(c/total_people_can_be_travel_this_route)<<endl;
}
else
{
cout<<(c/total_people_can_be_travel_this_route)+1<<endl;
}
return 0;
}