NWU_ACM

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 #include<stack>
 2 #include<queue>
 3 #include<cmath>
 4 #include<vector>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 #define inf 99999
11 int n,m,k;
12 vector<int>G[10002];
13 queue<int>q;
14 typedef struct
15 {
16     int id,l;
17 }d;
18 d dis[10002];
19 
20 bool cmp(d a, d b)
21 {
22     return a.l < b.l;
23 }
24 
25 int spfa(int x)
26 {
27     while(!q.empty()) q.pop();
28     bool inq[10002];
29     for(int i=1;i<=n;i++) {dis[i].l = inf;dis[i].id = i;}
30     memset(inq,false,sizeof(inq));
31     dis[0].l = -1;
32     dis[x].l = 0;
33     inq[x] = true;
34     q.push(x);
35     while(!q.empty())
36     {
37         int u = q.front(); q.pop(); inq[u] = false;
38         for(int i=0;i<G[u].size();i++)
39         {
40             int v = G[u][i];
41             if(dis[u].l + 1 < dis[v].l)
42             {
43                 dis[v].l = dis[u].l + 1;
44                 if(!inq[v]){
45                     inq[v] = true;
46                     q.push(v);
47                 }
48             }
49         }
50     }
51     sort(dis,dis+n+1,cmp);
52 
53     int max1,ans;
54     for(int i=n;i>=1;i--)
55     {
56         if(dis[i].l == inf){
57             continue;
58         }
59         else{
60             max1 = dis[i].l;
61             ans = i;
62             break;
63         }
64     }
65 
66     for(int i=ans - 1;i>=1;i--)
67     {
68         if(dis[i].l != max1)
69         {
70             return dis[i+1].id;
71         }
72     }
73 
74 }
75 
76 int main()
77 {
78     int x,y;
79     scanf("%d%d%d",&n,&m,&k);
80     for(int i=0;i<m;i++)
81     {
82         scanf("%d%d",&x,&y);
83         G[x].push_back(y);
84         G[y].push_back(x);
85     }
86     for(int i=1;i<=k;i++)
87     {
88         int qu;
89         scanf("%d",&qu);
90         if(G[qu].size() == 0)
91         {
92             printf("0\n");
93             continue;
94         }
95         printf("%d\n",spfa(qu));
96     }
97 }
View Code

 

posted on 2017-03-13 20:04  NWU_ACM  阅读(155)  评论(0编辑  收藏  举报