FZU 1635 Commandos

 

Commandos

Time Limit:1s Memory limit:32M
Accepted Submit:42 Total Submit:58
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

Original: Summer Training I--Graph

 

解题:

        题目蛮长的,大概意思是突击队去炸建筑物的故事。思路理清,突击队里面有一群人,每人当且仅当有个建筑物目标要去炸掉,然后一起回到终点目标。构图思想,先求起点到其余各点建筑物的最短路径,再求其余的那些点到终点的最短路径。最后到达的那个人决定了总时间。Floyd算法求解,代码如下:

#include <iostream> using namespace std; #define MaxSize 100 #define INF 32767 /************************************************************************/ /* 弗洛伊德算法 */ /* roads[]数组存储邻接矩阵 */ /* dist[]数组存放每对顶点的最短路径 */ /* n代表顶点的个数 */ /************************************************************************/ void Floyd(int roads[][MaxSize],int dist[][MaxSize],int n) { int i,j,k; for (i=0;i<n;i++) { for (j=0;j<n;j++) { dist[i][j]=roads[i][j]; } } for (k=0;k<n;k++) { for (i=0;i<n;i++) { for (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 T,N,R,s,d,a,b,i,j,k,min; int roads[MaxSize][MaxSize]; int dist[MaxSize][MaxSize]; cin>>T; for (k=0;k<T;k++) { memset(roads,INF,sizeof(roads)); memset(dist,INF,sizeof(dist)); cin>>N>>R; for (i=0;i<N;i++) { for (j=0;j<N;j++) { roads[i][j]=INF; if (i==j) { roads[i][j]=0; } } } while (R--) { cin>>a>>b; roads[a][b]=1; roads[b][a]=1; } Floyd(roads,dist,N); cin>>s>>d; min=0; for (i=0;i<N;i++) { if (dist[s][i]+dist[i][d]>min) { min=dist[s][i]+dist[i][d]; } } cout<<"Case "<<k+1<<": "<<min<<endl; } return 0; }

你可能感兴趣的:(Integer,ini,input,each,Training,Numbers)