-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1984.cc
60 lines (49 loc) · 1.34 KB
/
1984.cc
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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct RoadTag {
int to, dist, index;
RoadTag() {}
RoadTag(int t, int d, int i) : to(t), dist(d), index(i) {}
};
vector<vector<RoadTag> > roads;
int main() {
int N, M;
cin >> N >> M;
roads.resize(N+1);
for(int i = 1; i <= M; ++i) {
int f1, f2, d;
char c;
cin >> f1 >> f2 >> d >> c;
roads[f1].push_back(RoadTag(f2, d, i));
roads[f2].push_back(RoadTag(f1, d, i));
}
int K;
cin >> K;
while(K--) {
int from, to, limit;
cin >> from >> to >> limit;
priority_queue<pair<int, int> > q;
vector<bool> used(N+1, false);
q.push(make_pair(0, from));
int ans = -1;
while(!q.empty()) {
int cost = q.top().first;
int farm = q.top().second;
q.pop();
if(used[farm]) continue;
used[farm] = true;
if(farm == to) {
ans = -cost;
break;
}
for(int i = 0; i < roads[farm].size(); ++i) {
if(used[roads[farm][i].to]) continue;
if(roads[farm][i].index > limit) continue;
q.push(make_pair(cost-roads[farm][i].dist, roads[farm][i].to));
}
}
cout << ans << endl;
}
}