先在反图中从根节点DFS/BFS找出不能经过的点。。
然后跑最短路。。
【艹调了半天 改了若干写法 原来是STL+指针玩脱了
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 |
// <road.cpp> - 02/09/16 10:59:28 // This file is created by XuYike's black technology automatically. // Copyright (C) 2015 ChangJun High School, Inc. // I don't know what this program is. #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> using namespace std; typedef long long lol; int getint(){ int res=0,fh=1;char ch=getchar(); while((ch>'9'||ch<'0')&&ch!='-')ch=getchar(); if(ch=='-')fh=-1,ch=getchar(); while(ch>='0'&&ch<='9')res=res*10+ch-'0',ch=getchar(); return fh*res; } const int MAXN=10002; vector <int> b[MAXN],fb[MAXN]; struct point{ int x,dis; bool operator <(point b) const{return dis>b.dis;} bool operator >(point b) const{return dis<b.dis;} }; priority_queue <point> p; bool alive[MAXN],geng[MAXN]; bool death[MAXN]; int dis[MAXN]; void dfs(int x){ alive[x]=1; for(int i=0;i<fb[x].size();i++)if(!alive[fb[x][i]])dfs(fb[x][i]); } int main(){ freopen("road.in","r",stdin); freopen("road.out","w",stdout); int n=getint(),m=getint(); for(int i=1;i<=m;i++){ int x=getint(),y=getint(); if(x==y)continue; b[x].push_back(y); fb[y].push_back(x); } int s=getint(),t=getint(); dfs(t); for(int i=1;i<=n;i++) if(!alive[i]) for(int o=0;o<fb[i].size();o++)if(fb[i][o]!=t)death[fb[i][o]]=1; for(int i=1;i<=n;i++)if(death[i])alive[i]=0; if(!alive[s]){cout<<-1;return 0;} dis[s]=1; p.push((point){s,1}); while(!p.empty()){ int now=p.top().x;p.pop(); if(geng[now])continue; geng[now]=1; for(int i=0;i<b[now].size();i++){ int next=b[now][i]; if(!alive[next])continue; if(!dis[next]||dis[now]+1<dis[next]){ dis[next]=dis[now]+1; p.push((point){next,dis[next]}); } } } cout<<dis[t]-1; return 0; } |