我是靠谱客的博主 乐观黑夜,最近开发中收集的这篇文章主要介绍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.

样例输入 Copy

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

样例输出 Copy

2

解题思路:

这道题数据真的很水啊!有向图求两个点的最短路径问题。

AC代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1e9+7;
int n,m,a,b;
int map[105][105];
int vis[105],dis[105];
void dijkstra(int x)
{	
	memset(vis,1,sizeof(vis));
	int i,j;
	for(int i=1;i<=n;i++)
		dis[i]=map[x][i];
	vis[x]=0;
	for(i=1;i<=n;i++)
	{
		int min=INF;
		int temp;
		for(j=1;j<=n;j++)
			if(vis[j]&&dis[j]<min)
			{
				min=dis[j];
				temp=j;
			}
		vis[temp]=0;
		for(j=1;j<=n;j++)
			if(vis[j]&&dis[temp]+map[temp][j]<dis[j])
				dis[j]=dis[temp]+map[temp][j];
	}
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>a>>b;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				map[i][j]=(i==j?0:INF);
		for(int i=0;i<m;i++)
		{
			int a1,a2;
			cin>>a1>>a2;
			map[a1][a2]=1;
		}
		dijkstra(a);
		cout<<dis[b]<<endl;
	}
}

 

最后

以上就是乐观黑夜为你收集整理的Checkpoints的全部内容,希望文章能够帮你解决Checkpoints所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部