码迷,mamicode.com
首页 > 其他好文 > 详细

floyd的应用

时间:2015-04-08 12:24:22      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

A - Commandos
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Appoint description: 

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 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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
#define INF 100000000
int n,m,dist[105][105];
void floyd()
{
    for(int k=0;k<n;k++)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(dist[i][j]>dist[i][k]+dist[k][j])
                    dist[i][j]=dist[i][k]+dist[k][j];
            }
        }
    }
}
int main()
{
    int tt,a,b,st,ed,cas=1;
    scanf("%d",&tt);
    while(tt--)
    {
        int ans=0;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                dist[i][j]=INF,dist[i][i]=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            dist[a][b]=dist[b][a]=1;
        }
        floyd();
        scanf("%d%d",&st,&ed);
        for(int i=0;i<n;i++)
        {
            if(dist[st][i]>=INF||dist[i][ed]>=INF)
                continue;
            if(dist[st][i]+dist[i][ed]>ans)
                ans=dist[st][i]+dist[i][ed];
        }
        printf("Case %d: %d\n",cas,ans);
        cas++;
    }
    return 0;
}

  

floyd的应用

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4401702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!