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.
InputOutputSample Input2 4 3 0 1 2 1 1 3 0 3 2 1 0 1 1 0 Sample OutputCase 1: 4 Case 2: 1 Original: Summer Training I--Graph |
由题目的意思可以知道所求的是一共需要的时间,那么这个时间是由开始点到终点的最大时间决定的(由最慢的决定总时间)
floyd就可以过了
下面是代码:
- #include <iostream>
- using namespace std;
- #define MAX 101
- int grap[MAX][MAX];
- void floyd(int n)
- {
- int i,j,k;
- for(k=0;k<n;k++)
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- {
- if(k==j||i==j)continue;
- 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];
- }
- }
- int main()
- {
- int t,i,x,y,s,e,j,k,m,n,id=0,ma;
- cin>>t;
- while(t--)
- {
- cin>>n;
- cin>>k;
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- if(i!=j) grap[i][j]=100000000;
- else
- grap[i][j]=0;
- for(i=1;i<=k;i++)
- {
- scanf("%d%d",&x,&y);
- grap[x][y]=grap[y][x]=1;
- }
- scanf("%d%d",&s,&e);
- floyd(n);
- ma=0;
- for(i=0;i<n;i++)
- {
- if(grap[s][i]!=100000000&&grap[i][e]!=100000000&&grap[s][i]+grap[i][e]>ma)
- ma=grap[s][i]+grap[i][e];
- }
- printf("Case %d: ",++id);
- cout<<ma<<endl;
- }
- }