题解

题解

考虑到正着跑不好想, 我们尝试反向跑

以每个终点作为起点, 维护每个点的最小值和次小值(最小的被老虎ban掉了)

转移的时候用当前点的次小值去更新其所连的点的最小值和次小值

由于最小的次小值不能被其他次小值所更新, 所以我们可以使用dijkstra

把每个终点丢进去跑dijkstra

最后输出$1$的次小值即可

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define itn int
#define reaD read
#define mp(x,y) make_pair(1ll*x,y)
#define N 200005
using namespace std; int n, m, k, head[N], cnt;
struct edge { int to, next, cost; } e[N << 5];
bool vis[N];
long long dis1[N], dis2[N]; namespace Heap
{
pair<long long, int> heap[N << 2]; int sz = 0;
void push(pair <int, int> x) { x.first *= -1; heap[++sz] = x; push_heap(heap + 1, heap + sz + 1); }
void pop() { pop_heap(heap + 1, heap + sz + 1); sz--; }
pair<int, int> top() { pair<int, int> tmp; tmp = heap[1]; tmp.first *= -1; return tmp; }
bool empty() { return !sz; }
}; using namespace :: Heap; inline int read()
{
int x = 0, w = 1; char c = getchar();
while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * w;
} inline void adde(int u, int v, int w) { e[++cnt] = (edge) { v, head[u], w }; head[u] = cnt; } void dijkstra()
{
while(!empty())
{
int u = top().second; pop();
if(vis[u]) continue; vis[u] = 1;
for(int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
long long sum = dis2[u] + e[i].cost;
if(sum < dis2[v])
{
if(sum < dis1[v]) dis2[v] = dis1[v], dis1[v] = sum;
else dis2[v] = sum;
}
if(dis2[v] < dis1[0] && !vis[v]) push(mp(dis2[v], v));
}
}
} int main()
{
n = read(); m = read(); k = read();
for(int i = 1; i <= m; i++)
{
itn u = read() + 1, v = read() + 1, w = read();
adde(u, v, w); adde(v, u, w);
}
memset(dis1, 0x3f, sizeof(dis1));
memset(dis2, 0x3f, sizeof(dis2));
for(int i = 1; i <= k; i++)
{
int x = reaD();
dis1[x + 1] = dis2[x + 1] = 0;
push(mp(dis2[x + 1], x + 1));
}
dijkstra();
printf("%lld\n", dis2[1]);
return 0;
}

[题解] [bzoj2622] 深入虎穴的更多相关文章

  1. BZOJ2622 深入虎穴(最短路径)

    如果对某个点能求出与其相邻的所有点到达出口的最短时间,那么该点的答案就可以在其中取次小值了. 对于dijkstra魔改一下就能做到这个.初始时将所有出口的最短时间设为0并放入堆,记录最短和次短路径,每 ...

  2. 【BZOJ2622】[2012国家集训队测试]深入虎穴 次短路

    [BZOJ2622][2012国家集训队测试]深入虎穴 Description 虎是中国传统文化中一个独特的意象.我们既会把老虎的形象用到喜庆的节日装饰画上,也可能把它视作一种邪恶的可怕的动物,例如“ ...

  3. 2019年GPLT L2-3 深入虎穴 比赛题解 中国高校计算机大赛-团体程序设计天梯赛题解

    著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是一个房间,或者又有很多条路,同样是每条 ...

  4. 【BZOJ2622】[2012国家集训队测试]深入虎穴

    虎是中国传统文化中一个独特的意象.我们既会把老虎的形象用到喜庆的节日装饰画上,也可能把它视作一种邪恶的可怕的动物,例如“武松打虎”或者“三人成虎”.“不入虎穴焉得虎子”是一个对虎的威猛的形象的极好体现 ...

  5. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  6. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  7. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  8. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  9. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

随机推荐

  1. vs2010 回车、退格键等不能用

    有时候在vs2010中,突然回退键.回车键.方向键就用不了了,百度一堆方法,最后找到按Alt+Enter,就可以用了.

  2. JS基础_break和continue

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. linux core文件的打开和分析

    1. core文件生成打开方式: ulimit -c unlimited echo "1" > /proc/sys/kernel/core_uses_pid 看下服务器上是否 ...

  4. 抓包工具之tcpdump

    tcpdump 官网 -> http://www.tcpdump.org 1. 安装步骤 在官网分别下载 Tcpdump.Libpcap 这两个包链接 在安装Tcpdump之前,先安装Libpc ...

  5. Mac之常见问题

    1. 在命令行下无法使用ll命令 需要设置命令的别名. 文件位置:-/.bash_profile source ~/.profile export PATH="/usr/local/opt/ ...

  6. java9 新特征

    Java 平台级模块系统 java模块化解决的问题:减少Java应用和Java核心运行时环境的大小与复杂性 模块化的 JAR 文件都包含一个额外的模块描述器.在这个模块描述器中, 对其它模块的依赖是通 ...

  7. centos7重启网卡报Job for network.service failed because...错误

    解决: [root@mina0 hadoop]# systemctl stop NetworkManager[root@mina0 hadoop]# systemctl disable Network ...

  8. hadoop 中ALL Applications 中Tracking 下History查找不到MapReduce Job 日志

    运行一个Map Reduce job 想查看日志: 点击History ,找不到网页 解决办法如下: 1.其中有一个进程是需要启动的: Hadoop自带了一个历史服务器,可以通过历史服务器查看已经运行 ...

  9. 无聊开始玩路由器,入门Tomato固件

    无聊开始玩路由器,入门Tomato固件 在Wordpress上消失了一段时间,其实我最近几个月都比较宅.相信最近大家都了解过新出的一款很NB的路由器叫极路由,功能很强大,8G的可存储空间,可以安装路由 ...

  10. centos6.4升级openssh7.4p1

    Centos6.4版本yum升级openssh版本最高到5.3,想要升级到更高的版本需要重新编译 一.查看当前openssh版本: [root@localhost ~]# ssh -VOpenSSH_ ...