-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvestigation.cpp
More file actions
66 lines (54 loc) · 1.77 KB
/
Copy pathInvestigation.cpp
File metadata and controls
66 lines (54 loc) · 1.77 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
#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, int> pli;
/*
use priorirty queue to traverse through neighbors
if already visited a neighbour with a lower visit cost then ignore
else if previous visited cost is higher then visit & reset params
else if prev visit cost is equal then add ways
*/
signed main() {
int n, m;
cin >> n >> m;
map<int, vector<pli>> adj;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
adj[a].push_back({b, c});
}
vector<long long> visit_cost(n+1, 1e17);
vector<int> ways(n + 1);
vector<int> min_node_cnt(n + 1, 1e6);
vector<int> max_node_cnt(n + 1, 0);
int mod = 1e9 + 7;
ways[1] = 1;
min_node_cnt[1] = 0;
max_node_cnt[1] = 0;
visit_cost[1] = 0;
// store {cost , vertex}
priority_queue<pli, vector<pli>, greater<pli>> pq;
pq.push({0, 1});
while(!pq.empty()) {
auto [cur_cost, cur] = pq.top();
pq.pop();
for(auto [next, price] : adj[cur]){
long long cost = cur_cost + price;
if(cost > visit_cost[next]) continue;
if(visit_cost[next] == cost) {
(ways[next] += ways[cur]) %= mod;
min_node_cnt[next] = min(min_node_cnt[next], 1 + min_node_cnt[cur]);
max_node_cnt[next] = max(max_node_cnt[next], 1 + max_node_cnt[cur]);
} else {
visit_cost[next] = cost;
ways[next] = ways[cur];
min_node_cnt[next] = 1 + min_node_cnt[cur];
max_node_cnt[next] = 1 + max_node_cnt[cur];
pq.push({cost, next});
}
}
}
cout << visit_cost[n] << " ";
cout << ways[n] << " ";
cout << min_node_cnt[n] << " ";
cout << max_node_cnt[n] << endl;
}