标签:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<queue> 5 #include<stack> 6 #include<vector> 7 #include<algorithm> 8 #include<string> 9 #include<cstring> 10 #include<iomanip> 11 #include<bitset> 12 #include<cstdlib> 13 using namespace std; 14 vector <int> f[10005],g[10005]; 15 queue <int> q; 16 int n,m,a,b,s,t,maxn; 17 int dis[10005]; 18 bool vis[10005],del[10005],iin[10005],dee[10005]; 19 void deletee(int x) 20 { 21 del[x]=false; 22 for (int i=0;i<g[x].size();i++) 23 if (del[g[x][i]])//here 24 deletee(g[x][i]); 25 } 26 void sp() 27 { 28 for (int i=1;i<=n;i++) 29 if (del[i]) 30 for (int j=0;j<g[i].size();j++) 31 dee[g[i][j]]=true; 32 for (int i=1;i<=n;i++) 33 dee[i]=dee[i]|del[i]; 34 } 35 void spfa() 36 { 37 memset(dis,127,sizeof(dis)); 38 dis[s]=0; 39 q.push(s); 40 iin[s]=true; 41 while (!q.empty()) 42 { 43 int k=q.front();q.pop();iin[k]=false; 44 for (int i=0;i<f[k].size();i++) 45 if (!dee[f[k][i]]&&dis[k]+1<dis[f[k][i]]) 46 { 47 dis[f[k][i]]=dis[k]+1; 48 if (!iin[f[k][i]]) 49 { 50 q.push(f[k][i]); 51 iin[f[k][i]]=true; 52 } 53 } 54 } 55 } 56 int main() 57 { 58 freopen ("road.in","r",stdin); 59 //freopen ("road.out","w",stdout); 60 scanf ("%d%d",&n,&m); 61 for (int i=1;i<=m;i++) 62 { 63 scanf ("%d%d",&a,&b); 64 if (a!=b) 65 { 66 f[a].push_back(b); 67 g[b].push_back(a); 68 } 69 } 70 scanf ("%d%d",&s,&t); 71 memset(del,true,sizeof(del)); 72 deletee(t);sp(); 73 spfa(); 74 if (dis[t]>100000) printf ("-1"); 75 else printf ("%d",dis[t]); 76 return 0; 77 }
标签:
原文地址:http://www.cnblogs.com/Ang-Eric/p/5782580.html