Lightoj 1174 - Commandos (bfs)
题目链接:
题目描述:
有一军队秉承做就要做到最好的口号,准备去破坏敌人的军营。他们计划要在敌人的每一个军营里都放置一个炸弹。军营里有充足的士兵,每个士兵都可以携带充足的炸弹,问这个军队完成任务最少需要时间?(假设士兵在往敌军帐篷里放炸弹时,敌军不会防御。敌人是猪嘛?这是在和猪战斗嘛?)
解题思路:
水题,求士兵完成任务的最短时间,也就是说每个士兵执行任务的时候只能选择最优路径咯。但是又要满足每个点都要被走过,所以就要求出能覆盖所有点的最短路径里最长一条的长度咯。只需要从起点bfs一遍所有的点,然后从终点bfs一遍所有的点。辣么从起点到终点并且经过x的最短路径长度就是两次bfs出来的结果和咯。
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
struct node
{
int to, next;
}edge[maxn*maxn];
int head[maxn], tot, a[maxn], b[maxn]; void Add (int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot ++;
} void bfs (int s, int vis[])
{
queue <int> Q;
Q.push(s);
vis[s] = ;
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
if (vis[v]==-)
{
vis[v] = vis[u] + ;
Q.push (v);
}
}
}
} int main ()
{
int t, n, r, s, e, cas = ;
scanf ("%d", &t); while (t --)
{
scanf ("%d %d", &n, &r);
memset (head, -, sizeof(head));
tot = ; for (int i=; i<r; i++)
{
int u, v;
scanf ("%d %d", &u, &v);
Add (u, v);
Add (v, u);
} scanf ("%d %d", &s, &e);
memset (a, -, sizeof(a));
memset (b, -, sizeof(b));
bfs (s, a);
bfs (e, b); int ans = ;
for (int i=; i<n; i++)
ans = max (ans, a[i]+b[i]);
printf("Case %d: %d\n", cas++, ans);
}
return ;
}
Lightoj 1174 - Commandos (bfs)的更多相关文章
- LightOJ 1012 简单bfs,水
1.LightOJ 1012 Guilty Prince 简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...
- 暑期训练狂刷系列——Lightoj 1084 - Winter bfs
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k, ...
- lightoj 1046 - Rider(bfs)
A rider is a fantasy chess piece that can jump like a knight several times in a single move. A rider ...
- lightoj 1111 - Best Picnic Ever(dfs or bfs)
题目链接 http://www.lightoj.com/volume_showproblem.php?problem=1111 题意:给你一个有向图再给你几个人的位置,问所有人可以在哪些点相聚. 简单 ...
- LightOJ 1337 F - The Crystal Maze (bfs)
Description You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As ...
- Lightoj 1066 Gathering Food (bfs)
Description Winter is approaching! The weather is getting colder and days are becoming shorter. The ...
- LightOJ 1009 二分图染色+BFS/种类并查集
题意:有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个. 思路:可以使用种类并查集写.也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BF ...
- hihocoder 1174 [BFS /拓扑排序判断是否有环]
hihocoder 1174 [算法]: 计算每一个点的入度值deg[i],这一步需要扫描所有点和边,复杂度O(N+M). 把入度为0的点加入队列Q中,当然有可能存在多个入度为0的点,同时它们之间也不 ...
- LightOJ——1066Gathering Food(BFS)
1066 - Gathering Food PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB W ...
随机推荐
- cds.data:=dsp.data赋值有时会出现AV错误剖析
cds.data:=dsp.data赋值有时会出现AV错误剖析 如果QUERY没有查询到任何数据,cds.data:=dsp.data赋值会触发AV错误. 大家知道,DATASNAP有许多远程方法就是 ...
- ECC数据结构
在SM2 ECC算法中,有针对签名加密的数据结构,下面对这些结构进行分析 #define ECCref_MAX_BITS 512 #define ECCref_MAX_LEN ((ECCref_MAX ...
- 常见Python运行时错误
1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”) 该错误将发生在 ...
- centos 安装python2.7
安装pip sudo yum -y install epel-release sudo yum -y install python-pip 下载解压Python-2.7.3 #wget http:// ...
- POJ2573 Bridge 经典的过桥问题
曾经遇到过类似的.纪念一下!这题同一时候也是 ZOJ1877.经典的过桥问题 是有个博客解说的非常好的 戳这里 挺久曾经.遇到过一个基本一样的,那个题目仅仅要求求出 最短时间就可以,如今还有过桥的过 ...
- 【.NET Core项目实战-统一认证平台】基于jackcao博客使用VSCode开发及感悟One搭建开发环境
原博客系列文章链接:https://www.cnblogs.com/jackcao/ 金焰的世界 感谢博主无私的奉献,感谢博主幼儿班的教学 基于jackcao博客使用VsCode开发及感悟One搭建开 ...
- hive计算网页停留时长
hive表结构例如以下: create table pv_user_info( session_id string, user_id string, url string, starttime big ...
- leetcode——Implement strStr() 实现字符串匹配函数(AC)
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- win7-64bit安装comtypes的问题
Update 28/12/2014: Please download the latest comtypes 1.1.1 from https://pypi.python.org/pypi/comty ...
- 【iOS系列】-oc中特有的语法
[iOS系列]-oc中特有的语法 oc数据类型: 1,基本类型 2,对象类型 3,id 4,BOOL 5,block 6,SEL 1:category 使用继承关系来扩充一个类,有一个弊病,高耦合性 ...