-
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 解题分析:题目要求两点之间的最短距离,就是foyld经典算法要解决的问题。唯一的不同是真正的foyld算法是针对于有向图,这道题目是对于无向图,这只要在原有基础上加一个矩阵对称就ok了! code: #include<iostream.h> #include<string.h> #define INF 32767 int dist[101][101]; struct MGraph { int v[101]; int arc[100][100]; int vnum,arcnum; }; void CreatMGraph(MGraph &g) //建树 { int i,j,k; int m,n; cin>>g.vnum>>g.arcnum; for(i=0;i<g.vnum;i++) g.v[i]=i; for(j=0;j<g.vnum;j++) for(k=0;k<g.vnum;k++) { if(j==k) g.arc[j][k]=0; else g.arc[j][k]=INF; } for(i=0;i<g.arcnum;i++) { cin>>m>>n; g.arc[m][n]=1; g.arc[n][m]=1; } } void foyid(MGraph &g) //foyld算法求两点之间的最短路径,存于dist【100】。 { int i,j,k; for(i=0;i<g.vnum;i++) for(j=0;j<g.vnum;j++) { dist[i][j]=g.arc[i][j]; } for(k=0;k<g.vnum;k++) for(i=0;i<g.vnum;i++) for(j=0;j<g.vnum;j++) if(dist[i][k]+dist[k][j]<dist[i][j]){ dist[i][j]=dist[i][k]+dist[k][j]; } } int fun(MGraph &g) //求最短路径里面的最长时间,才是最短的 { int max=0; int a,b,i; cin>>a>>b; for(i=0;i<g.vnum;i++) { if(dist[a][i]+dist[i][b]>max) max=dist[a][i]+dist[i][b]; } return max; } int main() { int i; int n; cin>>n; for(i=1;i<=n;i++) { MGraph g; CreatMGraph(g); foyid(g); cout<<"Case "<<i<<": "<<fun(g)<<endl; memset(dist,0,sizeof(dist)); } return 0; }
|