Foj 1635 Commandos【最短路Floyd】

来源:互联网 发布:程序员文史综合题目 编辑:程序博客网 时间:2024/05/16 09:52

 Problem 1635 Commandos

Accept: 149    Submit: 256
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem 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

The first line of input contains a number T<50, where T denotes the number of test cases. Each case describes one head quarter scenario. The first line of each case starts with a positive integer 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, 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 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 such that, it will be possible to go from any building to another by using one or more roads.

 Output

For each case of input, there will be one line of output. It will contain the case number followed by the minimum time required to complete the mission. Look at the sample output for exact formatting.

 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

 Source

Summer Training I--Graph

题目大意:

有n个点,m条边,和一个起点终点,每一条边都是无向边,而且权值为1,问一条最长路,使得从起点先到这个点之后,再从这个点到终点的路径值。


思路:


1、Floyd求一遍最短路。


2、枚举一遍最大值,ans=max(map[s][i]+map[i][e])【0<=i<n】;


Ac代码:


#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int map[105][105];int main(){    int t;    int kase=0;    scanf("%d",&t);    while(t--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                map[i][j]=0x3f3f3f3f;            }            map[i][i]=0;        }        for(int i=0;i<m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            map[x][y]=map[y][x]=1;        }        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                for(int k=0;k<n;k++)                {                    map[j][k]=min(map[j][k],map[j][i]+map[i][k]);                }            }        }        int s,e;        int output=0;        scanf("%d%d",&s,&e);        for(int i=0;i<n;i++)        {            output=max(output,map[s][i]+map[i][e]);        }        printf("Case %d: %d\n",++kase,output);    }}






0 0
原创粉丝点击