前言

看到好多大佬都在跑分层图最短路,\(\text{DP}\) 解法的我瑟瑟发抖。。。

题目描述

给定一张 \(N\) 个点(点编号从 \(0\) 到 \(N-1\)),\(M\) 条边的无向带权图 \(G\)。给定常数 \(k\),你可以在图 \(G\) 中使不超过 \(k\) 条边的边权为 \(0\),求在该条件下点 \(s\) 到点 \(t\) 的最短路。

数据范围:\(2\le N\le10000,1\le M\le50000,0\le k\le10\)

基本思路

考虑 \(\text{DP}\)

我们设 \(dis[i][u]\) 表示从点 \(s\) 出发对 \(i\) 条边的边权清零时的最短路,答案就是 \(dis[k][t]\)

考虑转移:

首先跑一遍裸的最短路,求出 \(dis[0][u]\ u \in[0,n-1]\)

然后对于 \(\forall\ i \in [1,k]\)

\[dis[i][u]=min\{dis[i-1][v],dis[i][v]+(v,u)\}
\]

其中 \(u\) 是 \(v\) 的后继。

然后就可以开始转移了。

注意事项

  • 点编号从 \(0\) 到 \(N-1\)
  • 无向图边的空间开两倍

参考代码

/*--------------------------------
Author: The Ace Bee
Blog: www.cnblogs.com/zsbzsb
This code is made by The Ace Bee
--------------------------------*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
#define mp make_pair
#define pii pair < int, int >
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 10010;
const int __ = 100010;
int n, m, k, s, t, dis[11][_], exi[_];
int tot, head[_], nxt[__], ver[__], w[__];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; }
queue < int > q1, q2;
inline void spfa1(int x) {
memset(dis[x], 0x3f, sizeof dis[x]);
dis[x][s] = 0, exi[s] = 1;
q1.push(s), q2.push(s);
while (!q1.empty()) {
int u = q1.front(); q1.pop(), exi[u] = 0;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dis[x][v] > dis[x - 1][u]) {
dis[x][v] = dis[x - 1][u];
if (!exi[v])
exi[v] = 1, q1.push(v), q2.push(v);
}
}
}
}
inline void spfa2(int x) {
if (x == 0)
memset(dis[x], 0x3f, sizeof dis[x]), q2.push(s);
dis[x][s] = 0, exi[s] = 1;
while (!q2.empty()) {
int u = q2.front(); q2.pop(), exi[u] = 0;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dis[x][v] > dis[x][u] + w[i]) {
dis[x][v] = dis[x][u] + w[i];
if (!exi[v]) exi[v] = 1, q2.push(v);
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m), read(k), read(s), read(t);
for (rg int u, v, d, i = 1; i <= m; ++i)
read(u), read(v), read(d), Add_edge(u, v, d), Add_edge(v, u, d);
spfa2(0);
// for (rg int u = 0; u < n; ++u)
// printf("%d%c", dis[0][u], " \n"[u == n - 1]);
for (rg int i = 1; i <= k; ++i) {
spfa1(i);
// for (rg int u = 0; u < n; ++u)
// printf("%d%c", dis[i][u], " \n"[u == n - 1]);
spfa2(i);
// for (rg int u = 0; u < n; ++u)
// printf("%d%c", dis[i][u], " \n"[u == n - 1]);
}
printf("%d\n", dis[k][t]);
return 0;
}

完结撒花 \(QwQ\)

「JLOI2011」飞行路线的更多相关文章

  1. 「JLOI2011」「LuoguP4568」飞行路线(分层图最短路

    题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在nn个城市设有业务,设这些城市分别标记为00到n-1n−1,一共有mm种航线,每种航线连接两个城市,并且 ...

  2. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  3. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  4. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  5. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  6. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  7. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  8. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

  9. 「2014-3-17」C pointer again …

    记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

随机推荐

  1. Could not find result map com.youotech.tl_cons_credit_rating.entity.Result

    后端报错如下: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.yo ...

  2. eclipse修改快捷键

    eclipse修改快捷键 ctrl + shift + l查看快捷键 window -> preferences -> 搜索keys 鼠标点击以下五个表头,可以按照内容搜索 示例,选中Bi ...

  3. SIM7600CE TCP/IP连接与PPP拨号上网 4G上网

    SIM7600CE联网测试分为两部分: 1.TCP/IP连接 2.PPP拨号上网 实验环境:ubuntu-meta 16.04 硬件:树莓派3B,SIM7600CE 上网卡:移动的NB-IOT物联网卡 ...

  4. 判断是app 或者 网页

    function openInWebview () { let ua = navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger ...

  5. 消息队列(四)--- RocketMQ-消息发送2

    概述   接着我们上一篇继续分析消息发送,上节讲到消息发送前有可能遇到 broker 失效的情况,RocketMQ 主要是采用两种策略 : 重试发送 broker 故障延迟机制   后者指的是当发送给 ...

  6. Hadoop学习2—伪分布式环境搭建

    一.准备虚拟环境 1. 虚拟环境网络设置 A.安装VMware软件并安装linux环境,本人安装的是CentOS B.安装好虚拟机后,打开网络和共享中心 -> 更改适配器设置 -> 右键V ...

  7. 移动端,ajax 动态加载的元素,为动态添加的一系列同个类名的元素添加点击事件

    背景:一个列表页,有一系列同类名的元素,需要为每一个动态添加的列表项添加事件: 点击选择下图中不同的文档类型,再通过 ajax 动态加载不同的文档. 使用过的方法: 1.通知 jquery 的 $(s ...

  8. 操作系统OS - 阻塞(Blocking)非阻塞(Non-Blocking)与同步(Synchronous)异步(Asynchronous)

    参考: http://blog.jobbole.com/103290/ https://www.zhihu.com/question/19732473/answer/23434554 http://b ...

  9. Plastic Bottle Manufacturer - Consumer Demand For Plastic Bottles Becomes Higher

    Since transparent containers enable consumers to clearly see the contents, consumers are increasingl ...

  10. CN109241772A发票区块链记录方法、装置、区块链网关服务器和介质(腾讯)

    学习笔记-2 CN109241772A发票区块链记录方法.装置.区块链网关服务器和介质(腾讯) 解决什么问题? 让发票信息记录到区块链的情况下减少发票信息泄露 链上有什么数据? 发行发票事件信息(发票 ...