FOJ 1635 Commandos

 

Commandos
Time Limit:1s Memory limit:32M
Accepted Submit:27 Total Submit:32
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就可以过了

下面是代码:

  1. #include <iostream>
  2. using namespace std;
  3. #define MAX 101
  4. int grap[MAX][MAX];
  5. void floyd(int n)
  6. {
  7.     int i,j,k;
  8.      for(k=0;k<n;k++)
  9.         for(i=0;i<n;i++)
  10.             for(j=0;j<n;j++)
  11.                 {
  12.                 if(k==j||i==j)continue;
  13.                 if(i==k)break;  grap[i][j]= grap[i][j]<grap[i][k]+grap[k][j]? grap[i][j]: grap[i][k]+grap[k][j];
  14.                 }
  15. }
  16. int main()
  17. {
  18.     int t,i,x,y,s,e,j,k,m,n,id=0,ma;
  19.     cin>>t;
  20.     while(t--)
  21.         {
  22.             cin>>n;
  23.             cin>>k;
  24.             for(i=0;i<n;i++)
  25.                 for(j=0;j<n;j++)
  26.                         if(i!=j) grap[i][j]=100000000;
  27.                         else
  28.                             grap[i][j]=0;
  29.             for(i=1;i<=k;i++)
  30.                 {
  31.                     scanf("%d%d",&x,&y);
  32.                     grap[x][y]=grap[y][x]=1;
  33.                 }
  34.             scanf("%d%d",&s,&e);
  35.             floyd(n);
  36.             ma=0;
  37.             for(i=0;i<n;i++)
  38.                 {
  39.                     if(grap[s][i]!=100000000&&grap[i][e]!=100000000&&grap[s][i]+grap[i][e]>ma)
  40.                         ma=grap[s][i]+grap[i][e];
  41.                 }
  42.             printf("Case %d: ",++id);
  43.             cout<<ma<<endl;
  44.         }
  45. }

你可能感兴趣的:(FOJ 1635 Commandos)