LightOJ 1174 Commandos(FLoyd)

题解:

一队军队要走完所有的敌人基地,然后走到相应的终点,问最短时间。用Floyd算法解决,所得到的数据进行取最大值 ans = max (ans,dis[s][j]+dis[j][d]);

其实bfs也可以,从起点bfs一遍,再从终点bfs一遍,所需时间就是两者相加

代码

#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 110;
const int INF = 9999999;
int dis[MAXN][MAXN];
int s,d,t,n,r,x,y;

void init(){
    for(int i = 0; i < MAXN;i++){
        dis[i][i] = 0;
        for(int j = i+1; j < MAXN;j++)
            dis[i][j] = dis[j][i] = INF;
    }

}

void floyd(){
    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(){
    cin>>t;
    for(int i = 1;i <= t;i++){
        cin>>n;cin>>r;init();
        while(r--){
            cin>>x>>y;
            dis[x][y]=dis[y][x]=1;
        }
        cin>>s>>d;floyd();
        int ans = 0;
        for(int j = 0;j<n;j++){
            if(dis[s][j] == INF || dis[j][d] == INF) continue;
            ans = max(ans,dis[s][j]+dis[j][d]);
        }
        cout<<"Case "<<i<<": "<<ans<<endl;
    }
    return 0;
}

题目

Description
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 0to 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

你可能感兴趣的:(LightOJ 1174 Commandos(FLoyd))