lightoj 1174 - Commandos(最短路)

1174 - Commandos
Time Limit: 2 second(s)Memory Limit: 32 MB

A group of commandos were assigned a critical task. They are to destroy an enemy head quarter. The enemy head quarter consists of several buildings and the buildings are connected by roads. The commandos must visit each building and place a bomb at the base of each building. They start their mission at the base of a particular building and from there they disseminate to reach each building. The commandos must use the available roads to travel between buildings. Any of them can visit one building after another, but they must all gather at a common place when their task in done. In this problem, you will be given the description of different enemy headquarters. Your job is to determine the minimum time needed to complete the mission. Each commando takes exactly one unit of time to move between buildings. You may assume that the time required to place a bomb is negligible. Each commando can carry unlimited number of bombs and there is an unlimited supply of commando troops for the mission.

Input

Input starts with an integer T (≤50), denoting the number of test cases.

The first line of each case starts with a positive integer N (1 ≤ N ≤ 100), where N denotes the number of buildings in the head quarter. The next line contains a positive integer R, where R is the number of roads connecting two buildings. Each of the next R lines contain two distinct numbers u v (0 ≤ u, v < N), this means there is a road connecting building u to building v. The buildings are numbered from 0 to N-1. The last line of each case contains two integers s d (0 ≤ s, d < N). Where s denotes the building from where the mission starts and d denotes the building where they must meet. You may assume that two buildings will be directly connected by at most one road. The input will be given such that, it will be possible to go from any building to another by using one or more roads.

Output

For each case, print the case number and the minimum time required to complete the mission.

Sample Input

Output for Sample Input

2

4

3

0 1

2 1

1 3

0 3

2

1

0 1

1 0

Case 1: 4

Case 2: 1

 



题意:无限支军队从起点出发,最少要多长时间路过所有城市并且到达终点?

思路:

计算各个点到起点和终点的最短路之和,找出之和最大的即为答案



方法一,起点和终点各进行一次广搜

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+5;
int n, m, k, head[maxn], dis1[maxn], dis2[maxn];
struct node
{
	int v, next;
}edge[maxn];

void addEdge(int u, int v)
{
	edge[k].v = v;
	edge[k].next = head[u];
	head[u] = k++;
}

void bfs(int u, int dis[])
{
	dis[u] = 0;
	queue<int> q;
	q.push(u);
	while(!q.empty())
	{
		u = q.front(); q.pop();
		for(int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].v;
			if(dis[v] == -1) dis[v] = dis[u]+1, q.push(v);
		}
	}
}
int main(void)
{
	int t, s, e, ca = 1;
	cin >> t;
	while(t--)
	{
		k = 0;
		memset(head, -1, sizeof(head));
		cin >> n >> m;
		while(m--)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			addEdge(u, v);
			addEdge(v, u);
		}
		scanf("%d%d", &s, &e);
		
		int ans = 0;
		memset(dis1, -1, sizeof(dis1));
		memset(dis2, -1, sizeof(dis2));
		bfs(s, dis1);
		bfs(e, dis2);
		for(int i = 0; i < n; i++)
			ans = max(ans, dis1[i]+dis2[i]);
		printf("Case %d: ", ca++);
		printf("%d\n", ans);
	}
	return 0;
}

方法二,起点,终点各进行一次spfa

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+5;
const int INF = 0x3f3f3f3f;
int n, m, k, head[maxn], dis1[maxn], dis2[maxn];
bool book[maxn];
struct node
{
	int v, next;
}edge[maxn];

void addEdge(int u, int v)
{
	edge[k].v = v;
	edge[k].next = head[u];
	head[u] = k++;
}

void spfa(int u, int *dis)
{
	memset(book, 0, sizeof(book));
	queue<int> q;
	q.push(u);
	book[u] = 1;
	dis[u] = 0;
	while(!q.empty())
	{
		u = q.front(); q.pop();
		book[u] = 0;
		for(int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].v;
			if(dis[u]+1 < dis[v])
			{
				dis[v] = dis[u]+1;
				if(!book[v]) book[v] = 1, q.push(v);
			}
		}
	}
}
int main(void)
{
	int t, ca = 1;
	cin >> t;
	while(t--)
	{
		k = 0;
		memset(head, -1, sizeof(head));
		scanf("%d%d", &n, &m);
		while(m--)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			addEdge(u, v);
			addEdge(v, u);
		}
		int s, e, ans = 0;
		scanf("%d%d", &s, &e);
		memset(dis1, INF, sizeof(dis1));
		memset(dis2, INF, sizeof(dis2));
		spfa(s, dis1);
		spfa(e, dis2);		
		for(int i = 0; i < n; i++)
			ans = max(ans, dis1[i]+dis2[i]);
		printf("Case %d: ", ca++);
		printf("%d\n", ans);
	}
	return 0;
}


方法三,直接进行一次floyd,可以得到两两之间最短路

#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
const int INF = 0x3f3f3f3f;
int n, m, dis[maxn][maxn];

void init(void)
{
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			dis[i][j] = i==j ? 0 : INF;
}

void floyd(void)
{
	for(int k = 0; k < n; k++)
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
				dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);
}

int main(void)
{
	int t, ca = 1;
	cin >> t;
	while(t--)
	{
		scanf("%d%d", &n, &m);
		init();
		while(m--)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			dis[u][v] = dis[v][u] = 1;
		}
		floyd();
		int s, e, ans = 0;
		scanf("%d%d", &s, &e);
		for(int i = 0; i < n; i++)
		{
			if(dis[s][i] == INF || dis[i][e] == INF) continue;
			ans = max(ans, dis[s][i]+dis[i][e]);
		}
		printf("Case %d: ", ca++);
		printf("%d\n", ans);
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值