# nyoj 1369-Checkpoints （spfa）

1369-Checkpoints

As a landlocked country in central and southern Africa , the political situation has been relatively stable since the implementation of multi-party elections in ZBA in 1991. But the ZBA parliament passed the 90 day emergency order issued by the president on 11 days of local time . The tension is that the patriotic team led by the government troops and NPG leaders founded by aborigines started, in addition to the unlawful insurgents of illegal militants.

Chinese peacekeepers are going to the town of Kerver to seek Chinese foreign aid engineers.

The military map shows that there are many checkpoints in the war zone. It can be modeled as a directed graph: nodes represent checkpoints , and edges represents the roads. The goal is that the less peacekeepers pass the checkpoints, the safer it will be.

The first line of the input contains one integer T, which is the number of test cases (1<=T<=5). Each test case specifies:

 * Line 1:      N  M  A  B          (2 ≤ N ≤ 100)


N and M denote the number of nodes and edges in the directed graph respectively. The checkpoints are labeled 1, 2, …, N, where chinese peacekeepers at node A and foreign aid engineers at node B.

*Line 2~m+1: Xi Yi (i=1, …., M)

followed by M lines containing two integers Xi and Yi (1 ≤ Xi, Yi ≤ N), denoting that there is a directed edge from node Xi to node Yi in the network.

For each test case generate a single line: a single integer that the minimum number of checkpoints . If a checkpoint is passed on the way back and forth , it will be counted only once.

1
6 10 1 5
1 2
2 1
2 3
3 5
5 4
4 2
1 6
6 5
5 3
3 2

2

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<set>
using namespace std;
const int N = 1e6+10;
const int M = 500000;
typedef long long LL;
const LL mod = 20180520;
const int maxt=1000000000;
struct node
{
int x;
bool vis[105];
};
queue<node>q;
struct Q
{
int to, next;
} p[10010];
set<int>mp1[110],mp2[110];
void init()
{
for(int i=0;i<=100;i++)mp1[i].clear(),mp2[i].clear();
cnt=0;
}
{
return ;
}
void bfs(int s,int t,set<int>st[110])
{
node tmp;
tmp.x=s;
memset(tmp.vis,0,sizeof(tmp.vis));
tmp.vis[s]=1;
while(!q.empty())q.pop();
q.push(tmp);
int num=0;
while(!q.empty())
{
tmp=q.front();
q.pop();
if(num>=100)break;
int u=tmp.x;
{
int v=p[i].to;
if(tmp.vis[v])continue;
node o=tmp;
o.vis[v]=1,o.x=v;
if(v!=t)q.push(o);
else
{
for(int i=1;i<=100;i++)
{
if(o.vis[i])st[num].insert(i);
}
num++;
}
}
}
return ;
}
int solve()
{
int ans=10000;
for(int i=0;i<=100;i++)
{
if(mp1[i].size()==0)break;
for(int j=0;j<=100;j++)
{
if(mp2[j].size()==0)break;
set<int>mp=mp1[i];
set<int>::iterator it;
for(it=mp2[j].begin();it!=mp2[j].end();it++)mp.insert(*it);
ans=min(ans,(int)mp.size());
}
}
return ans;
}

int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, a, b;
scanf("%d %d %d %d", &n, &m, &a, &b);
init();
for(int i=0; i<m; i++)
{
int x,y;
scanf("%d %d", &x, &y);
}
bfs(a,b,mp1);
bfs(b,a,mp2);
printf("%d\n",solve()-2);
}
return 0;
}