题目描述
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.
输出
Output
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
/*
问题
输入节点数和边数,起点和终点,以及每一条边,计算从起点到终点再返回起点途中最少要经过几个顶点,重复的点不计。 解题思路
采用Dijkstra算法求一下起点到个点的最短路,然后将走过的点标记一下,再求一下终点到其他点的最短距离,标记过的点
不计算就可以了。
*/
#include<cstdio>
#include<cstring>
int e[][],book[],n,m,ans;
void Dijkstra(int s);
int main()
{
int T,u,v,a,b,i,j;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&n,&m,&a,&b);
for(i=;i<n;i++){
for(j=;j<n;j++){
e[i][j] = i==j?:;
}
}
memset(book,,sizeof(book));
while(m--){
scanf("%d%d",&u,&v);
e[u][v]=;
}
ans=;
Dijkstra(a);
Dijkstra(b);
printf("%d\n",ans);
}
return ;
} void Dijkstra(int s)
{
int dis[],bk[]={},i,j;
for(i=;i<n;i++)
dis[i]=e[s][i]; dis[s]=;
int u=;
bk[s]=;
for(i=;i<n;i++){
int mina=;
for(j=;j<n;j++){
if(bk[j] == && dis[j] < mina){
dis[j]=mina;
u=j;
}
}
bk[u]=;
if(book[u] == )
ans++;
book[u]=;
for(j=;j<n;j++){
if(bk[j] == && dis[j] < e[u][j]){
dis[j]=e[u][j];
}
}
}
}