题目链接:

  Lightoj  1174 - Commandos

题目描述:
  有一军队秉承做就要做到最好的口号,准备去破坏敌人的军营。他们计划要在敌人的每一个军营里都放置一个炸弹。军营里有充足的士兵,每个士兵都可以携带充足的炸弹,问这个军队完成任务最少需要时间?(假设士兵在往敌军帐篷里放炸弹时,敌军不会防御。敌人是猪嘛?这是在和猪战斗嘛?

解题思路:
  水题,求士兵完成任务的最短时间,也就是说每个士兵执行任务的时候只能选择最优路径咯。但是又要满足每个点都要被走过,所以就要求出能覆盖所有点的最短路径里最长一条的长度咯。只需要从起点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)的更多相关文章

  1. LightOJ 1012 简单bfs,水

    1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...

  2. 暑期训练狂刷系列——Lightoj 1084 - Winter bfs

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k, ...

  3. 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 ...

  4. lightoj 1111 - Best Picnic Ever(dfs or bfs)

    题目链接 http://www.lightoj.com/volume_showproblem.php?problem=1111 题意:给你一个有向图再给你几个人的位置,问所有人可以在哪些点相聚. 简单 ...

  5. 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 ...

  6. Lightoj 1066 Gathering Food (bfs)

    Description Winter is approaching! The weather is getting colder and days are becoming shorter. The ...

  7. LightOJ 1009 二分图染色+BFS/种类并查集

    题意:有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个. 思路:可以使用种类并查集写.也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BF ...

  8. hihocoder 1174 [BFS /拓扑排序判断是否有环]

    hihocoder 1174 [算法]: 计算每一个点的入度值deg[i],这一步需要扫描所有点和边,复杂度O(N+M). 把入度为0的点加入队列Q中,当然有可能存在多个入度为0的点,同时它们之间也不 ...

  9. LightOJ——1066Gathering Food(BFS)

    1066 - Gathering Food   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB W ...

随机推荐

  1. Navicat for MySQL出现#1045 错误怎么办

    #1045 - Access denied for user 'root'@'localhost' (using password: NO)这是因为你连接的时候没有密码或者密码没改对导致的.如下图所示 ...

  2. Codeforces 344B Simple Molecules

    #include<bits/stdc++.h> using namespace std; int main() { int a,b,c; scanf("%d%d%d", ...

  3. 各项异性滤波简单介绍Anisotropic Filtering(AF)

    本文主要整理简绍来自互联网的各项异性滤波的知识. 原文链接:http://www.linuxgraphics.cn/graphics/using_anisotropic_texture_filteri ...

  4. Codeforces Round #253 (Div. 1) A Borya and Hanabi

    A. Borya and Hanabi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. 移动APP怎样保存用户password

    <span style="font-size:14px;">为了更好的用户体验,移动APPclient一般都会将用户信息进行保存以便兴许能够自己主动登录.</sp ...

  6. Ubuntu16.04下安装Tensorflow GPU版本(图文详解)

    不多说,直接上干货! 推荐 全网最详细的基于Ubuntu14.04/16.04 + Anaconda2 / Anaconda3 + Python2.7/3.4/3.5/3.6安装Tensorflow详 ...

  7. JSON序列化-化繁为简

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. P1439 排列LCS问题

    P1439 排列LCS问题 56通过 220提交 题目提供者yeszy 标签二分动态规划 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 给出1-n的两个排列P1和P2 ...

  9. [IMX6DL][Android4.4] 电池低电量告警提示【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/51789964 之前版本的电池电量低是通过发送 intent ACTION_BATTERY_L ...

  10. HDU4027 Can you answer these queries? —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...