-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminhops.cpp
More file actions
executable file
·93 lines (81 loc) · 1.82 KB
/
minhops.cpp
File metadata and controls
executable file
·93 lines (81 loc) · 1.82 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//Dijkstra's using min_prioriy_queue
//O(V+ElogV)
#include <bits/stdc++.h>
#define f first
#define s second
#define ll unisgned long long int
#define SIZE 1000000
using namespace std;
vector < pair < ll,ll> > v[SIZE];
vector<pair <ll, ll> > lastedge;
bool vis[SIZE];
bool bothtypes[SIZE];
ll dist[SIZE];
ll edge[SIZE];
ll hopcount[SIZE];
void djk()
{
memset(vis, false, sizeof(vis));
memset(bothtypes, false, sizeof(bothtypes));
dist[1]=0;
edge[1]=0;
hopcount[1]=0;
multiset < pair <ll,ll> > s;
s.insert({0,1});
while(!s.empty())
{
pair< ll, ll> p = *(s.begin());
s.erase(s.begin());
ll fromNode = p.second;
if(vis[fromNode])continue;
vis[fromNode]=true;
for(ll i=0; i<(ll)v[fromNode].size(); i++)
{
ll toNode = v[fromNode][i].first;
ll w = v[fromNode][i].second;
if( dist[toNode]>(dist[fromNode]+w) )
{
dist[toNode]=(dist[fromNode]+w);
edge[toNode]=w;
//cout<<edge[toNode]<<" "<<dist[toNode]<<" for node "<<toNode<<"\n";
s.insert({dist[toNode], toNode});
if( ((w+edge[fromNode])%2==0) || bothtypes[fromNode] || fromNode==1)
hopcount[toNode]=hopcount[fromNode];
else
hopcount[toNode]=(1+hopcount[fromNode]);
}
else if(dist[toNode]==(dist[fromNode]+w))
{
if( (edge[toNode]%2==0 && w%2!=0) || (edge[toNode]%2!=0 && w%2==0) )
bothtypes[toNode]=1;
else if(((w+edge[fromNode])%2==0))
{
//cout<<"equal dist condition with same edge\n";
s.insert({dist[toNode], toNode});
edge[toNode]=w;
hopcount[toNode]=hopcount[fromNode];
}
}
}
}
}
int main(void)
{
ll n, m;
ll x, y, w;
cin>>n>>m;
for(ll i=0; i<m; i++)
{
cin>>x>>y>>w;
pair<ll, ll> p;
p = {y, w};
v[x].push_back(p);
p={x, w};
v[y].push_back(p);
}
for(ll j=0; j<=n; j++)
dist[j]=ULONG_MAX;
djk();
cout<<dist[n]<<" "<<hopcount[n]<<"\n";
return 0;
}