码迷,mamicode.com
首页 > 其他好文 > 详细

Checkpoints(第十一届河南省省赛真题)

时间:2018-06-03 19:35:35      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:any   lan   ret   rom   tput   party   directed   follow   节点   

题目描述

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

 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 }

 

Checkpoints(第十一届河南省省赛真题)

标签:any   lan   ret   rom   tput   party   directed   follow   节点   

原文地址:https://www.cnblogs.com/wenzhixin/p/9129843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!