Commandos LightOJ - 1174
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
Input starts with an integer T (≤50), denoting the number of test cases.
The first line of each case starts with a positive integer N (1 ≤ 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 u v (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 s d (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 given such that, it will be possible to go from any building to another by using one or more roads.
Output
For each case, print the case number and the minimum time required to complete the mission.
Sample Input
2
4
3
0 1
2 1
1 3
0 3
2
1
0 1
1 0
Sample Output
Case 1: 4
是在大神的提示下写的~~~
题意:一幅图中,找从起点到终点的最远距离。。。
做法:算出每一个点到起点和终点的最短距离,然后从最短距离中找最长距离。。。
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 #define N 105 7 using namespace std; 8 struct edge{ 9 int to; 10 int next; 11 }e[N*3]; 12 int tot; 13 int head[N],vis[N],dis[N]; 14 void init() 15 { tot=0; 16 memset(head,-1,sizeof(head)); 17 } 18 void addedge(int u,int v) 19 { e[tot].to=v; 20 e[tot].next=head[u]; 21 head[u]=tot++; 22 } 23 int BFS(int from,int to) 24 { if(from==to) return 0; 25 memset(vis,0,sizeof(vis)); 26 memset(dis,0,sizeof(dis)); 27 28 queue<int> q; 29 q.push(from); 30 vis[from]=1; 31 while(!q.empty()) 32 { int v=q.front(); 33 q.pop(); 34 for(int i=head[v];i!=-1;i=e[i].next) 35 { int t=e[i].to; 36 if(!vis[t]) 37 { vis[t]=1; 38 dis[t]=dis[v]+1; 39 if(t==to) return dis[t]; 40 q.push(t); 41 } 42 } 43 } 44 return -1; 45 } 46 int main() 47 { int k,n,m,p,q,tem,start,end,t; 48 scanf("%d",&k); 49 for(t=1;t<=k;t++) 50 { init(); 51 int ans=0; 52 scanf("%d",&n); 53 scanf("%d",&m); 54 for(int i=0;i<m;i++){ 55 scanf("%d%d",&p,&q); 56 addedge(p,q); 57 addedge(q,p); 58 } 59 scanf("%d%d",&start,&end); 60 for(int i=0;i<n;i++) 61 { tem=BFS(i,start)+BFS(i,end); 62 if(tem>ans) ans=tem; 63 } 64 printf("Case %d: %d\n",t,ans); 65 } 66 }
Case 2: 120:40:52