LightOJ 1174--Commandos (BFS)


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

2

4

3

0 1

2 1

1 3

0 3

2

1

0 1

1 0

Sample Output

Case 1: 4

Case 2: 1


题意:  求一队人在城市中的每一个建筑物放炸弹所需的最少时间..

思路: 一开始读错题了, 以为是一起走的, 就直接将题目简化成一个人走 所需的最少时间, 题目变难了好多...

        其实人是可以分开走的, BFS 解决~~从起点到终点求得所需最长时间, 再从终点到起点求得所需的最长时间, 两个时间加起来便是答案了~

CODE:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int map[105][105];
int num;
struct node
{
    int x,y;
    node(int x=0,int y=0):x(x),y(y){};
};
int bfs(int n,int *s)
{
    queue<int> que;
    int now,next;
    memset(s,0,sizeof(s));
    now=n;
    que.push(now);
    while(!que.empty())
    {
        now=que.front();
        que.pop();
        for(int i=0;i<num;i++)
        {
            next=i;
            if(map[now][i]==1&&s[i]==0&&i!=n)
            {
                s[i]=s[now]+1;
                que.push(next);
            }
        }
    }
}
int main()
{
    //freopen("in.in.c","r",stdin);
    int T;
    int A[105],B[105];
    scanf("%d",&T);
    for(int tt=1;tt<=T;tt++)
    {
        int q;
        int a,b;
        int S,E;
        memset(A,0,sizeof(A));
        memset(B,0,sizeof(B));
        memset(map,0,sizeof(map));
        scanf("%d%d",&num,&q);
        for(int i=0;i<q;i++)
        {
            scanf("%d %d",&a,&b);
            map[a][b]=map[b][a]=1;
        }
        scanf("%d%d",&S,&E);
        bfs(S,A);
        bfs(E,B);
        int ans=0;
        for(int i=0;i<num;i++)
        {
            ans=max(A[i]+B[i],ans);
        }
        printf("Case %d: %d\n",tt,ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值