LightOJ 1174--Commandos (BFS)

来源:互联网 发布:java 银行卡号生成器 编辑:程序博客网 时间:2024/05/20 18:16


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), whereN denotes the number of buildings in the head quarter. The next line contains a positive integerR, where R is the number of roads connecting two buildings. Each of the nextR lines contain two distinct numbers u v (0 ≤ u, v < N), this means there is a road connecting buildingu to building v. The buildings are numbered from0 to N-1. The last line of each case contains two integerss d (0 ≤ s, d < N). Where s denotes the building from where the mission starts andd 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