P1346 电车

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 int cost[201];
 7 int g[201][201];
 8 int n, b, e;
 9 int temp[1];
10 
11 struct node
12 {
13     int n;
14     int cost;
15     node() {}
16     node(int a, int b)
17     {
18         n = a;
19         cost = b;
20     }
21 };
22 
23 int main()
24 {
25     memset(g, 0x3f, sizeof(g));
26     memset(cost, 0x3f, sizeof(cost));
27     cin >> n >> b >> e;
28     for (int i = 1; i <= n; i++)
29     {
30         int m;
31         cin >> m;
32         if (!m) continue;
33         int t;
34         cin >> t;
35         g[i][t] = 0;
36         for (int j = 1; j < m; j++)
37         {
38             cin >> t;
39             g[i][t] = 1;
40         }
41     }
42     queue<node>q;
43     q.push(node(b, 0));
44     while (!q.empty())
45     {
46         node t = q.front();
47         q.pop();
48         for (int i = 1; i <= n; i++)
49         {
50             if (g[t.n][i] + t.cost < cost[i])
51             {
52                 cost[i] = g[t.n][i] + t.cost;
53                 q.push(node(i, g[t.n][i] + t.cost));
54             }
55         }
56     }
57     memset(temp, 0x3f, sizeof(temp));
58     if (cost[e] != temp[0]) cout << cost[e];
59     else cout << -1;
60 }
View Code

 

posted on 2019-10-09 13:32  thjkhdf12  阅读(115)  评论(0)    收藏  举报