-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1240.cpp
More file actions
56 lines (46 loc) · 1.2 KB
/
1240.cpp
File metadata and controls
56 lines (46 loc) · 1.2 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
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include <string>
using namespace std;
typedef pair<int, int>P;
vector<P> adj[1001];
int visited[1001];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
int M;
cin >> N >> M;
for (int i = 0; i < N-1; i++) {
int a, b, d;
cin >> a >> b >> d;
adj[a].push_back(P(b, d));
adj[b].push_back(P(a, d));
}
for (int i = 0; i < M; i++) {
fill(&visited[0], &visited[1001], 0);
queue<P>q;
int start, end;
cin >> start >> end;
q.push(P(start,0));
visited[start] = 1;
int min = 123456789;
while (!q.empty()) {
P curr = q.front(); q.pop();
if (curr.first == end) {
min = curr.second;
break;
}
for (auto next : adj[curr.first]) {
if (visited[next.first] == 0) {
visited[next.first] = 1;
q.push(P(next.first, curr.second + next.second));
}
}
}
cout << min << '\n';
}
}