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;}
- Lightoj1174——Commandos(双向最短路)
- lightoj 1174 - Commandos(最短路)
- Foj 1635 Commandos【最短路Floyd】
- 最短路(弗洛伊德——最短路)
- POJ-----3268双向最短路
- POJ_1511_Invitation Cards【双向最短路】
- POJ 3268 Silver Cow Party(双向最短路)
- POJ 1511 Invitation Cards(双向最短路)
- 最短路—1001
- 最短路—1002
- 最短路—1003
- 最短路—1004
- 最短路—1005
- 最短路—1006
- 最短路—1007
- 最短路—1008
- 最短路—1009
- 百科—最短路
- 在linux命令下如何访问一个url
- Win32 SDK基础(11)—— 消息队列和GetMessage/PeekMessage、SendMessage/Postmesage
- PyTorch参数初始化和Finetune
- Linux进程间通信——使用消息队列
- 四轴pid调试心得
- Lightoj1174——Commandos(双向最短路)
- 树莓派 玩起来(四)Raspberry pi 安装中文支持及输入法
- 自动求导机制
- iOS企业版应用,Provisioning Profile文件即将过期怎么办?
- mysql直接导出查询数据到文本
- 深入学习Android系统上mount命令的使用
- python
- 高并发的epoll+线程池,业务在线程池内
- Linux进程间通信——使用流套接字