-
Notifications
You must be signed in to change notification settings - Fork 12
/
1028.cpp
87 lines (83 loc) · 1.8 KB
/
1028.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
86
87
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;
const int MAXM=INT_MAX;
int ans[110][110];
struct Rot
{
char ip[50];
} r[110];
int top;
int f(char *ip)
{
for(int i=1;i<top;i++)
{
if(strcmp(r[i].ip,ip)==0)
return i;
}
return -1;
}
int main ()
{
int n , m;
char sour[50];
char dis[50];
while (scanf ("%d %d",&n,&m) != EOF)
{
top=1;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++)
{
ans[i][j] = MAXM;
}
ans[i][i] = 0;
}
while(m --)
{
int c;
scanf ("%s %s %d",sour,dis,&c);
int a=f(sour);
if(a==-1)
{
a=top;
strcpy(r[top++].ip,sour);
}
int b=f(dis);
if(b==-1)
{
b=top;
strcpy(r[top++].ip,dis);
}
ans[a][b] = ans[b][a] = c;
}
for (int k = 1; k <= n; k ++)
{
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++)
{
if (ans[i][k] == MAXM || ans[k][j] == MAXM) continue;
if (ans[i][k] + ans[k][j] < ans[i][j])
{
ans[i][j] = ans[i][k] + ans[k][j];
}
}
}
}
scanf("%d",&m);
while(m --)
{
scanf ("%s %s",sour,dis);
int a=f(sour);
int b=f(dis);
if(a==-1||b==-1||ans[a][b]==MAXM)
printf("-1\n");
else
printf("%d\n",ans[a][b]);
}
}
return 0;
}//Parsed in 0.181 seconds