Lightoj1174——Commandos(双向最短路)

来源:互联网 发布:linux循环语句实例 编辑:程序博客网 时间:2024/06/06 01:02

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
Output for Sample Input
2
4
3
0 1
2 1
1 3
0 3
2
1
0 1
1 0
Case 1: 4
Case 2: 1

一队突击队要炸房子,房子由单位长度的道路连,一队人到了一个房子安完炸弹后,会分散开来去下一个相邻的房子,最后安完就全部返回一个特定的地点。求这个过程至少需要多久,炸房子的时间和突击队人数都不用考虑,为了出题也是够拼。。。
所以以起点和终点的房子为算法的起点,各执行一次最短路径,两次所有点的最短路径加起来,最大的那个就是至少需要的时间

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <math.h>#include <algorithm>#include <queue>#include <iomanip>#include <map>#define INF 0x3f3f3f3f#define MAXN 105#define Mod 20007using namespace std;int n,m;int vis[MAXN],mp[MAXN][MAXN],dis[MAXN],dis1[MAXN],dis2[MAXN];void dijkstra(int v){    memset(vis,0,sizeof(vis));    for(int i=0; i<n; ++i)        dis[i]=mp[v][i];    dis[v]=0;    vis[v]=1;    for(int i=0; i<n; ++i)    {        int minn=INF,p=-1,j;        for(j=0; j<n; ++j)        {            if(!vis[j]&&dis[j]<minn)            {                minn=dis[j];                p=j;            }        }        if(p==-1)            break;        vis[p]=1;        for(int j=0; j<n; ++j)            if(!vis[j]&&dis[j]>dis[p]+mp[p][j])                dis[j]=dis[p]+mp[p][j];    }}int main(){    int t;    scanf("%d",&t);    for(int cas=1; cas<=t; ++cas)    {        scanf("%d",&n);        for(int i=0; i<n; ++i)            for(int j=0; j<n; ++j)                mp[i][j]=INF;        scanf("%d",&m);        int u,v;        while(m--)        {            scanf("%d%d",&u,&v);            mp[u][v]=1;            mp[v][u]=1;        }        int s,d;        scanf("%d%d",&s,&d);        dijkstra(s);        for(int i=0;i<n;++i)            dis1[i]=dis[i];        dijkstra(d);        for(int i=0;i<n;++i)            dis2[i]=dis[i];        int ans=-INF;        for(int i=0;i<n;++i)            if(dis1[i]!=INF&&dis2[i]!=INF)                ans=max(ans,dis1[i]+dis2[i]);        printf("Case %d: %d\n",cas,ans);    }    return 0;}
0 0