-
Notifications
You must be signed in to change notification settings - Fork 0
/
14938.cpp
66 lines (60 loc) · 1.45 KB
/
14938.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
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int n, m, r;
int map_item[101] = {0, };
vector<pair<int, int>>map_dist[101];
void input();
int dijkstra(int start);
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
int answer = 0;
for(int i = 1; i <= n; i++){
answer = max(answer, dijkstra(i));
}
cout << answer;
}
void input(){
cin >> n >> m >> r;
for(int i = 1; i <= n; i++){
cin >> map_item[i];
}
int a, b, l;
for(int i = 0; i < r; i++){
cin >> a >> b >> l;
map_dist[a].push_back({b, l});
map_dist[b].push_back({a, l});
}
}
int dijkstra(int start){
int dist[101];
for(int i = 0; i <= n; i++){
dist[i] = 987654321;
}
dist[start] = 0;
priority_queue<pair<int, int>>q;
q.push({0, start});
while(!q.empty()){
int nowDist = -q.top().first;
int nowNode = q.top().second;
q.pop();
if(nowDist > dist[nowNode]) continue;
for(int i = 0; i < map_dist[nowNode].size(); i++){
int nextDist = nowDist + map_dist[nowNode][i].second;
int nextNode = map_dist[nowNode][i].first;
if(dist[nextNode] > nextDist){
dist[nextNode] = nextDist;
q.push({-nextDist, nextNode});
}
}
}
int ret = 0;
for(int i = 1; i <= n; i++){
if(dist[i] <= m)ret += map_item[i];
}
return ret;
}