FOJ 1635 Commandos
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
: , 。 。
, 。 。
: , Floyd , ,
, 。
#include <stdio.h>
#define MaxVectex 110
#define MaxDistance 9999999
void Floyd(int Graph[][MaxVectex],int Vectex);
int begin;
int end;
int main()
{
int Graph[MaxVectex][MaxVectex];
register int i,j,k;
int NumOfTest;
int max;
int NumOfBuildings,NumOfRoads;
while (scanf("%d",&NumOfTest)!=EOF)
{
for (k=0;k<NumOfTest;k++)
{
max = 0;
scanf("%d",&NumOfBuildings);
scanf("%d",&NumOfRoads);
for (i=0;i<NumOfBuildings;i++)
{
for (j=0;j<NumOfBuildings;j++)
{
if (i == j)
{
Graph[i][j] = 0;
}
else
Graph[i][j] = MaxDistance;
}
}
for (i=0;i<NumOfRoads;i++)
{
scanf("%d%d",&begin,&end);
Graph[begin][end] = 1;
Graph[end][begin] = 1;
}
Floyd(Graph,NumOfBuildings);
scanf("%d%d",&begin,&end);
for (i=0;i<NumOfBuildings;i++)
{
if (max<Graph[begin][i]+Graph[i][end])
{
max = Graph[begin][i]+Graph[i][end];
}
}
printf("Case %d: %d/n",k+1,max);
}
}
return 0;
}
void Floyd(int Graph[][MaxVectex],int Vectex)
{
int i,j,k;
for (i=0;i<Vectex;i++)
{
for (j=0;j<Vectex;j++)
{
for (k=0;k<Vectex;k++)
{
if (Graph[j][k]>Graph[j][i]+Graph[i][k])
{
Graph[j][k] = Graph[j][i]+Graph[i][k];
}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
cocos2d Lua 학습(一)ios에서 루아 함수 호출 및 전참 방법 lua 코드: 출력 결과: lua 호출 C++ 방법: add 함수: lua 코드: 출력 결과: 함수를 호출합니다. 함수를 호출하려면 다음 협의를 따르십시오. 우선, 호출할 함...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.