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.
Input
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.
1 /*
2 问题
3 输入节点数和边数,起点和终点,以及每一条边,计算从起点到终点再返回起点途中最少要经过几个顶点,重复的点不计。
4
5 解题思路
6 采用Dijkstra算法求一下起点到个点的最短路,然后将走过的点标记一下,再求一下终点到其他点的最短距离,标记过的点
7 不计算就可以了。
8 */
9 #include<cstdio>
10 #include<cstring>
11 int e[110][110],book[110],n,m,ans;
12 void Dijkstra(int s);
13 int main()
14 {
15 int T,u,v,a,b,i,j;
16 scanf("%d",&T);
17 while(T--){
18 scanf("%d%d%d%d",&n,&m,&a,&b);
19 for(i=0;i<n;i++){
20 for(j=0;j<n;j++){
21 e[i][j] = i==j?0:99999999;
22 }
23 }
24 memset(book,0,sizeof(book));
25 while(m--){
26 scanf("%d%d",&u,&v);
27 e[u][v]=1;
28 }
29 ans=0;
30 Dijkstra(a);
31 Dijkstra(b);
32 printf("%d\n",ans);
33 }
34 return 0;
35 }
36
37 void Dijkstra(int s)
38 {
39 int dis[110],bk[110]={0},i,j;
40 for(i=0;i<n;i++)
41 dis[i]=e[s][i];
42
43 dis[s]=0;
44 int u=1;
45 bk[s]=1;
46 for(i=0;i<n;i++){
47 int mina=99999999;
48 for(j=0;j<n;j++){
49 if(bk[j] == 0 && dis[j] < mina){
50 dis[j]=mina;
51 u=j;
52 }
53 }
54 bk[u]=1;
55 if(book[u] == 0)
56 ans++;
57 book[u]=1;
58 for(j=0;j<n;j++){
59 if(bk[j] == 0 && dis[j] < e[u][j]){
60 dis[j]=e[u][j];
61 }
62 }
63 }
64 }