lightoj 1174 - Commandos(最短路)
来源:互联网 发布:幽灵行动荒野优化如何 编辑:程序博客网 时间:2024/05/21 18:41
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<bits/stdc++.h>using namespace std;const int maxn = 1e4+5;int n, m, k, head[maxn], dis1[maxn], dis2[maxn];struct node{int v, next;}edge[maxn];void addEdge(int u, int v){edge[k].v = v;edge[k].next = head[u];head[u] = k++;}void bfs(int u, int dis[]){dis[u] = 0;queue<int> q;q.push(u);while(!q.empty()){u = q.front(); q.pop();for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].v;if(dis[v] == -1) dis[v] = dis[u]+1, q.push(v);}}}int main(void){int t, s, e, ca = 1;cin >> t;while(t--){k = 0;memset(head, -1, sizeof(head));cin >> n >> m;while(m--){int u, v;scanf("%d%d", &u, &v);addEdge(u, v);addEdge(v, u);}scanf("%d%d", &s, &e);int ans = 0;memset(dis1, -1, sizeof(dis1));memset(dis2, -1, sizeof(dis2));bfs(s, dis1);bfs(e, dis2);for(int i = 0; i < n; i++)ans = max(ans, dis1[i]+dis2[i]);printf("Case %d: ", ca++);printf("%d\n", ans);}return 0;}
方法二,起点,终点各进行一次spfa
#include<bits/stdc++.h>using namespace std;const int maxn = 1e4+5;const int INF = 0x3f3f3f3f;int n, m, k, head[maxn], dis1[maxn], dis2[maxn];bool book[maxn];struct node{int v, next;}edge[maxn];void addEdge(int u, int v){edge[k].v = v;edge[k].next = head[u];head[u] = k++;}void spfa(int u, int *dis){memset(book, 0, sizeof(book));queue<int> q;q.push(u);book[u] = 1;dis[u] = 0;while(!q.empty()){u = q.front(); q.pop();book[u] = 0;for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].v;if(dis[u]+1 < dis[v]){dis[v] = dis[u]+1;if(!book[v]) book[v] = 1, q.push(v);}}}}int main(void){int t, ca = 1;cin >> t;while(t--){k = 0;memset(head, -1, sizeof(head));scanf("%d%d", &n, &m);while(m--){int u, v;scanf("%d%d", &u, &v);addEdge(u, v);addEdge(v, u);}int s, e, ans = 0;scanf("%d%d", &s, &e);memset(dis1, INF, sizeof(dis1));memset(dis2, INF, sizeof(dis2));spfa(s, dis1);spfa(e, dis2);for(int i = 0; i < n; i++)ans = max(ans, dis1[i]+dis2[i]);printf("Case %d: ", ca++);printf("%d\n", ans);}return 0;}
方法三,直接进行一次floyd,可以得到两两之间最短路
#include<bits/stdc++.h>using namespace std;const int maxn = 105;const int INF = 0x3f3f3f3f;int n, m, dis[maxn][maxn];void init(void){for(int i = 0; i < n; i++)for(int j = 0; j < n; j++)dis[i][j] = i==j ? 0 : INF;}void floyd(void){for(int k = 0; k < n; k++)for(int i = 0; i < n; i++)for(int j = 0; j < n; j++)dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);}int main(void){int t, ca = 1;cin >> t;while(t--){scanf("%d%d", &n, &m);init();while(m--){int u, v;scanf("%d%d", &u, &v);dis[u][v] = dis[v][u] = 1;}floyd();int s, e, ans = 0;scanf("%d%d", &s, &e);for(int i = 0; i < n; i++){if(dis[s][i] == INF || dis[i][e] == INF) continue;ans = max(ans, dis[s][i]+dis[i][e]);}printf("Case %d: ", ca++);printf("%d\n", ans);}return 0;}
- lightoj 1174 - Commandos(最短路)
- LightOJ 1174--Commandos (BFS)
- LightOJ - 1174 Commandos
- LightOJ - 1174 Commandos(floyd)
- lightOJ 1174 - Commandos
- LIGHTOJ 1174 – COMMANDOS 【FLOYD】
- LightOJ 1174 Commandos(FLoyd)
- Foj 1635 Commandos【最短路Floyd】
- Lightoj1174——Commandos(双向最短路)
- [最短路+水题]LightOJ 1019 - Brush (V)
- 【最短路】 LightOJ 1002 - Country Roads 变形
- LIGHTOJ 1019 – BRUSH (V) 【最短路】
- LightOJ 1002(最短路Dijkstra算法)
- LightOJ Brush (V) 1019【简单最短路】
- Lightoj 1281 (二维最短路)
- lightoj 1019 - Brush (V) 【最短路 floyd】
- LightOJ - 1019 Brush (V) (最短路) 水
- LightOJ 1002 Country Roads 最短路变形
- 删除字符串末尾所有空格
- win7安装chromedriver
- Android权限uses-permission
- python referenced before assignment问题
- LoRa组网案例3:智能温控,上报+唤醒
- lightoj 1174 - Commandos(最短路)
- java--联系人管理系统
- ps -ef 输出结果的具体含义
- NSRecursiveLock递归锁的使用
- poj 2112 Optimal Milking(二分+最大流)
- [LeetCode]51. N-Queens&52. N-Queens II
- MVP、Retrofit、RxJava、RxBus
- hdu 4445 crazy tank/HDU 4454 stealing acake/SGU 204 Little jumper
- 暑期社会实践心得