记录编号 |
139500 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[NOIP 2014]寻找道路 |
最终得分 |
100 |
用户昵称 |
Asm.Def |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.098 s |
提交时间 |
2014-11-12 16:35:26 |
内存使用 |
0.58 MiB |
显示代码纯文本
/*======================================================*/
/*========================ASM.DEF=======================*/
/*======================================================*/
#include <cstdio>
#include <cctype>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <list>
using namespace std;
#if defined WJX_Asm_Def_DEBUG
FILE *in = fopen("test", "r");
#define out stdout
#else
FILE *in = fopen("roadb.in", "r");
FILE *out = fopen("roadb.out", "w");
#endif
inline bool getd(int &x){
int c = fgetc(in);
bool minus = 0;
while(c != '-' && c != EOF && !isdigit(c))c = fgetc(in);
if(c == EOF)return 0;
if(c == '-'){
x = 0;
minus = 1;
}
else x = c - '0';
while(isdigit(c = fgetc(in)))x = x * 10 - '0' + c;
return 1;
}
#define iter(v) v::iterator
#define pb push_back
#define pf push_front
#define ppf pop_front
/*===========================================================*/
const int maxn = 10000 + 5, INF = 0x3f3f3f3f;
list<int> adj[maxn];
vector<int> back[maxn];
int n, m, s, t;
int dis[maxn];
bool in_p[maxn] = {0}, known[maxn] = {0}, linked[maxn] = {0};
void dfs(int cur){//判断是否与t联通
iter(vector<int>) it;
for(it = back[cur].begin();it !=back[cur].end();++it){
if(!known[*it]){
linked[*it] = known[*it] = 1;
dfs(*it);
}
}
}
inline void init(){
getd(n), getd(m);
int x, y;
while(m--){
getd(x), getd(y);
if(x == y)continue;
adj[x].pb(y);
back[y].pb(x);
}
getd(s), getd(t);
linked[t] = 1, known[t] = 1, in_p[t] = 1;
dfs(t);
iter(list<int>) it;
for(int i = 1;i <= n;++i){
bool ok = 1;
for(it = adj[i].begin();it != adj[i].end();++it)
if(!linked[*it]){ok = 0;break;}
if(ok)in_p[i] = 1;
dis[i] = INF;
}
}
deque<int> Q;
bool inQ[maxn] = {0};
inline void work(){
if(!in_p[s]){fprintf(out, "-1\n");return;}
Q.pb(s), inQ[s] = 1, dis[s] = 0;
int k;
iter(list<int>) it;
while(!Q.empty()){
k = Q.front();inQ[k] = 0;Q.ppf();
for(it = adj[k].begin();it != adj[k].end();++it){
if(!in_p[*it]){
it = adj[k].erase(it);
--it;
}
else if(dis[k] + 1 < dis[*it]){
dis[*it] = dis[k] + 1;
if(inQ[*it])continue;
inQ[*it] = 1;
if(Q.empty() || dis[*it] <= dis[Q.front()])Q.pf(*it);//Small Label First
else Q.pb(*it);
}
}
}
if(dis[t] == INF)fprintf(out, "-1\n");
else fprintf(out, "%d\n", dis[t]);
}
int main(){
init();
work();
#if defined WJX_Asm_Def_DEBUG
cout << (double)clock() / CLOCKS_PER_SEC << endl;
#endif
return 0;
}