-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11562.cpp
More file actions
30 lines (30 loc) · 712 Bytes
/
Copy path11562.cpp
File metadata and controls
30 lines (30 loc) · 712 Bytes
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
#include<stdio.h>
#include<iostream>
#include<algorithm>
#define INF 987654321
int dp[251][251];
using namespace std;
int main() {
int N, M, K, a, b, c;
scanf("%d %d", &N, &M);
for (int i = 0; i <= N; i++)for (int j = 0; j <= N; j++)dp[i][j] = INF;
for (int i = 0; i <= N; i++)dp[i][i] = 0;
while (M--) {
scanf("%d %d %d", &a, &b, &c);
if (c == 0) dp[b][a] = 1, dp[a][b] = 0;
else dp[a][b] = dp[b][a] = 0;
}
for (int k = 1; k <= N; k++)
for(int i=1;i<=N;i++)
for (int j = 1; j <= N; j++) {
if (dp[i][j]>dp[i][k]+dp[k][j]) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
}
scanf("%d", &K);
while (K--) {
scanf("%d %d", &a, &b);
printf("%d\n", dp[a][b]);
}
return 0;
}