forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigtruck.cpp
85 lines (66 loc) · 1.72 KB
/
bigtruck.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
#include <bits/stdc++.h>
using namespace std;
int inf = 1 << 30;
struct node {
int dest;
int dist;
int items;
};
bool operator<(const node& n1, const node& n2) {
if(n1.dist == n2.dist) {
return n1.items < n2.items;
}
return n1.dist > n2.dist;
}
int main() {
int n;
cin >> n;
vector<int> items(n);
for(auto& i : items) {
cin >> i;
}
int m;
cin >> m;
// {dest,weight}
vector<vector<pair<int,int>>> adj(n);
for(int i = 0; i < m; i++) {
int n1, n2, w;
cin >> n1 >> n2 >> w;
n1--; n2--;
adj[n1].push_back({n2,w});
adj[n2].push_back({n1,w});
}
vector<int> bestDist(n,inf);
vector<int> bestItem(n,0);
vector<bool> vis(n,false);
priority_queue<node> q;
q.push({0,0,items[0]});
bestDist[0] = 0;
bestItem[0] = items[0];
while(!q.empty()) {
int curr = q.top().dest;
q.pop();
if(vis[curr]) {
continue;
}
vis[curr] = true;
for(auto i : adj[curr]) {
int next = i.first;
if(bestDist[next] > bestDist[curr] + i.second) {
bestDist[next] = bestDist[curr] + i.second;
bestItem[next] = bestItem[curr] + items[next];
q.push({next,bestDist[next],bestItem[next]});
}
else if(bestDist[next] == bestDist[curr] + i.second) {
bestItem[next] = max(bestItem[next], bestItem[curr] + items[next]);
q.push({next,bestDist[next],bestItem[next]});
}
}
}
if(bestDist[n-1] >= inf) {
cout << "impossible" << endl;
}
else {
cout << bestDist[n-1] << " " << bestItem[n-1] << endl;
}
}